SQL ServerJOIN Type
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
CROSS JOIN
SELF JOIN
MERGE
PIVOT
Topics cover today
3.
SQL Server JOINS
Summary
Inthis session, you will learn about various SQL Server joins that allow you to combine data from two
tables.
In a relational database, data is distributed in multiple logical tables. To get a complete meaningful set
of data, you need to query data from these tables using joins. SQL Server supports many kinds of joins,
including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, and cross join. Each join type
specifies how SQL Server uses data from one table to select rows in another table.
Let’s set up sample tables for demonstration…
SQL Server JOINS
Settingup sample tables
First, create a new schema named hr:
CREATE SCHEMA hr;
Second, create two new tables named candidates and employees in the hr schema:
CREATE TABLE candidates(
id INT PRIMARY KEY IDENTITY,
fullname VARCHAR(100) NOT NULL
);
CREATE TABLE employees(
id INT PRIMARY KEY IDENTITY,
fullname VARCHAR(100) NOT NULL
);
6.
SQL Server JOINS
Settingup sample tables
Third, insert some rows into the candidates and employees tables:
INSERT INTO hr.candidates(fullname)
VALUES ('John Doe'),
('Lily Bush'),
('Peter Drucker'),
('Jane Doe');
INSERT INTO hr.employees(fullname)
VALUES ('John Doe'),
('Jane Doe'),
('Michael Scott'),
('Jack Sparrow');
Let’s call the candidates table the LEFT table and the employees table the RIGHT table.
7.
SQL Server JOINS
SQLServer INNER JOIN
INNER JOIN produces a data set that includes rows from the left table, matching rows from the right
table.
The following example uses the INNER JOIN clause to get the rows from the candidates table that has
the corresponding rows with the same values in the fullname column of the employees table:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
INNER JOIN hr.employees e ON e.fullname =
c.fullname;
Output
The Venn diagram illustrates the
result of the INNER JOIN of two
result sets.
8.
SQL Server JOINS
SQLServer LEFT JOIN
LEFT JOIN selects data starting from the left table and matching rows in the right table. The LEFT JOIN
returns all rows from the left table and the matching rows from the right table. If a row in the left table
does not have a matching row in the right table, the columns of the right table will have nulls.
The LEFT JOIN is also known as the left outer join. The outer keyword is optional.
The following statement joins the candidates table with the employees table using left join:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
LEFT JOIN hr.employees e ON e.fullname =
c.fullname;
Output
The Venn diagram illustrates the result
of the LEFT JOIN of two result sets.
9.
SQL Server JOINS
SQLServer LEFT JOIN
To get the rows that are available only in the left table but not in the right table, you add
a WHERE clause to the above query:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
LEFT JOIN hr.employees e ON e.fullname =
c.fullname
WHERE e.id IS NULL;
Output
The Venn diagram illustrates the result of the LEFT
JOIN that selects rows available only in the left table.
10.
SQL Server JOINS
SQLServer RIGHT JOIN
The RIGHT JOIN or RIGHT OUTER JOIN selects data starting from the right table. It is a reversed
version of the LEFT JOIN.
The RIGHT JOIN returns a result set that contains all rows from the right table and the matching rows
in the left table. If a row in the right table does not have a matching row in the left table, all columns in
the left table will contain nulls.
The following example uses the right join to query rows from candidates and employees tables:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
RIGHT JOIN hr.employees e ON e.fullname =
c.fullname;
Output
The Venn diagram illustrates the result
of the RIGHT JOIN of two result sets.
11.
SQL Server JOINS
SQLServer RIGHT JOIN
Similarly, you can get rows that are available only in the right table by adding a WHERE clause to the
above query as follows:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
RIGHT JOIN hr.employees e ON e.fullname =
c.fullname
WHERE c.id IS NULL;
Output
The Venn diagram that illustrates the result.
12.
SQL Server JOINS
SQLServer FULL JOIN
The FULL OUTER JOIN or FULL JOIN returns a result set that contains all rows from both left and right
tables, with the matching rows from both sides where available. In case there is no match, the missing
side will have NULL values.
The following example shows how to perform a FULL JOIN between the candidates and employees.
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
FULL JOIN hr.employees e ON e.fullname =
c.fullname;
Output
The Venn diagram illustrates the
result of the FULL JOIN of two
result sets.
13.
SQL Server JOINS
SQLServer FULL JOIN
To select rows that exist either left or right table, you exclude rows that are common to both tables by
adding a WHERE clause as shown in the following query:
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
FULL JOIN hr.employees e ON e.fullname =
c.fullname
WHERE c.id IS NULL OR e.id IS NULL;
Output
The Venn diagram illustrates the
result of the FULL JOIN of two
result sets.
14.
SQL Server JOINS
SQLServer CROSS JOIN
The CROSS JOIN joined every row from the first table (T1) with every row from the second table (T2).
In other words, the cross join returns a Cartesian product of rows from both tables.
Unlike the INNER JOIN or LEFT JOIN, the CROSS JOIN does not establish a relationship between the
joined tables.
Suppose the T1 table contains three rows 1, 2, and 3 and the T2 table contains three rows A, B, and C.
The CROSS JOIN gets a row from the first table (T1) and then creates a new row for every row in the
second table (T2). It then does the same for the next row for in the first table (T1) and so on.
15.
SQL Server JOINS
SQLServer CROSS JOIN
In this illustration, the CROSS JOIN creates nine rows in total. In general, if the first table has n rows
and the second table has m rows, the cross join will result in n x m rows.
16.
SQL Server JOINS
SQLServer CROSS JOIN
The following statement returns the combinations of all products and stores.
SELECT
c.id candidate_id,
c.fullname candidate_name,
e.id employee_id,
e.fullname employee_name
FROM hr.candidates c
CROSS JOIN hr.employees e;
Output
17.
SQL Server JOINS
SQLServer SELF JOIN
A SELF JOIN allows you to join a table to itself. It helps query hierarchical data or compare rows within
the same table.
A SELF JOIN uses the INNER JOIN or LEFT JOIN clause. Because the query that uses the SELF JOIN
references the same table, the table alias is used to assign different names to the same table within
the query.
SELECT L1.L_Name As DistrictName, L2.L_Name
FROM Location L1
INNER JOIN Location L2 ON L1.ParentID =
L2.L_ID
WHERE L1.L_Type = 'District'
Output
18.
SQL Server JOINS
SQLServer MERGE
Suppose, you have two table called source and target tables, and you need to update the target table
based on the values matched from the source table. There are three cases:
1. The source table has some rows that do not exist in the target table. In this case, you need
to INSERT rows that are in the source table into the target table.
2. The target table has some rows that do not exist in the source table. In this case, you need
to DELETE rows from the target table.
3. The source table has some rows with the same keys as the rows in the target table. However,
these rows have different values in the non-key columns. In this case, you need to UPDATE the
rows in the target table with the values coming from the source table.
The following picture illustrates the source and target tables with the corresponding actions: INSERT,
UPDATE, DELETE:
19.
SQL Server JOINS
SQLServer MERGE
If you use the INSERT, UPDATE, and DELETE statement individually, you have to construct three
separate statements to update the data to the target table with the matching rows from the source
table.
However, SQL Server provides the MERGE statement that allows you to perform three actions at the
same time. The following shows the syntax of the MERGE statement:
20.
SQL Server JOINS
SQLServer MERGE
Suppose we have two table category and category_staging that store the sales by product category.
CREATE TABLE category (
category_id INT PRIMARY KEY,
category_name VARCHAR(255) NOT NULL,
amount DECIMAL(10 , 2 )
);
CREATE TABLE category_staging (
category_id INT PRIMARY KEY,
category_name VARCHAR(255) NOT NULL,
amount DECIMAL(10 , 2 )
);
INSERT INTO category(category_id,
category_name, amount)
VALUES(1,'Children Bicycles',15000),
(2,'Comfort Bicycles',25000),
(3,'Cruisers Bicycles',13000),
(4,'Cyclocross Bicycles',10000);
INSERT INTO category_staging(category_id,
category_name, amount)
VALUES(1,'Children Bicycles',15000),
(3,'Cruisers Bicycles',13000),
(4,'Cyclocross Bicycles',20000),
(5,'Electric Bikes',10000),
(6,'Mountain Bikes',10000);
21.
SQL Server JOINS
SQLServer MERGE
To update data to the category (target table) with the values from the category_staging (source table),
you use the following MERGE statement:
MERGE category t
USING category_staging s
ON (s.category_id = t.category_id)
WHEN MATCHED
THEN UPDATE SET
t.category_name = s.category_name,
t.amount = s.amount
WHEN NOT MATCHED BY TARGET
THEN INSERT (category_id, category_name, amount)
VALUES (s.category_id, s.category_name,
s.amount)
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
MERGE target_table USING
source_table
ON merge_condition
WHEN MATCHED
THEN update_statement
WHEN NOT MATCHED
THEN insert_statement
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
SQL Server JOINS
SQLServer MERGE
In this example, we used the values in the category_id columns in both tables as the merge condition.
First, the rows with id 1, 3, 4 from the category_staging table matches with the rows from the
target table, therefore, the MERGE statement updates the values in category name and
amount columns in the category table.
Second, the rows with id 5 and 6 from the category_staging table do not exist in
the category table, so the MERGE statement inserts these rows into the target table.
Third, the row with id 2 from the category table does not exist in the sales_staging table,
therefore, the MERGE statement deletes this row.
As a result of the merger, the data in the category table is fully synchronized with the data in
the category_staging table.
In this tutorial, you have learned how to use the SQL Server MERGE statement to make changes in a
table based on matching values from another table.
24.
SQL Server JOINS
SQLServer PIVOT
SQL Server PIVOT operator rotates a table-valued expression. It turns the unique values in one column
into multiple columns in the output and performs aggregations on any remaining column values.
You follow these steps to make a query a pivot table:
First, select a base dataset for pivoting.
Second, create a temporary result by using a derived table or common table expression (CTE)
Third, apply the PIVOT operator.
Let’s apply these steps in the following example
25.
SQL Server JOINS
SQLServer PIVOT
The following query finds the number of products for each product category:
SELECT
category_name,
COUNT(product_id) product_count
FROM products p
INNER JOIN categories c ON c.category_id =
p.category_id
GROUP BY category_name;
Outp
ut
Our goal is to turn the category names from the first column of the output into multiple columns and
count the number of products for each category name as the following picture:
In addition, we can add the model year to group the category by model year as shown in the following
output:
26.
SQL Server JOINS
SQLServer PIVOT
First, select category name and product id from the products and categories tables as the base data for
pivoting:
SELECT
category_name,
product_id
FROM products p
INNER JOIN categories c ON c.category_id = p.category_id;
Second, create a temporary result set using a derived table:
SELECT * FROM (
SELECT
category_name,
product_id
FROM products p
INNER JOIN categories c ON c.category_id = p.category_id
) t
27.
SQL Server JOINS
SQLServer PIVOT
Third, apply the PIVOT operator:
SELECT * FROM
(
SELECT 'CategoryWiseCount' AS CategoryWiseCount,
category_name,
product_id
FROM products p
INNER JOIN categories c ON c.category_id = p.category_id
) t
PIVOT(
COUNT(product_id)
FOR category_name IN (
[Children Bicycles], [Comfort Bicycles], [Cruisers Bicycles], [Cyclocross
Bicycles],
[Electric Bikes], [Mountain Bikes], [Road Bikes])
) AS pivot_table;
This query generates the following output:
28.
SQL Server JOINS
SQLServer PIVOT
Now, any additional column which you add to the select list of the query that returns the base data will
automatically form row groups in the pivot table. For example, you can add the model year column to
the above query:
SELECT * FROM
(
SELECT category_name, product_id, model_year
FROM products p
INNER JOIN categories c ON c.category_id = p.category_id
) t
PIVOT(
COUNT(product_id)
FOR category_name IN (
[Children Bicycles], [Comfort Bicycles], [Cruisers Bicycles], [Cyclocross
Bicycles],
[Electric Bikes], [Mountain Bikes], [Road Bikes])
) AS pivot_table;
This query generates the following output:
29.
SQL Server JOINS
SQLServer UNPIVOT
SELECT category_name, ProductCount
FROM
(
SELECT
[Children Bicycles], [Comfort Bicycles], [Cruisers Bicycles], [Cyclocross Bicycles]
, [Electric Bikes], [Mountain Bikes], [Road Bikes]
FROM categoriespivot
) p
UNPIVOT
(
ProductCount for category_name IN
([Children Bicycles], [Comfort Bicycles], [Cruisers Bicycles], [Cyclocross Bicycles]
, [Electric Bikes], [Mountain Bikes], [Road Bikes])
) AS upvt;
This query generates the following output: