The SQL JOIN clause is used to combine data from two or more tables based on a related column.
In real-world databases, information is usually stored across multiple tables instead of one large table. SQL JOINs help retrieve related data efficiently. SQL JOINS practice questions with solutions help to understand the concepts.
For example:
- Student details are stored in a students table.
- Course details are stored in a courses table.
Using a JOIN, you can display the student’s name along with the course name in a single query.
Why Do We Use SQL JOIN?
SQL JOIN is used to:
- Combine related tables
- Reduce data duplication
- Generate business reports
- Retrieve meaningful information
- Build dashboards
- Analyze data across multiple tables
Types of SQL JOIN
| JOIN Type | Description |
|---|---|
| INNER JOIN | Returns matching records from both tables |
| LEFT JOIN | Returns all records from the left table and matching records from the right table |
| RIGHT JOIN | Returns all records from the right table and matching records from the left table |
| FULL OUTER JOIN | Returns all matching and non-matching records from both tables |
| CROSS JOIN | Returns every possible combination of rows |
| SELF JOIN | Joins a table with itself |
Note: MySQL supports
INNER JOIN,LEFT JOIN,RIGHT JOIN,CROSS JOIN, andSELF JOIN.FULL OUTER JOINis typically simulated usingUNION.
Sample Tables Used in This Chapter
students
| student_id | student_name | course_id |
|---|---|---|
| 101 | Rahul | 1 |
| 102 | Amit | 2 |
| 103 | Neha | 3 |
| 104 | Priya | 1 |
| 105 | Rohit | 2 |
| 106 | Ankit | 1 |
| 107 | Sneha | 3 |
courses
| course_id | course_name |
|---|---|
| 1 | Python |
| 2 | Java |
| 3 | SQL |
| 4 | Data Analytics |
1. SQL INNER JOIN to Display Student Names with Their Course Names
Problem Statement
A training institute stores student information and course information in separate tables.
Write an SQL query to display each student’s name along with the course they are enrolled in.
SQL Solution
SELECT
students.student_name,
courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.course_id;
Sample Output
| student_name | course_name |
|---|---|
| Rahul | Python |
| Amit | Java |
| Neha | SQL |
| Priya | Python |
| Rohit | Java |
| Ankit | Python |
| Sneha | SQL |
Explanation
The INNER JOIN matches rows where the course_id exists in both tables.
Only matching records are returned.
Since every student has a valid course ID, all students appear in the result.
Concepts Covered
- INNER JOIN
- ON Clause
- Matching Records
2. SQL INNER JOIN to Display Employee Names with Department Names
Problem Statement
A company stores employee details and department details in separate tables.
Write an SQL query to display employee names along with their department names.
Sample Tables
employees
| employee_id | employee_name | department_id |
|---|---|---|
| 1 | Aman | 101 |
| 2 | Riya | 102 |
| 3 | Vikas | 103 |
| 4 | Neha | 101 |
departments
| department_id | department_name |
|---|---|
| 101 | HR |
| 102 | IT |
| 103 | Finance |
SQL Solution
SELECT
employees.employee_name,
departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Sample Output
| employee_name | department_name |
|---|---|
| Aman | HR |
| Riya | IT |
| Vikas | Finance |
| Neha | HR |
Explanation
The INNER JOIN matches employees with their respective departments using the department_id.
Only employees having a matching department are displayed.
Concepts Covered
- INNER JOIN
- Multiple Tables
- Primary Key
- Foreign Key
3. SQL INNER JOIN to Display Customer Names with Their Orders
Problem Statement
An online shopping website stores customer details and order details in separate tables.
Write an SQL query to display customer names along with their order IDs.
Sample Tables
customers
| customer_id | customer_name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
| 3 | Amit |
orders
| order_id | customer_id | amount |
|---|---|---|
| 5001 | 1 | 1200 |
| 5002 | 2 | 2400 |
| 5003 | 1 | 800 |
SQL Solution
SELECT
customers.customer_name,
orders.order_id,
orders.amount
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
Sample Output
| customer_name | order_id | amount |
|---|---|---|
| Rahul | 5001 | 1200 |
| Neha | 5002 | 2400 |
| Rahul | 5003 | 800 |
Explanation
The query joins customers with their orders using customer_id.
Since Rahul placed two orders, his name appears twice—once for each order.
This behavior is expected in one-to-many relationships.
Concepts Covered
- INNER JOIN
- One-to-Many Relationship
- Business Reporting
4. SQL INNER JOIN to Display Product Names with Category Names
Problem Statement
An e-commerce company stores product information and category information in separate tables.
Write an SQL query to display each product along with its category name.
Sample Tables
products
| product_id | product_name | category_id | price |
|---|---|---|---|
| 101 | Laptop | 1 | 65000 |
| 102 | Keyboard | 2 | 1200 |
| 103 | Mouse | 2 | 700 |
| 104 | Monitor | 3 | 15000 |
| 105 | Headphones | 2 | 2500 |
categories
| category_id | category_name |
|---|---|
| 1 | Computers |
| 2 | Accessories |
| 3 | Monitors |
SQL Solution
SELECT
products.product_name,
categories.category_name,
products.price
FROM products
INNER JOIN categories
ON products.category_id = categories.category_id;
Sample Output
| product_name | category_name | price |
|---|---|---|
| Laptop | Computers | 65000 |
| Keyboard | Accessories | 1200 |
| Mouse | Accessories | 700 |
| Monitor | Monitors | 15000 |
| Headphones | Accessories | 2500 |
Explanation
The INNER JOIN connects the products table with the categories table using the category_id.
Each product is displayed together with the name of its category instead of just the category ID.
This is commonly used in:
- E-commerce websites
- Inventory management
- Product catalogs
- Shopping applications
Concepts Covered
- INNER JOIN
- Foreign Key
- Product Database
5. SQL INNER JOIN to Display Book Titles with Author Names
Problem Statement
A library stores books and authors in separate tables.
Write an SQL query to display every book along with its author’s name.
Sample Tables
books
| book_id | book_title | author_id |
|---|---|---|
| 201 | Python Basics | 1 |
| 202 | Mastering SQL | 2 |
| 203 | Java Programming | 3 |
| 204 | Data Analytics Guide | 2 |
authors
| author_id | author_name |
|---|---|
| 1 | Ravi Kumar |
| 2 | Anjali Sharma |
| 3 | Mohit Verma |
SQL Solution
SELECT
books.book_title,
authors.author_name
FROM books
INNER JOIN authors
ON books.author_id = authors.author_id;
Sample Output
| book_title | author_name |
|---|---|
| Python Basics | Ravi Kumar |
| Mastering SQL | Anjali Sharma |
| Java Programming | Mohit Verma |
| Data Analytics Guide | Anjali Sharma |
Explanation
The INNER JOIN matches each book with its corresponding author using the author_id.
Since one author can write multiple books, the author’s name appears for each matching book.
This relationship is known as a one-to-many relationship.
Concepts Covered
- INNER JOIN
- One-to-Many Relationship
- Library Database
6. SQL LEFT JOIN to Display All Customers Including Those Without Orders
Problem Statement
An online shopping website wants to display all customers, even if some customers have not placed any orders yet.
Write an SQL query to display customer names along with their order IDs.
Sample Tables
customers
| customer_id | customer_name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
| 3 | Amit |
| 4 | Priya |
orders
| order_id | customer_id | amount |
|---|---|---|
| 5001 | 1 | 1200 |
| 5002 | 2 | 2500 |
| 5003 | 1 | 900 |
SQL Solution
SELECT
customers.customer_name,
orders.order_id,
orders.amount
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;
Sample Output
| customer_name | order_id | amount |
|---|---|---|
| Rahul | 5001 | 1200 |
| Rahul | 5003 | 900 |
| Neha | 5002 | 2500 |
| Amit | NULL | NULL |
| Priya | NULL | NULL |
Explanation
A LEFT JOIN returns all records from the left table (customers).
If a customer has no matching order, SQL returns NULL for the order-related columns.
This is commonly used for:
- Customer Reports
- CRM Dashboards
- Marketing Campaigns
- Customer Activity Analysis
Concepts Covered
- LEFT JOIN
- NULL Values
- One-to-Many Relationship
7. SQL LEFT JOIN to Display All Employees Including Those Without Departments
Problem Statement
An HR manager wants to display all employees, including employees who are not assigned to any department.
Write an SQL query to display employee names with their department names.
Sample Tables
employees
| employee_id | employee_name | department_id |
|---|---|---|
| 1 | Aman | 101 |
| 2 | Riya | 102 |
| 3 | Vikas | NULL |
| 4 | Neha | 101 |
departments
| department_id | department_name |
|---|---|
| 101 | HR |
| 102 | IT |
SQL Solution
SELECT
employees.employee_name,
departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Sample Output
| employee_name | department_name |
|---|---|
| Aman | HR |
| Riya | IT |
| Vikas | NULL |
| Neha | HR |
Explanation
Every employee is displayed because the employees table is on the left side of the join.
Since Vikas is not assigned to any department, the department name appears as NULL.
Concepts Covered
- LEFT JOIN
- NULL
- Employee Database
8. SQL LEFT JOIN to Display All Students Including Those Without Projects
Problem Statement
A college wants to display all students, including students who have not been assigned any project.
Write an SQL query to display student names with their project titles.
Sample Tables
students
| student_id | student_name |
|---|---|
| 101 | Rahul |
| 102 | Neha |
| 103 | Amit |
| 104 | Sneha |
projects
| project_id | student_id | project_title |
|---|---|---|
| 1 | 101 | Library Management System |
| 2 | 102 | Hospital Management |
| 3 | 104 | E-Commerce Website |
SQL Solution
SELECT
students.student_name,
projects.project_title
FROM students
LEFT JOIN projects
ON students.student_id = projects.student_id;
Sample Output
| student_name | project_title |
|---|---|
| Rahul | Library Management System |
| Neha | Hospital Management |
| Amit | NULL |
| Sneha | E-Commerce Website |
Explanation
The query displays every student.
Students who do not have a project assigned receive NULL in the project_title column.
This report helps identify students who still need project allocation.
Concepts Covered
- LEFT JOIN
- NULL
- Student Management
9. SQL LEFT JOIN to Display All Products Including Uncategorized Products
Problem Statement
An online store wants to display all products, including products that have not been assigned to any category.
Write an SQL query to display product names with category names.
Sample Tables
products
| product_id | product_name | category_id |
|---|---|---|
| 101 | Laptop | 1 |
| 102 | Mouse | 2 |
| 103 | Keyboard | NULL |
| 104 | Monitor | 3 |
categories
| category_id | category_name |
|---|---|
| 1 | Computers |
| 2 | Accessories |
| 3 | Monitors |
SQL Solution
SELECT
products.product_name,
categories.category_name
FROM products
LEFT JOIN categories
ON products.category_id = categories.category_id;
Sample Output
| product_name | category_name |
|---|---|
| Laptop | Computers |
| Mouse | Accessories |
| Keyboard | NULL |
| Monitor | Monitors |
Explanation
Every product appears in the result.
The Keyboard has no category assigned, so SQL displays NULL.
This helps store administrators identify products requiring categorization.
Concepts Covered
- LEFT JOIN
- NULL
- Product Database
10. SQL LEFT JOIN to Display All Teachers Including Those Without Subjects
Problem Statement
A school wants to display all teachers, even if they have not yet been assigned any subject.
Write an SQL query to display teacher names with their subject names.
Sample Tables
teachers
| teacher_id | teacher_name |
|---|---|
| 1 | Anita |
| 2 | Rakesh |
| 3 | Suman |
| 4 | Karan |
subjects
| subject_id | teacher_id | subject_name |
|---|---|---|
| 101 | 1 | Mathematics |
| 102 | 2 | Science |
| 103 | 4 | English |
SQL Solution
SELECT
teachers.teacher_name,
subjects.subject_name
FROM teachers
LEFT JOIN subjects
ON teachers.teacher_id = subjects.teacher_id;
Sample Output
| teacher_name | subject_name |
|---|---|
| Anita | Mathematics |
| Rakesh | Science |
| Suman | NULL |
| Karan | English |
Explanation
The query returns every teacher.
Teachers without an assigned subject receive NULL in the subject_name column.
This report helps school administrators identify unassigned teachers.
Concepts Covered
- LEFT JOIN
- NULL Values
- School Database
11. SQL RIGHT JOIN to Display All Departments Including Empty Departments
Problem Statement
A company wants to display all departments, including departments that currently have no employees.
Write an SQL query to display department names along with employee names.
Sample Tables
employees
| employee_id | employee_name | department_id |
|---|---|---|
| 1 | Aman | 101 |
| 2 | Riya | 102 |
| 3 | Neha | 101 |
departments
| department_id | department_name |
|---|---|
| 101 | HR |
| 102 | IT |
| 103 | Finance |
| 104 | Marketing |
SQL Solution
SELECT
employees.employee_name,
departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
Sample Output
| employee_name | department_name |
|---|---|
| Aman | HR |
| Neha | HR |
| Riya | IT |
| NULL | Finance |
| NULL | Marketing |
Explanation
A RIGHT JOIN returns all records from the right table (departments).
Departments without employees still appear, while employee columns become NULL.
This report is useful for:
- HR Analytics
- Department Audits
- Organization Planning
Concepts Covered
- RIGHT JOIN
- NULL Values
- Department Reports
12. SQL RIGHT JOIN to Display All Categories Including Empty Categories
Problem Statement
An e-commerce website wants to display every product category, including categories that currently contain no products.
Sample Tables
products
| product_id | product_name | category_id |
|---|---|---|
| 101 | Laptop | 1 |
| 102 | Mouse | 2 |
| 103 | Monitor | 3 |
categories
| category_id | category_name |
|---|---|
| 1 | Computers |
| 2 | Accessories |
| 3 | Monitors |
| 4 | Gaming |
SQL Solution
SELECT
products.product_name,
categories.category_name
FROM products
RIGHT JOIN categories
ON products.category_id = categories.category_id;
Sample Output
| product_name | category_name |
|---|---|
| Laptop | Computers |
| Mouse | Accessories |
| Monitor | Monitors |
| NULL | Gaming |
Explanation
Every category appears in the result.
Since no product belongs to the Gaming category, SQL returns NULL for the product name.
Concepts Covered
- RIGHT JOIN
- Product Database
- NULL Values
13. SQL CROSS JOIN to Generate Every Student-Course Combination
Problem Statement
A training institute wants to generate every possible combination of students and available courses for batch planning.
Sample Tables
students
| student_id | student_name |
|---|---|
| 101 | Rahul |
| 102 | Neha |
| 103 | Amit |
courses
| course_id | course_name |
|---|---|
| 1 | Python |
| 2 | Java |
SQL Solution
SELECT
students.student_name,
courses.course_name
FROM students
CROSS JOIN courses;
Sample Output
| student_name | course_name |
|---|---|
| Rahul | Python |
| Rahul | Java |
| Neha | Python |
| Neha | Java |
| Amit | Python |
| Amit | Java |
Explanation
A CROSS JOIN creates the Cartesian Product of two tables.
Each student is paired with every available course.
This type of join is commonly used for:
- Scheduling
- Simulation
- Test Data Generation
- Planning Systems
Concepts Covered
- CROSS JOIN
- Cartesian Product
14. SQL SELF JOIN to Display Employees with Their Managers
Problem Statement
A company stores employee and manager information in the same table.
Write an SQL query to display each employee along with their manager.
Sample Table
employees
| employee_id | employee_name | manager_id |
|---|---|---|
| 1 | Rakesh | NULL |
| 2 | Aman | 1 |
| 3 | Neha | 1 |
| 4 | Vikas | 2 |
SQL Solution
SELECT
e.employee_name AS employee,
m.employee_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.employee_id;
Sample Output
| employee | manager |
|---|---|
| Rakesh | NULL |
| Aman | Rakesh |
| Neha | Rakesh |
| Vikas | Aman |
Explanation
A SELF JOIN joins a table with itself.
Aliases (e and m) are used so SQL can treat the same table as two separate tables.
This technique is commonly used for:
- Organization Charts
- Employee Hierarchies
- Family Trees
- Category Hierarchies
Concepts Covered
- SELF JOIN
- Table Alias
- LEFT JOIN
15. SQL INNER JOIN with Three Tables
Problem Statement
An online shopping website wants to display:
- Customer Name
- Order ID
- Product Name
- Quantity Ordered
The data is stored across three different tables.
Sample Tables
customers
| customer_id | customer_name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
orders
| order_id | customer_id | product_id | quantity |
|---|---|---|---|
| 5001 | 1 | 101 | 2 |
| 5002 | 2 | 102 | 1 |
products
| product_id | product_name |
|---|---|
| 101 | Laptop |
| 102 | Mouse |
SQL Solution
SELECT
customers.customer_name,
orders.order_id,
products.product_name,
orders.quantity
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
INNER JOIN products
ON orders.product_id = products.product_id;
Sample Output
| customer_name | order_id | product_name | quantity |
|---|---|---|---|
| Rahul | 5001 | Laptop | 2 |
| Neha | 5002 | Mouse | 1 |
Explanation
This query joins three tables:
customersordersproducts
It retrieves complete order information in a single query.
Multi-table joins are widely used in:
- E-commerce Platforms
- ERP Systems
- Banking Applications
- CRM Software
- Business Intelligence Reports
Concepts Covered
- Multiple INNER JOINs
- Multi-Table Queries
- Relational Databases
Chapter Summary
In this chapter, you learned how SQL JOIN operations combine data from two or more related tables to produce meaningful results.
In real-world database applications, information is rarely stored in a single table. Instead, related data is separated into multiple tables to reduce redundancy and improve data management. SQL JOINs make it possible to retrieve this related information efficiently.
Throughout this chapter, you practiced:
- Using INNER JOIN to retrieve only matching records.
- Using LEFT JOIN to display all records from the left table, even if there is no match.
- Using RIGHT JOIN to display all records from the right table.
- Using CROSS JOIN to generate every possible combination of rows.
- Using SELF JOIN to relate records within the same table.
- Joining three tables in a single query for real-world reporting.
These concepts are essential for building reports, dashboards, and applications that rely on relational databases.
Key Takeaways
- SQL JOIN combines data from multiple tables.
INNER JOINreturns only matching records.LEFT JOINreturns all rows from the left table and matching rows from the right table.RIGHT JOINreturns all rows from the right table and matching rows from the left table.CROSS JOINcreates every possible combination of rows.SELF JOINjoins a table with itself using aliases.- Multi-table joins are common in business reporting.
- Primary keys and foreign keys are used to establish relationships between tables.
- JOINs are one of the most important SQL interview topics.
- Mastering JOINs is essential for Data Analysts, SQL Developers, Backend Developers, and Data Engineers.
Frequently Asked Questions (FAQs)
1. What is a SQL JOIN?
A SQL JOIN combines data from two or more related tables.
Example:
SELECT
students.student_name,
courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.course_id;
2. What is the difference between INNER JOIN and LEFT JOIN?
| INNER JOIN | LEFT JOIN |
|---|---|
| Returns only matching records | Returns all rows from the left table and matching rows from the right table |
| Non-matching rows are excluded | Non-matching rows contain NULL values |
3. When should I use LEFT JOIN?
Use LEFT JOIN when you want to display all records from the left table, even if there is no matching record in the right table.
Example:
SELECT
customers.customer_name,
orders.order_id
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;
4. What is a CROSS JOIN?
A CROSS JOIN returns the Cartesian product of two tables.
Example:
SELECT
students.student_name,
courses.course_name
FROM students
CROSS JOIN courses;
Every student is paired with every course.
5. What is a SELF JOIN?
A SELF JOIN joins a table with itself.
Example:
SELECT
e.employee_name,
m.employee_name AS manager
FROM employees e
LEFT JOIN employees m
ON e.manager_id = m.employee_id;
This is commonly used to display employee-manager relationships.
6. Can SQL JOIN combine more than two tables?
Yes.
Example:
SELECT
customers.customer_name,
products.product_name
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
INNER JOIN products
ON orders.product_id = products.product_id;
SQL can join multiple related tables in one query.
7. Why are primary keys and foreign keys important in JOINs?
- A Primary Key uniquely identifies each record in a table.
- A Foreign Key references the primary key of another table.
These relationships allow SQL JOINs to match related records correctly.
8. Where are SQL JOINs used in real-world applications?
SQL JOINs are widely used in:
- Banking Systems
- E-commerce Platforms
- Student Management Systems
- HR Management Software
- Hospital Databases
- CRM Applications
- ERP Systems
- Sales Dashboards
- Financial Reporting
- Business Intelligence Projects
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
