employee table stores a department_id rather than the full department name, because storing the name in every employee row would mean updating hundreds of rows every time a department was renamed. JOINs are the mechanism that bring those separate tables back together when you need to query them — letting you retrieve the employee’s name and the department’s name in a single result set. This chapter covers the two most important join types: INNER JOIN and LEFT JOIN.
Reference Tables
All examples in this chapter use these tables. Department:
Employee:
Ajay has no department assigned (
NULL). His row will behave differently depending on which join type you use — watch for this in the examples below.Why Do We Need JOINs?
Without a JOIN, a query against theemployee table returns only the numeric department_id, not the human-readable department name:
To see the department name alongside each employee, you JOIN the two tables on their shared key column.
JOIN Syntax
JOINnames the second table to include.ONspecifies the matching condition — usually a foreign key in one table equalling a primary key in the other.- Table aliases (like
eandd) are commonly used to keep queries concise.
Practice: Which clause specifies the matching condition?
Practice: Which clause specifies the matching condition?
The
ON clause specifies how two tables should be matched.INNER JOIN
AnINNER JOIN returns only the rows that have a matching value in both tables. If a row in either table has no match in the other, it is excluded from the result entirely.
Example 1: Employee names with department names
Notice that Ajay is not included — his
department_id is NULL, so there is no matching row in the Department table.
Example 2: High-earners with their department
Practice: Employees in the HR department
Practice: Employees in the HR department
Display all employees who work in HR.
Practice: Employees earning more than ₹70,000 with departments
Practice: Employees earning more than ₹70,000 with departments
LEFT JOIN
ALEFT JOIN returns all rows from the left table, plus any matching rows from the right table. Where no match exists in the right table, SQL fills the right table’s columns with NULL.
Example 1: All employees including unassigned ones
Ajay now appears with
NULL for department_name, because LEFT JOIN guarantees every row from the left table (employee) is included.
Example 2: All employees with salary and department
Finding employees with no department
Combining LEFT JOIN with anIS NULL filter lets you find records that have no match in the right table — a common real-world need.
department_id is directly on the employee table:
Practice: Display all employees with department names
Practice: Display all employees with department names
Practice: How does a LEFT JOIN differ from an INNER JOIN?
Practice: How does a LEFT JOIN differ from an INNER JOIN?
- INNER JOIN returns only rows where both tables have a matching key.
- LEFT JOIN returns all rows from the left table, plus matching rows from the right. Where there is no match, the right-table columns contain
NULL.