⚡0 XP🔥0

Joins - Combining Tables

Lesson 5 of 10 ⏱ 5 min read ✎ 4 exercises ❓ 5-question quiz ▶ Runs SQL

Combine related tables with INNER JOIN, LEFT JOIN, RIGHT/FULL joins, self joins and cross joins, join three or more tables, and find unmatched rows.

Good database design splits data into several tables (students, departments, courses...) to avoid repetition. To answer real questions - which department is Aarav in? who scored highest in Java Programming? - you join the tables back together using their keys.

INNER JOIN#

INNER JOIN (or just JOIN) pairs rows from two tables where the join condition is true:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

Table aliases make joins shorter and are essential when both tables have a column with the same name (name here):

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
हिंदी में समझें

JOIN दो tables को उनकी common key (जैसे dept_id) से जोड़ता है। students में सिर्फ़ dept_id = 1 लिखा है; department का नाम departments table में है। ON s.dept_id = d.dept_id बताता है कि किस row को किससे जोड़ना है। INNER JOIN में सिर्फ़ वही rows आती हैं जिनका जोड़ीदार दोनों tables में मिले।

Count the result: 29 rows, not 30. Om Malviya has dept_id = NULL, so he has no match and an inner join drops him.

LEFT JOIN - keep everything on the left#

LEFT JOIN returns all rows of the left table; where there is no match, the right side's columns are NULL:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

Finding rows with no match#

A LEFT JOIN followed by WHERE right.key IS NULL finds "orphans" - one of the most useful patterns in SQL:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

Joins with GROUP BY#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

COUNT(s.student_id) (not COUNT(*)) gives 0 for Data Science, because the NULL produced by the left join is not counted.

Joining three or more tables#

enrollments connects students and courses (a many-to-many relationship). Chain joins to build a marksheet:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

(The bracketed query inside is a subquery - next lesson.)

Self join#

A table can be joined with itself. faculty.reports_to points to another row of faculty:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

RIGHT JOIN, FULL OUTER JOIN and CROSS JOIN#

  • RIGHT JOIN - like LEFT JOIN with the tables swapped (keep all rows of the right table). Most people simply swap the tables and use LEFT JOIN.
  • FULL OUTER JOIN - all rows from both sides, matched where possible. Supported in PostgreSQL, SQL Server and SQLite 3.39+; MySQL emulates it with LEFT JOIN ... UNION ... RIGHT JOIN.
  • CROSS JOIN - every combination of rows (a Cartesian product). Useful for generating combinations, dangerous by accident.
▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
Common Mistake

Forgetting the ON condition (or writing FROM a, b without a WHERE link) silently creates a cross join: 30 students × 6 departments = 180 rows of nonsense. If a join returns far more rows than expected, check the join conditions first.

Join types at a glance#

Join Returns
INNER JOIN only matching pairs
LEFT JOIN all left rows + matches (NULL if none)
RIGHT JOIN all right rows + matches
FULL OUTER JOIN all rows from both sides
CROSS JOIN every combination
self join a table joined to itself via aliases

Summary#

  • Join tables on their keys: FROM a JOIN b ON a.key = b.key; use aliases.
  • INNER JOIN keeps matches only; LEFT JOIN keeps all rows of the left table.
  • LEFT JOIN ... WHERE right.key IS NULL finds unmatched rows.
  • Chain joins for many-to-many relationships; self joins handle hierarchies.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Student with department name

Show each student's name and their department name as department (only students who have a department), ordered by department then student name.

Database: college (see the tables in this lesson)
2

Courses with no enrollments

Find the course_id and title of courses that nobody has enrolled in.

Database: college (see the tables in this lesson)
3

Marksheet with three tables

For student 101, show the course title, marks and grade for every course they took, highest marks first.

Database: college (see the tables in this lesson)
4

Faculty and their manager

Show every faculty member's name as faculty and the name of the person they report to as reports_to (NULL for those who report to nobody).

Database: college (see the tables in this lesson)

Quick Quiz

Q1.

INNER JOIN returns...

Q2.

Which join keeps every row of the left table, even without a match?

Q3.

How do you find departments that have NO students?

Q4.

A self join is...

Q5.

If table A has 5 rows and B has 4, a CROSS JOIN returns...