Good databases split information across several tables: students in one, departments in another, course results in a third. A JOIN puts them back together in a query by matching related columns, such as a student’s dept code with a department’s code. This lesson covers INNER JOIN, joining three tables, LEFT JOIN, and finding rows that have no match.
In this lesson you will learn
- How a join matches rows with
ON - Table aliases for shorter queries
- Joining three tables through a link table
- The difference between
INNER JOINandLEFT JOIN - How to find rows with no match
The tables
students(id, name, city, dept, cgpa) · departments(code, name, building) with CSE, EEE, BBA and ME · courses(id, title, dept, credits) · enrollments(student_id, course_id, marks), which links students to courses. The ME department has no students, and Nadia has no enrollments.
INNER JOIN
SELECT s.name, d.name AS department, d.building
FROM students s
INNER JOIN departments d ON s.dept = d.code
WHERE s.city = 'Dhaka';
SELECT s.name, c.title, e.marks
FROM enrollments e
JOIN students s ON s.id = e.student_id
JOIN courses c ON c.id = e.course_id
ORDER BY e.marks DESC
LIMIT 4;
Output
+--------+------------------------+----------+
| name | department | building |
+--------+------------------------+----------+
| Nusrat | Computer Science | Block A |
| Tania | Computer Science | Block A |
| Rafi | Electrical Engineering | Block B |
+--------+------------------------+----------+
(3 rows)
+--------+-----------------+-------+
| name | title | marks |
+--------+-----------------+-------+
| Tania | Data Structures | 95 |
| Nusrat | Databases | 92 |
| Tania | Databases | 90 |
| Nusrat | Data Structures | 88 |
+--------+-----------------+-------+
(4 rows)
INNER JOIN departments d ON s.dept = d.codepairs each student with the department whose code matches. PlainJOINmeansINNER JOIN.sanddare aliases. They shorten the query and make it clear which table each column comes from, which matters when both tables have anamecolumn.- The second query walks through the
enrollmentslink table to connect students with courses, a typical many-to-many pattern. - An inner join returns only rows that match on both sides.
LEFT JOIN and Missing Matches
-- Every department, even those with no students
SELECT d.code, COUNT(s.id) AS students
FROM departments d
LEFT JOIN students s ON s.dept = d.code
GROUP BY d.code
ORDER BY d.code;
-- Students who are not enrolled in any course
SELECT s.name
FROM students s
LEFT JOIN enrollments e ON e.student_id = s.id
WHERE e.student_id IS NULL;
Output
+------+----------+
| code | students |
+------+----------+
| BBA | 2 |
| CSE | 4 |
| EEE | 2 |
| ME | 0 |
+------+----------+
(4 rows)
+-------+
| name |
+-------+
| Nadia |
+-------+
(1 row)
LEFT JOINkeeps every row from the left table, even with no match. ME appears with 0 students; an inner join would leave it out.COUNT(s.id)counts only real matches.COUNT(*)would count the empty ME row as 1.- Where nothing matches, the right side’s columns are
NULL. Filtering one.student_id IS NULLtherefore finds students with no enrollments: Nadia.
Join Types
| Join | Returns |
|---|---|
INNER JOIN | Only rows that match in both tables |
LEFT JOIN | All rows from the left table, matched data or NULL |
RIGHT JOIN | All rows from the right table (swap the tables and use LEFT JOIN for the same result) |
FULL OUTER JOIN | All rows from both tables |
CROSS JOIN | Every row paired with every row (rarely what you want) |
RIGHT and FULL joins are supported in SQLite from version 3.39, and in PostgreSQL and SQL Server. MySQL has no FULL OUTER JOIN.
Common Mistakes
- Forgetting the
ONcondition, which pairs every row with every row and multiplies the result. - Ambiguous column names such as
namewhen both tables have one. Prefix with the alias. - Filtering the right table in
WHEREafter aLEFT JOIN, which silently turns it back into an inner join. Put that condition inONinstead.
Practice Exercises
Exercise 1. List each course title with its department’s full name.
Show answer
SELECT c.title, d.name
FROM courses c
JOIN departments d ON d.code = c.dept;
Exercise 2. Show the average marks per student name, for students with enrollments.
Show answer
SELECT s.name, ROUND(AVG(e.marks), 1) AS avg_marks
FROM students s
JOIN enrollments e ON e.student_id = s.id
GROUP BY s.id, s.name;
Frequently Asked Questions
What is the difference between JOIN and INNER JOIN?
None. JOIN on its own means INNER JOIN.
Can I join a table to itself?
Yes, a self join uses two aliases of the same table, for example to pair each employee with their manager stored in the same table.
Summary
- Joins combine tables by matching columns in
ON. INNER JOINkeeps matches only;LEFT JOINkeeps every left row.- Use aliases to keep queries short and clear.
LEFT JOIN ... WHERE right.col IS NULLfinds rows with no match.
Next: SQL Subqueries.
