CTA
Structured Query Language (SQL) is a type of programming language that is used for storing, searching, updating, and retrieving data in a table inside a relational database. SQL Joins are used to join some rows or all the rows from two or more tables based on a common column between them. SQL JOIN is a very important topic for interviewers, and there can be many important related topics to SQL joins.
In this blog, we will provide you with the Top Interview Questions on SQL Joins. So let’s get started!
Table of Contents:
Watch this video on SQL Interview Questions and Answers
What are SQL Joins?
Before you get into the questions, first, you should understand what SQL Joins are. SQL Joins are used for retrieving data from two or more tables based on a column that is related to both tables. It can retrieve data from two or more tables simultaneously. This is done by using common key values that are shared across different tables. You can use each SQL JOIN with multiple tables. You can also pair it with other clauses, the most popular one will be using the WHERE clause for filtering the retrieved data.
The various types of SQL Joins are given below:

Master SQL: Empower Your Data Skills Today!
Unlock the power of databases with hands-on SQL training and become a data-driven professional.
SQL Joins Interview Questions for Beginners
1. What are joins in SQL?
Joins in SQL are used to combine records from multiple tables and also for retrieving data from those tables based on a column that is common to both tables. By using the SQL JOIN clause, the records can be fetched from two or more tables in a database. In general, you can use SQL joins to retrieve data from two or more tables that are related to each other.
2. What are the different types of Joins in SQL?
There are 5 types of Joins in SQL that are explained below:
-
- INNER JOIN: It is responsible for returning only the rows between 2 similar tables.
-
- LEFT JOIN (LEFT OUTER JOIN): It is responsible for returning all rows from the left table, and similar rows from the right table. If there are no similar rows, you will get NULL values from the right table.
-
- RIGHT JOIN (RIGHT OUTER JOIN): It is used for returning all the rows from the right table, and similar rows from the left table. If there are similar rows, you will get NULL values for columns from the left table.
-
- FULL JOIN (FULL OUTER JOIN): It is used for returning all the rows when there are matches found in one of the tables. If there are no matches found, you will get NULL values for columns from the right table.
3. What is the importance of SQL Joins in Database Management?
The importance of SQL Joins in Database Management is given below:
-
- SQL joins are used to join data from different tables. This makes it easier for you to read and use in one place.
-
- Joins in SQL help you to maintain a normalized database (there is no duplicate data).
-
- Using SQL joins will help you reduce the number of errors while updating or deleting records.
-
- SQL joins are used to retrieve data quickly than running multiple queries.
-
- A single join fetches all the required data at once, instead of running many small queries.
-
- SQL joins also help to improve the performance for searching, filtering, and sorting.
4. Explain the Merge join in SQL.
A Merge Join is used to combine two pre-sorted datasets. It used INNER, LEFT, or FULL joins by scanning the two tables simultaneously and matching rows. This makes it one of the fastest join methods. If you want this to work, you must sort both input tables on the join column, and the join columns must have matching data types (e.g., you cannot merge a numeric column with a text column). While models that perform well with large sorted datasets have their limitations, one of these is that they need proper sorting beforehand and should be compatible with strict data types between the columns that are joined.
5. State the difference between inner join and left join.
Given below are the differences between inner join and left join in tabular format:
Now, let us write a sample code so that you can understand the difference between inner join and left join.
Example: SQL code that shows the difference between INNER JOIN and LEFT JOIN
-- Step 1: Create the Employees table
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT
);
-- Step 2: Create the Departments table
CREATE TABLE Departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
-- Step 3: Insert sample data into Employees
INSERT INTO Employees (employee_id, name, department_id) VALUES
(1, 'Alice', 101),
(2, 'Bob', 102),
(3, 'Charlie', 104),
(4, 'Diana', NULL);
-- Step 4: Insert sample data into Departments
INSERT INTO Departments (department_id, department_name) VALUES
(101, 'HR'),
(102, 'Engineering'),
(103, 'Marketing');
-- Step 5: INNER JOIN - returns only matching rows
SELECT
e.name AS Employee_Name,
d.department_name AS Department
FROM
Employees e
INNER JOIN
Departments d
ON
e.department_id = d.department_id;
-- Step 6: LEFT JOIN - returns all rows from Employees with matched/unmatched Departments
SELECT
e.name AS Employee_Name,
d.department_name AS Department
FROM
Employees e
LEFT JOIN
Departments d
ON
e.department_id = d.department_id;
Output:
Explanation:
-
- The above SQL code is used to create two tables, one table is for employees and the other for departments. After that, it fills the two tables with sample records for testing.
-
- After that, the code runs two queries: one with an INNER JOIN and the other with a LEFT JOIN, which shows the difference between the outputs.
6. Write the differences between left join and right join.
Given below are the differences between left join and right join in tabular format:
Feature |
LEFT JOIN |
RIGHT JOIN |
Definition |
Returns all records from the left table and matched records from the right table. |
Returns all records from the right table and matched records from the left table. |
Unmatched Rows |
NULLs are returned for unmatched rows from the right table. |
NULLs are returned for unmatched rows from the left table. |
Focus Table |
Prioritizes the left table. |
Prioritizes the right table. |
Usage Scenario |
Used when all records from the left table are needed. |
Used when all records from the right table are needed. |
Symmetry |
Asymmetric; LEFT JOIN ≠ RIGHT JOIN. |
Asymmetric; RIGHT JOIN ≠ LEFT JOIN. |
Now, let us write a sample code so that you can understand the difference between left join and right join.
Example: SQL code that shows the difference between LEFT JOIN and RIGHT JOIN
Step 1: At first, create the employees table
CREATE TABLE employees (
emp_id INT,
emp_name VARCHAR(50),
dept_id INT
);
Step 2: After that, insert data into the employees table
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES
(1, 'E1', 101),
(2, ‘E2’, 102),
(3, 'E3', NULL),
(4, 'E4', 104);
Step 3: Next, you have to create the departments table
CREATE TABLE departments (
dept_id INT,
dept_name VARCHAR(50)
);
Step 4: Next, you have to insert the data into the departments table
INSERT INTO departments (dept_id, dept_name) VALUES
(101, 'HR'),
(102, 'Finance'),
(103, 'Engineering');
Step 5: Implement LEFT JOIN to your code
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.dept_id;
Step 6: Implement the RIGHT JOIN to your code
SELECT e.emp_name, d.dept_name
FROM employees e
RIGHT JOIN departments d ON e.dept_id = d.dept_id;
Output: Demonstrating both LEFT and RIGHT JOINS
Explanation:
The above SQL code clearly shows the difference between LEFT JOIN and RIGHT JOIN. It shows how LEFT JOIN retains all the rows present in the employees table and the RIGHT JOIN retains all the rows present in the departments table, regardless of whether there is a match found or not.
7. What is Hash Join in SQL?
A Hash Join in SQL is a method that is used to combine rows from two tables that have a similar column. It works with two inputs, which is the same as other types of joins in SQL:
-
- A build input ( it is usually the smaller table).
-
- The probe input ( it is usually the larger table).
In a Hash Join, at first, the database creates a hash table by using the values from the build input. After that, it goes through the probe input and looks for the matching values using the hash table.
Hash Join can be useful when:
-
- When the tables are not sorted.
-
- When there are no indexes present.
Hash Joins are used by the database when no other join method is more efficient. They can be used for joining large, unsorted, and non-indexed tables
8. What is a Nested Join in SQL?
A JOIN is a technique in SQL that is used to combine rows from two or more tables, which is based on a common column between them. One of the most common methods to perform this operation is the Nested Loop Join. In a Nested Loop Join, the database treats one of the tables as the outer table and the other table as the inner table. This process starts by taking rows from the outer table and scanning the entire inner table to find all the rows that match the join condition. This process continues to repeat for every row in the outer table. Nested Loop Joins are efficient when the table is small and when the inner table contains indexes on the join column.
Given below is a sample code demonstrating Nested Join in SQL:
Example:
-- Step 1: At first, you have to create 'Departments' table
CREATE TABLE Departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
-- Step 2: Then you have to indert insert sample data into 'Departments'
INSERT INTO Departments (dept_id, dept_name) VALUES
(1, 'HR'),
(2, 'Engineering'),
(3, 'Marketing');
-- Step 3: Then you have to create 'Employees' table
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);
-- Step 4: After that, you have to insert sample data into 'Employees'
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES
(101, 'E1', 1),
(102, 'E2', 2),
(103, 'E3', 2),
(104, 'E4', 3),
(105, 'E5', NULL);
-- Step 5: Then you have to perform INNER JOIN (Nested Loop Join behavior in small datasets)
SELECT
e.emp_id,
e.emp_name,
d.dept_name
FROM
Employees e
INNER JOIN
Departments d ON e.dept_id = d.dept_id;
Output:
Explanation:
-
- The above SQL code is used to join the Employees table with the departments table on the dept_id.
-
- Employees who do not have a matching department are excluded.
9. Write an SQL query to join 3 tables.
Joining 3 tables in SQL will help you to combine the data or information that is stored separately. For example, one table has customer details, another table has the order of the information, and the third table has the product data. Then, by using the JOIN on the columns such as customer ID or product ID, you can combine these tables together, which will provide a combined view of what was ordered by whom and when. Also, this will help you to analyze and understand data that spans across various parts of your database.
Given below is a sample code for joining 3 tables:
-- Step 1: At first, you have to create 'Customers' table
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100)
);
-- Step 2: After that, you have to insert data into 'Customers'
INSERT INTO Customers (customer_id, customer_name) VALUES
(1, 'C1'),
(2, 'C2');
-- Step 3: Then you have to create the 'Orders' table
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id INT,
product_id INT,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
-- Step 4: Then you have to insert data into 'Orders'
INSERT INTO Orders (order_id, customer_id, product_id) VALUES
(101, 1, 1001),
(102, 2, 1002),
(103, 1, 1002);
-- Step 5: After that you have to create 'Products' table
CREATE TABLE Products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100)
);
-- Step 6: Then you have to insert data into 'Products'
INSERT INTO Products (product_id, product_name) VALUES
(1001, 'Laptop'),
(1002, 'Mobile');
-- Step 7: At last, join all three tables
SELECT
c.customer_name,
o.order_id,
p.product_name
FROM
Customers c
JOIN
Orders o ON c.customer_id = o.customer_id
JOIN
Products p ON o.product_id = p.product_id;
Example: SQL query to join 3 tables
Output:
Explanation:
The above query shows how you can retrieve order details of customers, including the product name, by joining three tables using INNER JOINs.
10. How can you join a table to itself?
In SQL, when you join a table to itself, it is called a self-join. You can use a self-join when the table contains hierarchical relationships, like one employee reporting to another employee. In real-world cases, you also have to include additional related tables, like departments or projects, that can help you to generate comprehensive reports.
Given below is a sample code for joining a table to itself using a Self-Join.
Example: SQL Query to join a table to itself along with two other tables:
-- Step 1: At first, you have create the 'Employee' table
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
manager_id INT,
dept_id INT
);
-- Step 2: Then you have to insert data into 'Employee'
INSERT INTO Employee (emp_id, emp_name, manager_id, dept_id) VALUES
(1, 'C1', NULL, 10),
(2, 'C2', 1, 10),
(3, 'C3', 1, 20),
(4, 'C4', 2, 20);
-- Step 3: After that you have to create the 'Department' table
CREATE TABLE Department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
);
-- Step 4: Then you have to insert data into 'Department'
INSERT INTO Department (dept_id, dept_name) VALUES
(10, 'Engineering'),
(20, 'Marketing');
-- Step 5: After that you have to Create the 'Project' table
CREATE TABLE Project (
project_id INT PRIMARY KEY,
emp_id INT,
project_name VARCHAR(100)
);
-- Step 6: Then you have to insert data into 'Project'
INSERT INTO Project (project_id, emp_id, project_name) VALUES
(201, 2, 'App Development'),
(202, 3, 'Market Research'),
(203, 4, 'SEO Optimization');
-- Step 7: After that you have to perform the self-join along with joins to other tables
SELECT
e.emp_name AS Employee,
m.emp_name AS Manager,
d.dept_name AS Department,
p.project_name AS Project
FROM
Employee e
LEFT JOIN
Employee m ON e.manager_id = m.emp_id
LEFT JOIN
Department d ON e.dept_id = d.dept_id
LEFT JOIN
Project p ON e.emp_id = p.emp_id;
Output:
Explanation:
The above output shows the relationship between employees and managers, their departments, and the projects they are working on. All this information is derived from three tables, which include a self-join on the Employee table.
Intermediate SQL Interview Questions
11. What happens when you perform a LEFT JOIN and there are no matching rows in the right table?
When you perform a LEFT JOIN and there are no similar rows on the right table, the result still contains all the rows from the left table. For the rows that have no similar rows on the right table, SQL fills them with NULL values for all columns from the right table. This helps you to ensure that there are no rows excluded from the left table.
Given below is a sample code demonstrating the above-mentioned process:
Example:
-- Step 1: First, you have to create the 'employees' table
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50)
);
-- Step 2: Then you have to insert data into 'employees'
INSERT INTO employees (emp_id, emp_name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');
-- Step 3: Now you have to create the 'projects' table
CREATE TABLE projects (
project_id INT PRIMARY KEY,
emp_id INT,
project_name VARCHAR(100),
FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
);
-- Step 4: Then, you have to insert data into 'projects'
INSERT INTO projects (project_id, emp_id, project_name) VALUES
(101, 1, 'Website Redesign'),
(102, 2, 'Database Upgrade');
-- Note: Charlie (emp_id = 3) has no project
-- Step 5: After that, you have to perform a LEFT JOIN
SELECT
e.emp_id,
e.emp_name,
p.project_name
FROM
employees e
LEFT JOIN
projects p
ON
e.emp_id = p.emp_id;
Output:
Explanation:
-
- In the above code, the LEFT JOIN is used to ensure that all employees appear in the output.
-
- For C3, there is no match found in the projects table. Hence, NULL value is returned for the project_name column.
12. You are given two tables: Customers and Orders. Now write a query to find all customers who have never placed an order.
When a LEFT JOIN is used between two tables, all the records from the left table are included in the result set. If there are no matching records found in the right table, the result will include the row of the left table, but the columns in the right table will contain NULL values for that row.
Given below is a practical example by using two tables: Employees and Departments.
Example:
-- Step 1: Create the 'Employees' table
CREATE TABLE Employees (
emp_id INT,
emp_name VARCHAR(50),
dept_id INT
);
-- Step 2: Insert sample data into 'Employees'
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES
(1, 'C1', 101),
(2, 'C2', 102),
(3, 'C3', 103); -- Note: dept_id 103 does not exist in Departments
-- Step 3: Create the 'Departments' table
CREATE TABLE Departments (
dept_id INT,
dept_name VARCHAR(50)
);
-- Step 4: Insert sample data into 'Departments'
INSERT INTO Departments (dept_id, dept_name) VALUES
(101, 'HR'),
(102, 'Engineering');
-- Step 5: LEFT JOIN to fetch employee names with their department names
SELECT
e.emp_name,
d.dept_name
FROM
Employees e
LEFT JOIN
Departments d
ON
e.dept_id = d.dept_id;
Output:
Explanation:
-
- In the above output, C1 and C2 belong to the departments that exist. Therefore, you can retrieve their department names.
-
- The department ID of C3 (103) does not exist in the Departments table. Hence, the dept_name is shown as null.
13. What is the difference between CROSS JOIN and INNER JOIN?
The difference between CROSS JOIN and INNER JOIN is given below in a tabular format:
Feature |
CROSS JOIN |
INNER JOIN |
Definition |
Produces the Cartesian product of two tables |
Returns only matching rows based on a given condition |
Join Condition Required |
No |
Yes |
Number of Rows Returned |
Multiplication of row counts from both tables |
Depends on matching rows based on a condition |
Use Case |
To generate all possible combinations |
To retrieve logically related data |
Result Size |
Usually very large |
Usually smaller and meaningful |
Common Columns Required |
Not required |
Required |
Given below is a sample code to show the differences between CROSS JOIN and INNER JOIN.
Example:
-- Step 1: Create Department Table
DROP TABLE IF EXISTS Department;
CREATE TABLE Department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
-- Insert sample data into Department table
INSERT INTO Department (dept_id, dept_name) VALUES
(1, 'Sales'),
(2, 'Marketing');
-- Step 2: Create Employee Table
DROP TABLE IF EXISTS Employee;
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);
-- Insert sample data into Employee table
INSERT INTO Employee (emp_id, emp_name, dept_id) VALUES
(101, 'E1', 1),
(102, 'E2', 2),
(103, 'E3', 1);
-- Step 3: Demonstrate CROSS JOIN
SELECT emp.emp_name, dept.dept_name
FROM Employee emp
CROSS JOIN Department dept;
-- Step 4: Demonstrate INNER JOIN
SELECT emp.emp_name, dept.dept_name
FROM Employee emp
INNER JOIN Department dept
ON emp.dept_id = dept.dept_id;
Output:
Explanation:
In the above SQL code, the Department and Employees tables are created. After that, sam
14. Write a query to display employee names along with their manager names from a single Employee table.
To display the names of the employees along with the names of their managers from a single employee table, you can use a self-join. For this, the Employee table is joined to itself on the employee ID of the manager.
Example:
-- Step 1: Drop the table if it already exists
DROP TABLE IF EXISTS Employee;
-- Step 2: Create the Employee table
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
manager_id INT
);
-- Step 3: Insert sample data into Employee table
INSERT INTO Employee (emp_id, emp_name, manager_id) VALUES
(1, 'E1', NULL),
(2, 'E2', 1),
(3, 'E3', 1),
(4, 'E4', 2),
(5, 'E5', 2),
(6, 'E6', 3);
-- Step 4: Query to display employees along with their manager names
SELECT
e.emp_name AS Employee,
m.emp_name AS Manager
FROM
Employee e
LEFT JOIN
Employee m ON e.manager_id = m.emp_id;
Output:
Explanation:
The above SQL code is used to create an Employee table, which shows the relationships of the employees with their managers. It then retrieves a list of employees along with the names of their respective managers.
SQL Joins Interview Questions for Experienced
15. How should data be structured to support Join Operations in a one-to-many relationship?
For supporting Join Operations in a one-to-many relationship, the data should be structured in such a way that one table contains unique primary keys, and the other table contains a foreign key which takes references from the primary key in the first table. This design assists the SQL joins to merge correlated records in the two tables by using the shared key.
For example, let us consider a relationship where one customer can have many orders. Given below is a practical representation of how you can structure the tables.
Example: One-to-Many Join
-- Step 1: At first, you should drop tables if they already exist
DROP TABLE IF EXISTS Orders;
DROP TABLE IF EXISTS Customers;
-- Step 2: After that, you have to create Customers table
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50)
);
-- Step 3: Then you have to Insert sample data into Customers table
INSERT INTO Customers (customer_id, customer_name) VALUES
(1, 'C1'),
(2, 'C2'),
(3, 'C3');
-- Step 4: After that, you have to Create Orders table
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
-- Step 5: After that, you have to insert sample data into Orders table
INSERT INTO Orders (order_id, order_date, customer_id) VALUES
(101, '2024-01-10', 1),
(102, '2024-01-12', 2),
(103, '2024-01-15', 1),
(104, '2024-01-20', 3);
-- Step 6: At last, you have to join both tables to get customer names with their orders
SELECT
c.customer_name,
o.order_id,
o.order_date
FROM
Customers c
INNER JOIN
Orders o ON c.customer_id = o.customer_id;
Output:
Explanation:
The above code is used to create two tables, Customers and Orders, that are related to each other. After that, sample data is inserted, which in return retrieves a list of names of customers along with their corresponding author IDs and dates. The customer_id column is INNER JOINed.
Get 100% Hike!
Master Most in Demand Skills Now!
16. How can you structure data to support Join Operations in a many-to-many relationship?
In order to support JOIN operations in a many-to-many relationship, you should structure the data using 3 tables:
-
- Two entity tables that are used to hold the main data (e.g., Students and Courses)
-
- One junction table is used to link two entities together by using foreign keys. Here, each row is used to represent one instance of the relationship between the two entities.
The above-mentioned design helps in the efficient use of JOINs, that is used to retrieve related records from both sides of the relationship.
Example: Student Enrolling in Courses
Given below is an example where each student can enroll in multiple courses, and each course can have multiple students.
-- Step 1: At first, you have to drop tables if they already exist
DROP TABLE IF EXISTS Enrollments;
DROP TABLE IF EXISTS Students;
DROP TABLE IF EXISTS Courses;
-- Step 2: Then you have to create the Students table
CREATE TABLE Students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50)
);
-- Step 3: After that, you have to create the Courses table
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);
-- Step 4: Then you have to create Enrollments table (junction table)
CREATE TABLE Enrollments (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);
-- Step 5: After that you have to insert sample data into Students
INSERT INTO Students (student_id, student_name) VALUES
(1, 'S1'),
(2, 'S2'),
(3, 'S3');
-- Step 6: Then you have to insert sample data into Courses
INSERT INTO Courses (course_id, course_name) VALUES
(101, 'Mathematics'),
(102, 'History'),
(103, 'Physics');
-- Step 7: After that you have to insert sample data into Enrollments
INSERT INTO Enrollments (student_id, course_id) VALUES
(1, 101),
(1, 102),
(2, 101),
(3, 103),
(3, 102);
-- Step 8: At last you have to JOIN all three tables to get a full relationship view
SELECT
s.student_name,
c.course_name
FROM
Enrollments e
JOIN
Students s ON e.student_id = s.student_id
JOIN
Courses c ON e.course_id = c.course_id;
Output:
Explanation:
The above SQL code is used to create a many-to-many relationship between Students and Courses. It uses a junction table (Enrollments), which is used to fill it with sample data. After that, it retrieves a list of students along with the courses that they have enrolled in through JOIN operations.
17. What is Natural Join?
A Natural Join in SQL is the one that matches and joins the rows between two or more tables based on all the columns automatically and by matching the compatible data types. Unlike INNER JOIN, where you have to specify the condition separately using ON, a NATURAL JOIN is responsible for eliminating the requirement to manually write the join condition. It automatically predicts that you have to join all columns that exist in both tables with the same name.
Natural joins are responsible for quick queries, but are not used for production environments due to their implicit behavior. If there are any changes in the schema, it can lead to unwanted results.
Given below is a sample code for NATURAL JOIN in SQL:
Example:
-- Step 1: At first, you have to drop tables if they already exist
DROP TABLE IF EXISTS Departments;
DROP TABLE IF EXISTS Employees;
-- Step 2: The you have to create Departments table
CREATE TABLE Departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
-- Step 3: After that you have to create Employees table
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT
);
-- Step 4: Then you have to insert data into Departments
INSERT INTO Departments (dept_id, dept_name) VALUES
(1, 'Sales'),
(2, 'Marketing'),
(3, 'Finance');
-- Step 5: After that, you have to insert data into Employees
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES
(101, 'E1', 1),
(102, 'E2', 2),
(103, 'E3', 3),
(104, 'E4', 1);
-- Step 6: Then perform NATURAL JOIN to combine employee and department info
SELECT emp_name, dept_id, dept_name
FROM Employees
NATURAL JOIN Departments;
Output:
Explanation:
The above SQL code is used to create two tables: Employees and Departments. After that, sample data is added to the two tables, and then a NATURAL JOIN is performed to return the names of each employee with their department ID, along with the name based on the common column dept_id.
18. What is Equi Join in SQL?
In SQL, an EQUI JOIN is a type of INNER JOIN that is used to retrieve rows from multiple tables based on a condition that is used to compare values in common columns by using the equal to sign ( = ). It can be used to return records where the columns which are specified in both tables have missing values.
Given below is a sample code for EQUI JOIN in SQL:
Example:
-- Step 1: At first, you have to drop tables if they already exist
DROP TABLE IF EXISTS Departments;
DROP TABLE IF EXISTS Employees;
-- Step 2: Then, you have to create the Departments table
CREATE TABLE Departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
-- Step 3: After that, you have to create Employees table
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT
);
-- Step 4: Then you have to insert sample data into Departments
INSERT INTO Departments (dept_id, dept_name) VALUES
(1, 'Sales'),
(2, 'Marketing'),
(3, 'Finance');
-- Step 5: After that, you have to insert sample data into Employees
INSERT INTO Employees (emp_id, emp_name, dept_id) VALUES
(101, 'E1', 1),
(102, 'E2', 2),
(103, 'E3', 3),
(104, 'E4', 1);
-- Step 6: At last, just perform Equi Join using equality condition
SELECT
e.emp_name,
d.dept_name
FROM
Employees e,
Departments d
WHERE
e.dept_id = d.dept_id;
FROM Employees
NATURAL JOIN Departments;
Output:
Explanation:
The above SQL code is used to create two tables: Departments and Employees. After that, sample data is added to them, which returns the names of the employees along with their department names. This is done using both an Equi Join and a Natural Join based on the shared column name, dept_id.
19. What is a Non-Equi Join?
A Non-Equi Join in SQL is used to retrieve data from various tables. This can be done by using join conditions along with comparison operators like <, >, !=, or BETWEEN instead of using just the equal to sign (=).
Example:
DROP TABLE IF EXISTS Employees;
DROP TABLE IF EXISTS SalaryGrades;
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
salary DECIMAL(10, 2)
);
CREATE TABLE SalaryGrades (
grade_id INT PRIMARY KEY,
min_salary DECIMAL(10, 2),
max_salary DECIMAL(10, 2)
);
INSERT INTO Employees (emp_id, emp_name, salary) VALUES
(1, 'E1', 3000),
(2, 'E2', 4500),
(3, 'E3', 6000),
(4, 'E4', 7500),
(5, 'E5', 9000);
INSERT INTO SalaryGrades (grade_id, min_salary, max_salary) VALUES
(1, 2000, 3999),
(2, 4000, 5999),
(3, 6000, 7999),
(4, 8000, 9999);
SELECT
e.emp_name,
e.salary,
s.grade_id
FROM
Employees e
JOIN
SalaryGrades s ON e.salary BETWEEN s.min_salary AND s.max_salary;
Output:
Explanation:
The above SQL code is used to create two tables: Employees and SalaryGrades. After that, sample data is added to the tables. After that, Non-Equi join is performed using the BETWEEN opera
20. What is the difference between the Union Clause and the Join Clause in SQL?
The difference between the Union Clause and Join Clause is given below in tabular format:
Feature |
UNION Clause |
JOIN Clause |
Purpose |
It is used to combine the results of two or more SELECT queries. |
It is used to combine columns from two or more tables. |
Operation Type |
It uses a vertical combination (adds rows). |
It uses a horizontal combination (adds columns). |
Table Relationship |
It does not need a relationship between tables. |
It needs a defined relationship (usually via keys) |
Number of Columns |
All queries must have the same number of columns |
Can involve any number of columns from joined tables |
Column Data Types |
Columns must have compatible data types |
Columns can have different data types |
Duplicates Handling |
Removes duplicates by default (UNION), keeps them with UNION ALL |
Keeps all rows unless filtered explicitly |
Common Use Cases |
Combine query results from similar tables (e.g., logs from multiple months) |
Retrieve related data across tables (e.g., employees and departments) |
Performance Impact |
Generally slower due to duplicate elimination |
Can be faster with proper indexing |
Syntax Example |
SELECT name FROM A UNION SELECT name FROM B |
SELECT A.name, B.salary FROM A JOIN B ON A.id = B.id |
21. Is it necessary for the Join Condition to be based on equality?
The JOIN condition in SQL is not related to equality conditions. It can use both the equal to operator ( = ) and comparison operators like <, <=, >, >=, !=, and BETWEEN. These are called the non-equi joins. They may be applied to listing the particular ranges, to locating the unique combinations, or to indicating the duplicates according to some conditions other than equality.
22. What is the difference between Full Join and Cross Join?
The difference between Full Join and Cross Join is given below in Tabular format:
Feature |
UNION Clause |
JOIN Clause |
Purpose |
It is used to combine the results of two or more SELECT queries. |
It is used to combine columns from two or more tables. |
Operation Type |
It uses a vertical combination (adds rows). |
It uses a horizontal combination (adds columns). |
Table Relationship |
It does not need a relationship between tables. |
It needs a defined relationship (usually via keys) |
Number of Columns |
All queries must have the same number of columns |
Can involve any number of columns from joined tables |
Column Data Types |
Columns must have compatible data types |
Columns can have different data types |
Duplicates Handling |
Removes duplicates by default (UNION), keeps them with UNION ALL |
Keeps all rows unless filtered explicitly |
Common Use Cases |
Combine query results from similar tables (e.g., logs from multiple months) |
Retrieve related data across tables (e.g., employees and departments) |
Performance Impact |
Generally slower due to duplicate elimination |
Can be faster with proper indexing |
Syntax Example |
SELECT name FROM A UNION SELECT name FROM B |
SELECT A.name, B.salary FROM A JOIN B ON A.id = B.id |
23. Explain Common Table Expression in SQL.
In SQL, A Common Table Expression (CTE) is basically a temporary result set that is defined using the WITH keyword. It can be used in a SELECT, INSERT, UPDATE, DELETE, or MERGE statement. You can define one or more CTEs and reuse them in the main query to get better readability.
24. Write the differences between Cross Join and Natural Join.
The difference between Cross Join and Natural Join is given below in a tabular format:
Feature |
Cross Join |
Natural Join |
Definition |
Combines every row of the first table with every row of the second table (Cartesian product). |
Automatically joins tables based on columns with the same name and compatible data types. |
Join Condition |
Does not require any conditions. |
Implicitly matches columns with the same names in both tables. |
Result Set Size |
Very large, as it multiplies rows from both tables. |
Smaller and more meaningful result set based on matched columns. |
Use Case |
Used for generating all possible combinations (e.g., product catalog). |
Used when tables share one or more common column names and you want to match them. |
Syntax Simplicity |
Simple syntax but usually followed by a filter condition. |
Very simple, as it does not require an ON or USING clause. |
Potential Pitfalls |
It can produce extremely large results if not used carefully. |
May join on unintended columns if they have the same name, but unrelated data. |
Syntax Example |
SELECT * FROM TableA CROSS JOIN TableB; |
SELECT * FROM TableA NATURAL JOIN TableB; |
25. How would you optimize a complex query involving multiple joins (INNER, LEFT, and CROSS JOINs) on large datasets to improve performance?
Complex join queries should be optimized by ensuring that proper indexes are used on join keys so as to quickly match rows and avoid a full table scan. The WHERE clause can be used early in the query to apply filters and reduce the volume of rows fleshed out in the joins, which enhances efficiency. You should avoid using CROSS JOINs unless it is necessary. This is because they generate large intermediate result sets. By reviewing the execution plan of the queries, you can identify the inefficiencies in performance, like problems in join orders or missing indexes. Also, by selecting only the required columns in the SELECT clause, you can reduce the amount of memory usage, and it also helps to speed up the process of retrieving data.
SQL Joins Salary based on Skills
CTA
Salary in India
Experience Level |
Average Salary (INR) |
Entry-Level (Freshers) |
3,10,000 |
Intermediate (2-6 years) |
6,70,000 |
Senior-Level |
16,00,000 |
Salary in the US
Experience Level |
Average Salary (USD) |
Entry-Level (Freshers) |
65,000 |
Intermediate (2-6 years) |
97,500 |
Senior-Level |
122,713 |
SQL Developer Joins Job Trends
According to the Bureau of Labor Statistics US, the employment of SQL developers is projected to grow 22% by 2029.
- Growth Projections: The growth suggested by the Bureau of Labor Statistics of 22% in the field of SQL development might surpass all other occupation fields’ growth by 8%.
Job Opportunities for SQL Developers
Job Role |
Description |
Database Administrator |
The primary role is to ensure the efficient working of the database without any issues. |
Business Analyst |
The use of SQL skills often helps business analysts retrieve data for reporting and analysis. |
Data Analyst |
Among data analysts, SQL helps them analyze large amounts of data stored in the databases. |
Data Scientist |
A very efficient tool for data scientists to access and analyze large amounts of data. |
Software Engineer |
To interact with the databases while working on software solutions. |
ETL Developer |
Use SQL to access and manage data, but it is primarily used in the ETL process. |
Data Modeler |
Data model optimization through SQL is indicated by performance. |
Server Engineer |
To manage servers and handle data storage and retrieval requests. |
Roles and Responsibilities in SQL Joins
According to the job posted on Naukri.com by TELUS International
Role: SQL Developer
- Responsibilities:
- Leverage business and statistical knowledge to transform data into information.
- Elicit, document, and confirm business requirements and technical specifications.
- Perform data quality assessments, data transformation, and data cleansing.
- Develop SQL scripts, processes, and ETL flows to support reports, dashboards, and KPIs in Tableau Desktop.
- Skill Required:
- Develop BI solutions using standard RDBMS, Oracle SQL, MS SQL, etc.
- Strong expertise in SQL query development.
- Experienced in estimating complexity, time, and effort and planning projects.
- Ability to work both independently and collaboratively with BI stakeholders.
- Demonstrate organizational, analytical, and innovative thinking skills.
I hope this set of SQL Joins Interview Questions will help you prepare for your interviews. Best of luck!
Become a Data Pro – Free SQL Course Inside!
Learn how to write powerful queries and manage databases with our beginner-friendly free SQL course.
Conclusion
It is important for you to master SQL JOINS if you are preparing for data analyst interviews. You should have good knowledge of various types of SQL Joins like INNER, LEFT, RIGHT, FULL, CROSS, NATURAL, EQUI, and NON-EQUI, and also their differences. You can build your knowledge on data queries by practicing the commonly asked questions mentioned in this blog. Having good knowledge of SQL Joins will help you to build your foundation in relational databases and also set you apart from others.
Top SQL Joins Interview Questions – FAQs
Frequently Asked Questions
Q1. What is an SQL JOIN?
Joins in SQL are used to bring together the rows of two or more tables, having columns that have a relationship with each other.
Q2. When should I use an INNER JOIN?
You can use an INNER JOIN when you want to have similar rows from both tables.
Q3. What is the main purpose of LEFT JOIN?
You can use LEFT JOIN to show all records from the left table and match from the right table if it is available.
Q4. Can I join more than two tables in SQL?
In SQL, it is possible to chain JOIN clauses separated by common keys to join many tables.
Q5. What is the difference between JOIN and UNION?
The difference is that the JOIN clause is used to combine columns from different tables, while the UNION clause is used to stack rows from similar tables.
Our SQL Courses Duration and Fees
Cohort Starts on: 22nd Jun 2025
₹15,048
Cohort Starts on: 29th Jun 2025
₹15,048