This lesson on JOINS - FULL, CROSS JOIN is hands-on and example-driven. You will be able to select the appropriate SQL join type—INNER, LEFT, RIGHT, FULL, or CROSS—to accurately combine data from two or more tables. You will understand how primary and foreign keys facilitate these relationships and how to handle non-matching rows using NULL values.
What You'll Be Able To Do
- Define the roles of Primary and Foreign Keys in establishing table relationships.
- Write queries using INNER JOIN to retrieve only matching records between two datasets.
- Apply LEFT JOIN to retain all records from the primary table, filling non-matches with NULLs.
- Distinguish between the vertical stacking operations UNION and UNION ALL.
- Construct a FULL JOIN query to include all rows from both joined tables.
- Explain the result set generated by a CROSS JOIN (Cartesian product).
Topics Covered in JOINS - FULL, CROSS JOIN
- Joins and FROM Clause (0:00 - 0:30) — Joins are SQL instructions placed in the FROM clause used to combine data sets.
- Primary and Foreign Keys (0:30 - 1:15) — A primary key uniquely identifies a row, while a foreign key links to another table's primary key.
- Joining Logic and Risks (1:15 - 2:30) — Tables can be joined on any columns of the same type, but joining without foreign keys is risky.
- INNER JOIN (Default) (2:30 - 4:30) — INNER JOIN combines rows only when there are matching values in the specified common column.
- LEFT JOIN Behavior (4:30 - 5:30) — LEFT JOIN returns all rows from the first table, using NULLs for non-matching values from the right table.
- RIGHT JOIN Behavior (5:30 - 6:45) — RIGHT JOIN returns all rows from the right table, using NULLs for non-matching values from the left table.
- FULL JOIN (OUTER JOIN) (6:45 - 7:45) — FULL JOIN combines all data from both tables, using NULLs where matches do not exist.
- UNION and UNION ALL (7:45 - 8:30) — UNION stacks data vertically and deduplicates, while UNION ALL stacks data vertically and keeps duplicates.
- CROSS JOIN (Cartesian) (8:30 - 9:00) — CROSS JOIN combines every row of the first table with every row of the second table.
SQL Cheat Sheet
-
JOIN or INNER JOIN— Includes only rows with matching values in both tablesSELECT * FROM customer JOIN event ON customer.id = event.customer_id; -
LEFT JOIN— Includes all rows from the left table, matching rows from the rightSELECT * FROM customer LEFT JOIN event ON customer.id = event.customer_id; -
RIGHT JOIN— Includes all rows from the right table, matching rows from the leftSELECT * FROM event RIGHT JOIN action_types ON event.action_id = action_types.id; -
FULL JOIN or OUTER JOIN— Includes all rows from both tables, filling non-matches with NULLSELECT * FROM teacher FULL JOIN student ON teacher.age = student.age; -
UNION— Stacks two result sets vertically, removing duplicate rowsSELECT age FROM teacher UNION SELECT age FROM student; -
UNION ALL— Stacks two result sets vertically, retaining all duplicate rowsSELECT age FROM teacher UNION ALL SELECT age FROM student; -
CROSS JOIN— Combines every row from the first table with every row of the secondSELECT * FROM teacher CROSS JOIN student;
Comparison Table
| Join Type | Matching Requirement | Non-Matching Rows |
|---|---|---|
| INNER JOIN | Match required in both | Excluded |
| LEFT JOIN | Match required in right | Left rows kept, right NULL |
| FULL JOIN | No match required | All rows kept, NULLs used |
Common Pitfalls
- Mistake: Joining tables using columns that are not unique identifiers. Avoid: Always use columns that are unique or foreign keys to prevent duplicate data.
- Mistake: Assuming JOIN means FULL JOIN and expecting all data. Avoid: Use INNER JOIN (or JOIN) only when you want records that exist in both tables.
- Mistake: Joining tables on columns of the same type but unrelated meaning. Avoid: Ensure the columns share a common dimension or logical relationship (e.g., IDs).
FAQs
- Why is RIGHT JOIN rarely used in practice? Any query written using a RIGHT JOIN can be rewritten as a LEFT JOIN, which is the more conventional and widely used standard.
- What happens if I use JOIN without specifying INNER, LEFT, or RIGHT? JOIN is the default syntax and is equivalent to an INNER JOIN, only returning rows with matches in both tables.
- Do the columns used for joining have to be Primary or Foreign Keys? No, they only need to be of the same data type, but using PK/FK ensures logical and accurate relationships.
- How does UNION differ from UNION ALL? UNION automatically removes duplicate rows from the combined result set, while UNION ALL stacks data vertically and retains all rows, including duplicates.