SQL Right Join

HTML
CSS
C#
SQL

SQL Right Join

Join operation in SQL is used to combine multiple tables together into a single table. If we use the right join to combine two different tables, then we will get all the records from the right table. But we will get only those records from the left table, which have the corresponding key in the right table. Rest other records in the left table for which the common column value doesn’t match with the common column value of the right table; displayed as NULL.

Syntax:

SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1 RIGHT JOIN TableName2 ON TableName1.ColumnName = TableName2.ColumnName;

Table 1: Loan
Table 2: Borrower
Example

Write a query to perform the right join operation considering the loan table as the left table and the borrower table as the right table.

SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l RIGHT JOIN Borrower b ON l.LoanID = b.LoanID;

You will get the following output:

LoanID, Branch, Amount, CustID, CustName are retrieved from loan and borrower tables. All the records from the borrower table are retrieved. Only those records that have a corresponding LoanID in the borrower table are retrieved from the loan table. Rest other records in the loan table for which a LoanID doesn’t match with the LoanID of the borrower table, are displayed as NULL.

Course Video

1. Write a SQL query to get all productCategoryId, CategoryNamw with Product SubcategoryId, name from Production.ProductSubcategory and Production.ProductCategory.

NOTE: OUTPUT on SQL Editor Count: 37 Rows.
OUTPUT

2. Write a SQL query to get all employeeID, Job title WITH firstName, LastName, startDate from Person, HumanResources.Employee and HumanResources.EmployeeDepartmentHistory tables.

NOTE: OUTPUT on SQL Editor Count: 100 Rows.
OUTPUT

3. Write a query to join salesOrderHeader, SalesOrderDetail, SpecialOfferproduct and SpecialOffer table to retrieve two columns from each table.

NOTE: OUTPUT on SQL Server Count: 5579 Rows,

OUTPUT on SQL Editor Count: 170 Rows.

OUTPUT

4. write a SQL query to get all PurchaseOrderHeader details with ShipMethods Name, ShipRate.

NOTE:  OUTPUT on SQL Editor Count: 100 Rows.
OUTPUT

5. write a SQL query to join PurchaseOrderDetail, PurchaseOrderHeader, ShipMethod table to retrieve two columns from each table.

NOTE: OUTPUT on SQL Editor Count: 156 Rows.
OUTPUT

NOTE: Use NORTHWIND DATABASE For Practicing these Questions.

6. Compose a query to extract all product IDs and their corresponding unit prices, alongside the associated order IDs and order dates from the OrderDetails and Orders tables. [Output: 2155 ROWS]

OUTPUT

7. write a query to retrieve all product IDs and their respective unit prices from order details table, along with the associated order IDs, order dates, product names,and quantities per unit from orders and products tables, while also ensuring that only orders containing products with a unit price higher than the average unit price of all products are included. [ output: 718 rows]

 

OUTPUT

8. write a query to extract all product IDs and their corresponding product names, along with the order IDs, unit prices, quantities, category names, supplier IDs, contact titles, and cities, from the Products, OrderDetails, Categories, and Suppliers tables, with the additional stipulation that only orders containing products from suppliers located in cities starting with the letter ‘M’ are considered. [output: 376 ROWS]

OUTPUT

9. Compose a SQL query to join the Shippers table with the Orders, Customers, and Employees tables, extracting two columns from each table. Apply filters that include restrict employee records based on a hire date earlier than January 1st, 1993. [OUTPUT: 352 ROWS]

OUTPUT

10. Compose a SQL query to get all Order IDs and Quantities alongside Product IDs, Product Names, Category IDs, Category Names, Company Names, and Contact Names from the OrderDetails, Products, and Categories tables. Ensure that only order details with a discount greater than zero are included, and categories labeled as ‘confections’ or ‘produce’ [OUTPUT: 211 ROWS]

OUTPUT