JOINS in SQL Server With Examples
JOINS in SQL Server With Examples
JOINS in SQL Server With Examples
Join facilitates the retrieval of information from multiple tables. In Sql server we
have following 6 types of Joins:
1. INNER JOIN
2. LEFT OUTER JOIN
3. RIGHT OUTER JOIN
4. FULL OUTER JOIN
5. CROSS JOIN
6. SELF JOIN
To demo these features let us first create the Customers and Orders table as
depicted in the below image by using the following script:
Demo 1: As per the data in our demo tables, Customers with CustomerId 1 and 3
in Customers table have the orders in the Orders table. Where as the customer
with CustomerId 2 doesnt have any order in the Orders table. So the Inner Join on
the CustomerId column between Customers and Orders table will return the
Customer and Order details of the Customers with CustomerId 1 and 3 only.
SELECT *
FROM Customers C
INNER JOIN Orders O
ON O.CustomerId = C.CustomerId
RESULT:
Demo 2: Below Inner Join query demonstrates how to get name of all the
Customer who have at-least one order in the Orders table.
SELECT C.Name
FROM Customers C
INNER JOIN Orders O
ON O.CustomerId = C.CustomerId
RESULT:
Name
-
Basavaraj
Shree
Demo 1: As per the data in our demo tables, Customers with CustomerId 1 and 3
in Customers table have the orders in the Orders table. Where as the customer
with CustomerId 2 doesnt have any order in the Orders table. So the Left join on
the CustomerId column between Customers and Orders table will return the
Customer and Order details of the Customers with CustomerId 1 and 3 and for
CustomerId 2 the Order Table columns will have NULL value in the result.
SELECT *
FROM Customers C
LEFT OUTER JOIN Orders O
ON O.CustomerId = C.CustomerId
RESULT:
Demo 2: Below query demonstrates how to get the name of the Customer who
dont have Orders using LEFT OUTER JOIN.
RESULT:
CustomerId Name
2 Kalpana
Demo 1: As per the data in our demo tables, only for the order with OrderId 200
we dont have its corresponding customer info with CustomerId 4 in the
Customers table. And for the other two orders, the corresponding customer info is
present in the Customers Table. So for the orders with CustomerId 1 and 3 will
have customer details and for the order with CustomerId 4, the Customers table
columns will have NULL value in the result.
SELECT *
FROM Customers C
RIGHT OUTER JOIN Orders O
ON O.CustomerId = C.CustomerId
RESULT:
Demo 2: Below query demonstrates how to get the Orders with a CustomerId, for
which we dont have a mapping any record in the Customers Table:
SELECT O.*
FROM Customers C
RIGHT OUTER JOIN Orders O
ON O.CustomerId = C.CustomerId
WHERE C.CustomerId IS NULL
RESULT:
OrderId CustomerId OrderDate
200 4 2014-01-31 23:48:32.853
And for the Order with OrderId 200 having CustomerId 4 doesnt have a matching
record in the customer table with CustomerId 4. So in the result of FULL Outer
join between Customers and Orders table on the CustomerId column will have
NULL values for the Customers table columns for the Order with OrderId 200.
SELECT *
FROM Customers C
FULL OUTER JOIN Orders O
ON O.CustomerId = C.CustomerId
RESULT:
Demo 2: Below query demonstrates how to get the list of all the Customers
without Orders and also the Orders which doesnt have corresponding customer
in the Customers Table.
SELECT *
FROM Customers C
FULL OUTER JOIN Orders O
ON O.CustomerId = C.CustomerId
WHERE C.CustomerId IS NULL OR O.OrderId IS NULL
RESULT:
No.of Rows in the Result of CRoss Join = (No. of Rows in LEFT Table) * (No. of
Rows in RIGHT Table)
SELECT *
FROM Customers C
CROSS JOIN Orders O
RESULT:
To demo this join let us create an Employee table with data as depicted in the
below image by the following script:
SELECT E.EmployeeId,
E.Name 'Employee Name', M.Name 'Manager Name'
FROM dbo.Employee E
INNER JOIN Employee M
ON M.EmployeeId = E.ManagerId
RESULT: