- Define SQL joins.
- Explain what SQL joins and how to use them.
- Use the different type of joins to display records.
- Shopping datbase example
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN returns all rows from multiple tables where the join condition is met.
For the next 5 minutes, research SQL joins are
Now, turn & talk to your neighbor and discuss:
- At a high level, what are sql joins and how might they be useful?
- How many differnt join statements are there?
Before we continue with examples, we will list the types of the different SQL JOINs you can use:
-
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
-
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
-
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
-
FULL JOIN: Return all rows when there is a match in ONE of the tables
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2). The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins. The FULL OUTER JOIN keyword returns all the rows from the left table (Customers), and all the rows from the right table (Orders). If there are rows in "Customers" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;