Reading history

SQL JOINs: INNER JOIN, LEFT JOIN and More

4 min read 2 views

Learn SQL joins: INNER JOIN and LEFT JOIN with ON, table aliases, joining three tables, finding unmatched rows, and all join types.

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 JOIN and LEFT 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.code pairs each student with the department whose code matches. Plain JOIN means INNER JOIN.
  • s and d are aliases. They shorten the query and make it clear which table each column comes from, which matters when both tables have a name column.
  • The second query walks through the enrollments link 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 JOIN keeps 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 on e.student_id IS NULL therefore finds students with no enrollments: Nadia.

Join Types

JoinReturns
INNER JOINOnly rows that match in both tables
LEFT JOINAll rows from the left table, matched data or NULL
RIGHT JOINAll rows from the right table (swap the tables and use LEFT JOIN for the same result)
FULL OUTER JOINAll rows from both tables
CROSS JOINEvery 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

  1. Forgetting the ON condition, which pairs every row with every row and multiplies the result.
  2. Ambiguous column names such as name when both tables have one. Prefix with the alias.
  3. Filtering the right table in WHERE after a LEFT JOIN, which silently turns it back into an inner join. Put that condition in ON instead.

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 JOIN keeps matches only; LEFT JOIN keeps every left row.
  • Use aliases to keep queries short and clear.
  • LEFT JOIN ... WHERE right.col IS NULL finds rows with no match.

Next: SQL Subqueries.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *