โ
SQL Joins Interview Questions With Answers ๐ฅ๏ธ
1. What is a JOIN in SQL. Explain with an example.
โข JOIN combines data from multiple tables
โข Tables connect using a common column
โข Usually primary key to foreign key
Example tables
Customers
customer_id, name
Orders
order_id, customer_id, amount
Query
SELECT c.name, o.amount
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;
Explanation
โข SQL matches customer_id in both tables
โข Output shows only related customer order data
2. What is INNER JOIN. When do you use it.
โข INNER JOIN returns only matching rows
โข Rows without match are removed
Example
Find customers who placed orders
Query
SELECT c.customer_id, c.name
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;
Logic
โข Customers without orders are excluded
โข Only matched records appear
3. What is LEFT JOIN. Explain with use case.
โข LEFT JOIN returns all rows from left table
โข Matching rows from right table
โข Non matches show NULL
Example
Find all customers and their orders
Query
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id;
Logic
โข Customers without orders still appear
โข order_id becomes NULL
4. Difference between INNER JOIN and LEFT JOIN.
โข INNER JOIN removes non matching rows
โข LEFT JOIN keeps all left table rows
โข LEFT JOIN shows NULL for missing matches
Interview tip
Explain using one missing record example
5. What is RIGHT JOIN.
โข Returns all rows from right table
โข Matching rows from left table
โข Rarely used in real projects
Example
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o
ON c.customer_id = o.customer_id;
6. What is FULL OUTER JOIN.
โข Returns all rows from both tables
โข Matches where possible
โข Non matches show NULL
Example
SELECT c.name, o.order_id
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id;
Use case
โข Data reconciliation
โข Comparing two datasets
7. How do you find records present in one table but not in another.
Find customers with no orders
Query
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
Logic
โข LEFT JOIN keeps all customers
โข WHERE filters non matched rows
8. Explain JOIN with WHERE clause. Common mistake.
โข WHERE runs after JOIN
โข Wrong WHERE condition breaks LEFT JOIN
Wrong
LEFT JOIN orders
WHERE orders.amount > 1000
Correct
LEFT JOIN orders
ON (link unavailable) = (link unavailable)
AND orders.amount > 1000
9. How do you join more than two tables.
โข JOIN step by step
โข Each JOIN needs condition
Example
SELECT c.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;
10. SQL execution order for JOIN queries.
โข FROM
โข JOIN
โข WHERE
โข GROUP BY
โข HAVING
โข SELECT
โข ORDER BY
Interview advice
โข Always explain logic first
โข Draw table flow in words
โข Then write query
Double Tap โฅ๏ธ For More
Post #2247
2.71K
- โค 10