Sub topic 15
SQL joins
This chapter assumes you know the relational algebra joins (theta, equi, natural, outer) and the SQL basics of SELECT, WHERE and DISTINCT. It shows how each join is written in SQL, what happens to duplicates and NULLs, and which mistakes change a GATE answer.
From algebra to SQL
A join combines rows from two tables. In relational algebra, a theta join is a Cartesian product followed by a selection. SQL gives you the same idea in two syntaxes.
-- Old style: product, then filter in WHERE
SELECT s.sname, e.cid
FROM Student s, Enrolled e
WHERE s.sid = e.sid;
-- Explicit style: INNER JOIN with an ON condition
SELECT s.sname, e.cid
FROM Student s
INNER JOIN Enrolled e ON s.sid = e.sid;Both queries return the same rows. INNER is optional, so JOIN alone means an inner join. Conceptually, the database pairs every Student row with every Enrolled row, then keeps the pairs where the condition is true.
The tables below are used in the rest of the chapter.
| Student sid | sname | age |
|---|---|---|
| 1 | Asha | 19 |
| 2 | Ravi | 21 |
| 3 | Meera | NULL |
| Enrolled sid | cid |
|---|---|
| 1 | 10 |
| 1 | 20 |
| 2 | 10 |
Inner join, natural join and USING
An inner join keeps only the pairs that satisfy the condition. For the tables above, the result has three rows: Asha with 10, Asha with 20, and Ravi with 10. Meera has no enrollment, so she disappears.
SQL has shorthand for equi-joins on same-named columns.
-- NATURAL JOIN: joins on ALL columns with the same name
SELECT * FROM Student NATURAL JOIN Enrolled;
-- USING: joins on the listed columns only
SELECT * FROM Student JOIN Enrolled USING (sid);In both forms the shared column sid appears once in the output. With ON, both s.sid and e.sid appear.
Outer joins
An outer join keeps rows that have no match. The side being preserved gets NULL in the columns of the other table.
SELECT s.sid, s.sname, e.cid
FROM Student s
LEFT OUTER JOIN Enrolled e ON s.sid = e.sid;The result is four rows: (1, Asha, 10), (1, Asha, 20), (2, Ravi, 10) and (3, Meera, NULL).
| Join type | Rows kept |
|---|---|
| INNER JOIN | Only matching pairs |
| LEFT OUTER JOIN | All left rows, plus matching pairs |
| RIGHT OUTER JOIN | All right rows, plus matching pairs |
| FULL OUTER JOIN | All rows from both sides |
OUTER is optional, so LEFT JOIN means LEFT OUTER JOIN. A left outer join of A and B returns the same rows as a right outer join of B and A, with the columns in a different order.
ON versus WHERE in outer joins
For an inner join, a condition in ON and the same condition in WHERE give the same result. For an outer join they do not.
-- Query A: condition in ON
SELECT s.sid, e.cid
FROM Student s
LEFT JOIN Enrolled e ON s.sid = e.sid AND e.cid = 10;
-- Query B: condition in WHERE
SELECT s.sid, e.cid
FROM Student s
LEFT JOIN Enrolled e ON s.sid = e.sid
WHERE e.cid = 10;Query A first decides which Enrolled rows match, then preserves every student. It returns (1, 10), (2, 10) and (3, NULL). Query B builds the full outer result first, then filters. The row for Meera has e.cid = NULL, so the WHERE condition is UNKNOWN and the row is dropped. Query B returns (1, 10) and (2, 10). Putting a filter on the right table in WHERE turns a left outer join into an inner join, unless the test is IS NULL.
Anti-join: finding rows with no match
A common idiom uses a left join and tests for NULL on the right side.
SELECT s.sid, s.sname
FROM Student s
LEFT JOIN Enrolled e ON s.sid = e.sid
WHERE e.sid IS NULL;This returns Meera. It gives the same answer as NOT EXISTS when the tested column cannot be NULL in real matches. Test a column that is never NULL in a matched row, such as the join key or a primary key column.
Self-joins and aliases
To compare rows of the same table, join the table to itself. Aliases are required so the two copies can be told apart.
-- Pairs of different students with the same age
SELECT a.sname, b.sname
FROM Student a
JOIN Student b ON a.age = b.age AND a.sid < b.sid;The condition a.sid < b.sid removes pairs of a row with itself and stops each pair from appearing twice, in both orders.
Duplicates and NULLs in joins
Joins are bag operations. If Student has one row for sid 1 and Enrolled has two rows for sid 1, the join has two rows for that student. Projecting sname alone then gives Asha twice unless you use DISTINCT.
NULL never equals anything, including another NULL. A row with sid = NULL on one side matches nothing in an inner join, even if the other table also has a NULL. In an outer join, such a row is kept and padded with NULLs.
Counting result rows
GATE questions often ask how many rows a join can return. Use these bounds, where R has m rows and S has n rows.
| Query | Minimum | Maximum |
|---|---|---|
| R × S (cross join) | m·n | m·n |
| R inner join S on any condition | 0 | m·n |
| R left outer join S | m | m·n, or more precisely the larger of m and the matched total |
| R join S on R's primary key = S's foreign key to R, S.fk NOT NULL | n | n |
| Same join, but S.fk may be NULL | number of non-NULL fk rows | n |
The fourth row works because each S row matches exactly one R row. The key of R is unique, so no S row can match two R rows, and the foreign key guarantees a match exists.
Exercise
Student has rows (1, Asha), (2, Ravi), (3, Meera). Enrolled(sid, cid) has rows (1, 10), (1, 20), (2, 10), and sid is a foreign key to Student. How many rows does SELECT s.sname FROM Student s LEFT JOIN Enrolled e ON s.sid = e.sid WHERE e.cid <> 10; return, and which rows are they?
Show answer
One row, Asha. The left join produces (Asha,10), (Asha,20), (Ravi,10), (Meera,NULL). The WHERE condition e.cid <> 10 is true only for (Asha,20). For (Meera,NULL) the comparison is UNKNOWN, so the row is dropped, and the other rows have cid 10.
Exercise
R has 5 rows and S has 8 rows. S has a column fk declared NOT NULL as a foreign key to R's primary key. What are the minimum and maximum row counts of R JOIN S ON R.pk = S.fk?
Show answer
Both are 8. Every S row has a non-NULL fk that must match exactly one R row, because pk is unique. So each S row yields exactly one joined row.