SQL Joins
INNER, LEFT, RIGHT, FULL, CROSS and self joins: which rows each keeps, where NULLs appear, and the traps that change the row count.
Overview
A join combines rows from two tables into one row whenever a condition, written after ON, is true. Nearly every real query touches more than one table, so interviewers use joins to see whether you can predict exactly which rows come back.
The two questions to ask about every join are: which rows survive when there is no match, and how many rows come back when there are several matches.
The sample tables
The data has four employees and three departments. Fay has no department, and Legal has no employees. Those two rows are what make the join types differ. All the SQL on this page runs on PostgreSQL.
Interview questions
- What is the difference between a condition in ON and in WHERE for a LEFT JOIN?
- How do you find records that exist in one table but not in another?
- What join algorithms does a database use, and when does it pick each?
Sign up free to read the full topic
More Databases & SQL topics
Start free on Cariara · Coding problems · System design studio