SQL Joins Introduction
SQL

SQL Joins Introduction — How to Combine Data from Multiple Tables

CHAPTER 20 of 30 SQL Tutorial — Free Course on Neotech Navigators
Real databases store data across multiple related tables instead of one giant table. SQL JOINs let you combine rows from two or more tables based on a related column between them. In this chapter, you will learn why JOINs exist, the four main types, and how to write your first JOIN query using table aliases.

Why Do We Need JOINs?

Imagine a company database with two tables: Employees stores employee information, and Departments stores department details. Instead of repeating department names in every employee row, we store a DeptID that links to the Departments table. This is called a foreign key relationship.

TABLE: Employees
+----+-----------+--------+--------+
| ID | Name      | DeptID | Salary |
+----+-----------+--------+--------+
| 1  | Priya K.  | 101    | 55000  |
| 2  | John D.   | 102    | 62000  |
| 3  | Arun M.   | 101    | 78000  |
| 4  | Emma W.   | 103    | 51000  |
| 5  | Ravi S.   | NULL   | 72000  |
+----+-----------+--------+--------+
TABLE: Departments
+-----+----------------+-----------+
| ID  | DeptName       | Location  |
+-----+----------------+-----------+
| 101 | IT             | Delhi     |
| 102 | Marketing      | London    |
| 103 | Sales          | Mumbai    |
| 104 | HR             | New York  |
+-----+----------------+-----------+

Notice that Employees.DeptID matches Departments.ID. This is the link between the two tables. JOINs use this link to combine the data.

The Four Types of SQL JOINs

JOIN Type What It Returns
INNER JOIN Only rows that have matching values in both tables
LEFT JOIN All rows from the left table + matching rows from the right (NULL if no match)
RIGHT JOIN All rows from the right table + matching rows from the left (NULL if no match)
FULL JOIN All rows from both tables (NULL where there is no match on either side)

INNER JOIN — Only Matching Rows

INNER JOIN returns only the rows where there is a match in both tables:

SQL
SELECT e.Name, d.DeptName, e.Salary
FROM Employees AS e
INNER JOIN Departments AS d
  ON e.DeptID = d.ID;
✓ OUTPUT
+-----------+-----------+--------+
| Name      | DeptName  | Salary |
+-----------+-----------+--------+
| Priya K.  | IT        | 55000  |
| John D.   | Marketing | 62000  |
| Arun M.   | IT        | 78000  |
| Emma W.   | Sales     | 51000  |
+-----------+-----------+--------+

Ravi S. is excluded because his DeptID is NULL (no matching department). HR is excluded because no employee belongs to department 104.

How the JOIN Works Step by Step

The database processes this query in these steps:

  1. FROM — Start with the Employees table (left table)
  2. JOIN — Pair it with the Departments table (right table)
  3. ON — Match rows where e.DeptID = d.ID
  4. SELECT — Return the requested columns from matched rows

LEFT JOIN — All Left Rows + Matches

LEFT JOIN returns all rows from the left table and the matching rows from the right table. If there is no match, the right columns are filled with NULL:

SQL
SELECT e.Name, d.DeptName, e.Salary
FROM Employees AS e
LEFT JOIN Departments AS d
  ON e.DeptID = d.ID;
✓ OUTPUT
+-----------+-----------+--------+
| Name      | DeptName  | Salary |
+-----------+-----------+--------+
| Priya K.  | IT        | 55000  |
| John D.   | Marketing | 62000  |
| Arun M.   | IT        | 78000  |
| Emma W.   | Sales     | 51000  |
| Ravi S.   | NULL      | 72000  |
+-----------+-----------+--------+

Ravi S. now appears with NULL for DeptName because LEFT JOIN keeps all employees regardless of whether they have a department.

RIGHT JOIN — All Right Rows + Matches

RIGHT JOIN returns all rows from the right table and matching rows from the left:

SQL
SELECT e.Name, d.DeptName, d.Location
FROM Employees AS e
RIGHT JOIN Departments AS d
  ON e.DeptID = d.ID;
✓ OUTPUT
+-----------+-----------+-----------+
| Name      | DeptName  | Location  |
+-----------+-----------+-----------+
| Priya K.  | IT        | Delhi     |
| Arun M.   | IT        | Delhi     |
| John D.   | Marketing | London    |
| Emma W.   | Sales     | Mumbai    |
| NULL      | HR        | New York  |
+-----------+-----------+-----------+

The HR department appears with NULL for Name because no employee is assigned to it.

FULL OUTER JOIN — All Rows from Both Tables

FULL OUTER JOIN returns all rows from both tables, with NULL where there is no match:

SQL
SELECT e.Name, d.DeptName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
  ON e.DeptID = d.ID;
✓ OUTPUT
+-----------+-----------+
| Name      | DeptName  |
+-----------+-----------+
| Priya K.  | IT        |
| John D.   | Marketing |
| Arun M.   | IT        |
| Emma W.   | Sales     |
| Ravi S.   | NULL      |
| NULL      | HR        |
+-----------+-----------+

Both Ravi (no department) and HR (no employees) appear in the output.

⚠️ MySQL Does Not Support FULL OUTER JOIN

MySQL does not have FULL OUTER JOIN. You can simulate it by combining a LEFT JOIN and a RIGHT JOIN using UNION. PostgreSQL, SQL Server, and Oracle all support FULL OUTER JOIN natively.

JOIN Comparison Summary

JOIN Type Left Table Rows Right Table Rows Unmatched Rows
INNER JOIN Only matched Only matched Excluded
LEFT JOIN All rows Only matched Right columns = NULL
RIGHT JOIN Only matched All rows Left columns = NULL
FULL OUTER JOIN All rows All rows NULL on missing side

💡 Which JOIN Should I Use?

Use INNER JOIN when you only want rows with matches in both tables (the most common case). Use LEFT JOIN when you need all rows from the main table even if some have no related data. LEFT JOIN is the second most common type. RIGHT JOIN and FULL JOIN are used less frequently.

For a complete reference of JOIN syntax, see the official MySQL JOIN documentation. In the next chapter, you will practice SQL INNER JOIN in depth with more examples.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a LEFT JOIN query that shows all employees with their department name and location. Employees without a department should still appear.

ANSWER
SELECT e.Name,
       d.DeptName,
       d.Location,
       e.Salary
FROM Employees AS e
LEFT JOIN Departments AS d
  ON e.DeptID = d.ID;

Expected Output:

+-----------+-----------+-----------+--------+
| Name      | DeptName  | Location  | Salary |
+-----------+-----------+-----------+--------+
| Priya K.  | IT        | Delhi     | 55000  |
| John D.   | Marketing | London    | 62000  |
| Arun M.   | IT        | Delhi     | 78000  |
| Emma W.   | Sales     | Mumbai    | 51000  |
| Ravi S.   | NULL      | NULL      | 72000  |
+-----------+-----------+-----------+--------+

📝 What You Learned in This Chapter

  • JOINs combine rows from two or more tables using a related column
  • INNER JOIN returns only rows with matches in both tables
  • LEFT JOIN returns all left-table rows + matches from the right
  • RIGHT JOIN returns all right-table rows + matches from the left
  • FULL OUTER JOIN returns all rows from both tables
  • The ON clause specifies the matching condition between tables
  • Table aliases (AS) make JOIN queries shorter and more readable

Frequently Asked Questions

What is a SQL JOIN?
A SQL JOIN combines rows from two or more tables based on a related column between them. For example, you can join an Employees table with a Departments table using a department ID that exists in both tables. JOINs are fundamental to working with relational databases.
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows that have a match in both tables. Unmatched rows are excluded entirely. LEFT JOIN returns all rows from the left table even if there is no match in the right table — unmatched right columns are filled with NULL.
Which JOIN type is most commonly used?
INNER JOIN is the most common because you usually want only the rows with matching data in both tables. LEFT JOIN is the second most common, used when you need to keep all rows from the main table even if some have no related data.
Can I JOIN more than two tables?
Yes. You can chain multiple JOINs in one query. For example: FROM Employees JOIN Departments ON ... JOIN Locations ON .... Each JOIN adds another table to the result. There is no practical limit to the number of tables you can join.
What does the ON clause do in a JOIN?
The ON clause specifies the matching condition between the two tables. It tells the database which columns to compare when combining rows. For example, ON e.DeptID = d.ID matches employees to departments where the employee’s DeptID equals the department’s ID.

📖 Chapter 20 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com