Joins-SQL
Joins-SQL
INNER JOIN
The INNER JOIN keyword return rows when there is at least one match in both tables.
Customers Table:
Orders Table:
SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
INNER JOIN
Orders O
ON
C.CustomerId = O.CustomerId
The INNER JOIN keyword return rows when there is at least one match in both tables. If there are
rows in "Customers" that do not have matches in "Orders", those rows will NOT be listed.
Lecturer : Damien Kettle Module : Database Administration
LEFT JOIN
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there
are no matches in the right table (table_name2).
SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
LEFT JOIN
Orders O
ON
C.CustomerId = O.CustomerId
The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no
matches in the right table (Orders).
Lecturer : Damien Kettle Module : Database Administration
RIGHT JOIN
The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if
there are no matches in the left table (table_name1).
SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
RIGHT JOIN
Orders O
ON
C.CustomerId = O.CustomerId
FULL JOIN
A FULL OUTER JOIN is neither "left" nor "right"— it's both! It includes all the rows from both of the
tables or result sets participating in the JOIN. When no matching rows exist for rows on the "left"
side of the JOIN, you see Null values from the result set on the "right." Conversely, when no
matching rows exist for rows on the "right" side of the JOIN, you see Null values from the result set
on the "left."
SELECT C.FirstName,
C.LastName,
O.OrderId,
O.Product
FROM
Customers C
FULL OUTER JOIN
Orders O
ON
C.CustomerId = O.CustomerId