DBMS Notes
A join combines rows from two or more tables based on a related column between them. Joins are the primary mechanism for querying data spread across...
What is a Join?
A join combines rows from two or more tables based on a related column between them. Joins are the primary mechanism for querying data spread across multiple tables in a normalized database.
1. INNER JOIN
Returns only rows where there is a match in both tables.
| EmpID | Name | DeptID | DeptName |
|---|---|---|---|
| 101 | Alice | 1 | Computer Science |
| 102 | Bob | 2 | Mathematics |
| 103 | Carol | 1 | Computer Science |
INNER JOIN Result (matching rows only)
SELECT e.EmpID, e.Name, d.DeptName
FROM Employee e
INNER JOIN Department d ON e.DeptID = d.DeptID;2. LEFT OUTER JOIN (LEFT JOIN)
Returns all rows from the left table, and matching rows from the right table. If no match, NULLs are used for right-table columns.
| EmpID | Name | DeptID | DeptName |
|---|---|---|---|
| 101 | Alice | 1 | Computer Science |
| 102 | Bob | 2 | Mathematics |
| 103 | Carol | 1 | Computer Science |
LEFT JOIN Result
SELECT e.EmpID, e.Name, d.DeptName
FROM Employee e
LEFT JOIN Department d ON e.DeptID = d.DeptID;3. RIGHT OUTER JOIN (RIGHT JOIN)
Returns all rows from the right table, and matching rows from the left table. If no match, NULLs for left-table columns.
| EmpID | Name | DeptID | DeptName |
|---|---|---|---|
| 101 | Alice | 1 | Computer Science |
| 102 | Bob | 2 | Mathematics |
| 103 | Carol | 1 | Computer Science |
RIGHT JOIN Result
SELECT e.EmpID, e.Name, d.DeptName
FROM Employee e
RIGHT JOIN Department d ON e.DeptID = d.DeptID;4. FULL OUTER JOIN
Returns all rows from both tables. NULLs used where there is no match.
| EmpID | Name | DeptID | DeptName |
|---|---|---|---|
| 101 | Alice | 1 | Computer Science |
| 102 | Bob | 2 | Mathematics |
| 103 | Carol | 1 | Computer Science |
| 104 | David | NULL | NULL |
| NULL | NULL | 3 | Physics |
FULL OUTER JOIN Result
-- MySQL workaround (no FULL OUTER JOIN natively)
SELECT e.EmpID, e.Name, d.DeptName FROM Employee e
LEFT JOIN Department d ON e.DeptID = d.DeptID
UNION
SELECT e.EmpID, e.Name, d.DeptName FROM Employee e
RIGHT JOIN Department d ON e.DeptID = d.DeptID;5. CROSS JOIN
Returns the Cartesian product — every combination of rows from both tables.
SELECT e.Name, d.DeptName
FROM Employee e
CROSS JOIN Department d;Useful for: generating combinations, test data, calendar tables.
6. SELF JOIN
Joins a table with itself. Used for hierarchical or comparative queries within the same table.
-- Find employee and their manager (both in Employee table)
SELECT e.Name AS Employee, m.Name AS Manager
FROM Employee e
LEFT JOIN Employee m ON e.ManagerID = m.EmpID;
-- Find pairs of employees in the same department
SELECT A.Name AS Emp1, B.Name AS Emp2, A.DeptID
FROM Employee A
JOIN Employee B ON A.DeptID = B.DeptID AND A.EmpID < B.EmpID;7. NATURAL JOIN
Automatically joins on all columns with the same name in both tables. No ON clause needed.
SELECT * FROM Employee NATURAL JOIN Department;
-- Joins on DeptID (common column name)Use with caution — column name matches may be unintentional.
Join Types — Visual Summary
| INNER JOIN | Matched rows only |
| LEFT JOIN | All left + matched right |
| RIGHT JOIN | All right + matched left |
| FULL OUTER JOIN | All rows from both sides |
| CROSS JOIN | All combinations |
| SELF JOIN | Table joined with itself |
Joining Multiple Tables
SELECT e.Name, d.DeptName, p.ProjectTitle
FROM Employee e
JOIN Department d ON e.DeptID = d.DeptID
JOIN Works_On w ON e.EmpID = w.EmpID
JOIN Project p ON w.ProjID = p.ProjID
WHERE d.DeptName = 'Computer Science'
ORDER BY e.Name;Summary Table
| Join Type | Includes Unmatched Left | Includes Unmatched Right |
|---|---|---|
| INNER | No | No |
| LEFT | Yes | No |
| RIGHT | No | Yes |
| FULL OUTER | Yes | Yes |
| CROSS | N/A (all combinations) | N/A |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Joins in SQL.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Database Management Systems (DBMS) topic.
Search Terms
dbms, database management systems (dbms), unit, joins, sql, joins in sql
Related Database Management Systems (DBMS) Topics