Course

SQL Joins: Syntax, Usage, and Examples

SQL joins allow you to retrieve related data from multiple tables in a relational database. They help establish relationships between records by matching values in common columns. Understanding joins is essential for handling structured data efficiently.

How to Use SQL Joins

SQL provides different types of joins, each designed to serve a specific purpose:

  • INNER JOIN – Retrieves only matching records from both tables.
  • LEFT JOIN (LEFT OUTER JOIN) – Returns all rows from the left table and only matching ones from the right.
  • RIGHT JOIN (RIGHT OUTER JOIN) – Returns all rows from the right table and only matching ones from the left.
  • FULL OUTER JOIN – Combines the results of left and right joins, returning all records.
  • CROSS JOIN – Forms a Cartesian product where each row from the first table pairs with every row from the second.
  • SELF JOIN – Joins a table to itself to compare related data within the same dataset.

Basic SQL Join Syntax

Each type of join follows a similar syntax but differs in how it retrieves data:

sql
SELECT table1.column1, table2.column2 FROM table1 [JOIN TYPE] table2 ON table1.common_column = table2.common_column;

Replace [JOIN TYPE] with INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, or another applicable join type.

When to Use SQL Joins

1. Combining Related Data

In relational databases, related data often exists in separate tables. Joins let you retrieve information efficiently without duplicating records.

Example: A business has customers and orders stored in different tables. Using a join, you can fetch customer names alongside their orders.

sql
SELECT customers.name, orders.order_date FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id;

2. Retrieving All Records from One Table with Matching Data

A LEFT JOIN is useful when you need all records from one table, regardless of whether a matching record exists in the second table.

sql
SELECT employees.name, projects.project_name FROM employees LEFT JOIN projects ON employees.employee_id = projects.employee_id;

Even if an employee isn’t assigned to a project, their name will still appear.

3. Handling Partial Matches Between Tables

A FULL OUTER JOIN is helpful when you need to retrieve all records from both tables, filling missing data with NULL values.

sql
SELECT students.name, courses.course_name FROM students FULL OUTER JOIN enrollments ON students.student_id = enrollments.student_id FULL OUTER JOIN courses ON enrollments.course_id = courses.course_id;

Examples of SQL Joins in Action

1. INNER JOIN Example

An INNER JOIN filters out records that don’t have a corresponding match.

sql
SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;

This query only returns employees assigned to a department.

2. LEFT JOIN Example

A LEFT JOIN retains all records from the first table and includes matching data from the second table when available.

sql
SELECT customers.name, orders.order_date FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Customers who haven’t placed orders will still appear in the results, but with NULL values in the order_date column.

3. RIGHT JOIN Example

A RIGHT JOIN is useful when you want to include all records from the second table, even if there’s no corresponding record in the first.

sql
SELECT suppliers.supplier_name, products.product_name FROM products RIGHT JOIN suppliers ON products.supplier_id = suppliers.supplier_id;

All suppliers are listed, even if they don’t supply any products.

4. FULL OUTER JOIN Example

This type of join combines both LEFT JOIN and RIGHT JOIN logic, retrieving all records from both tables.

sql
SELECT employees.name, departments.department_name FROM employees FULL OUTER JOIN departments ON employees.department_id = departments.department_id;

If an employee isn’t assigned a department, or if a department has no employees, they still appear in the results.

5. CROSS JOIN Example

A CROSS JOIN produces every possible combination of rows from both tables.

sql
SELECT products.product_name, categories.category_name FROM products CROSS JOIN categories;

If there are 5 products and 3 categories, the result contains 15 rows.

6. SELF JOIN Example

A SELF JOIN compares rows within the same table.

sql
SELECT e1.name AS employee, e2.name AS manager FROM employees e1 INNER JOIN employees e2 ON e1.manager_id = e2.employee_id;

This retrieves employees along with their respective managers.

Learn More About SQL Joins

1. SQL Join Types at a Glance

  • INNER JOIN – Matches records between both tables.
  • LEFT JOIN – Returns all records from the left table and matching ones from the right.
  • RIGHT JOIN – Returns all records from the right table and matching ones from the left.
  • FULL OUTER JOIN – Returns all records from both tables, filling missing data with NULL.
  • CROSS JOIN – Combines every row from one table with every row from another.
  • SELF JOIN – Compares rows within the same table.

2. SQL Table Join vs. Equal Comparison

Joins match records across tables, while an equal comparison (=) filters records within a single table.

Using a Join

sql
SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;

Using an Equal Comparison Without a Join

sql
SELECT employees.name, employees.department_id FROM employees WHERE employees.department_id = 5;

3. Inner Join vs. Outer Join in SQL

An INNER JOIN returns only matching records, while an OUTER JOIN includes unmatched records and fills missing values with NULL.

INNER JOIN Example

sql
SELECT customers.name, orders.order_date FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id;

LEFT OUTER JOIN Example

sql
SELECT customers.name, orders.order_date FROM customers LEFT OUTER JOIN orders ON customers.customer_id = orders.customer_id;

4. The Join Statement in SQL

Joins combine records from different tables using a common key.

sql
SELECT columns FROM table1 JOIN table2 ON table1.common_column = table2.common_column;

5. Outer Joins in SQL

Outer joins retrieve unmatched records along with matched ones.

sql
SELECT employees.name, projects.project_name FROM employees LEFT OUTER JOIN projects ON employees.employee_id = projects.employee_id;

6. Self Join in SQL

A SELF JOIN is useful for hierarchical structures like company reporting chains.

sql
SELECT emp1.name AS Employee, emp2.name AS Manager FROM employees emp1 JOIN employees emp2 ON emp1.manager_id = emp2.employee_id;