Showing posts with label JOIN queries. Show all posts
Showing posts with label JOIN queries. Show all posts

Monday, June 1, 2026

SQL Interview Cheat Sheet - SQL JOINS CHEAT SHEET


Sample Tables

Employee

EmpIDNameDeptID
1Anita10
2Ravi20
3JohnNULL

Department

DeptIDDeptName
10HR
20IT
30Finance

INNER JOIN

Returns only matching records.

SELECT e.Name,
d.DeptName
FROM Employee e
INNER JOIN Department d
ON e.DeptID = d.DeptID;

Result:

NameDeptName
AnitaHR
RaviIT

LEFT JOIN

Returns all rows from left table.

SELECT e.Name,
d.DeptName
FROM Employee e
LEFT JOIN Department d
ON e.DeptID = d.DeptID;

Result:

NameDeptName
AnitaHR
RaviIT
JohnNULL

Interview Question:
Find employees without departments.

SELECT e.*
FROM Employee e
LEFT JOIN Department d
ON e.DeptID=d.DeptID
WHERE d.DeptID IS NULL;

RIGHT JOIN

Returns all rows from right table.

SELECT e.Name,
d.DeptName
FROM Employee e
RIGHT JOIN Department d
ON e.DeptID=d.DeptID;

FULL OUTER JOIN

Returns matching and non-matching rows.

SELECT *
FROM Employee e
FULL OUTER JOIN Department d
ON e.DeptID=d.DeptID;

SELF JOIN

Employee and Manager in same table.

EmpIDNameManagerID
1AnitaNULL
2Ravi1
3John1
SELECT e.Name Employee,
m.Name Manager
FROM Employee e
LEFT JOIN Employee m
ON e.ManagerID=m.EmpID;

CROSS JOIN

Every row joins every row.

SELECT *
FROM Employee
CROSS JOIN Department;

Rows Returned:

Employee Rows × Department Rows

3 × 3 = 9 rows

Wednesday, May 27, 2026

Top SQL Queries for Practice (With Short Answers) - Join Queries (31–45)

 

  1. Get employee with department name
    SELECT e.emp_name, d.dept_name
    FROM Employee e
    JOIN Department d ON e.dept_id = d.dept_id;
  1. Get all employees even without department
    SELECT e.emp_name, d.dept_name
    FROM Employee e
    LEFT JOIN Department d ON e.dept_id = d.dept_id;
  1. Get all departments even without employees
    SELECT e.emp_name, d.dept_name
    FROM Employee e
    RIGHT JOIN Department d ON e.dept_id = d.dept_id;
  1. Get matching + non-matching rows from both tables
    SELECT e.emp_name, d.dept_name
    FROM Employee e
    FULL OUTER JOIN Department d ON e.dept_id = d.dept_id;
  1. Get employee and manager name
    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;
  1. Find employees without department
    SELECT e.*
    FROM Employee e
    LEFT JOIN Department d ON e.dept_id = d.dept_id
    WHERE d.dept_id IS NULL;
  1. Find departments without employees
    SELECT d.*
    FROM Department d
    LEFT JOIN Employee e ON e.dept_id = d.dept_id
    WHERE e.emp_id IS NULL;
  1. Find customers with no orders
    SELECT c.*
    FROM Customers c
    LEFT JOIN Orders o ON c.customer_id = o.customer_id
    WHERE o.order_id IS NULL;
  1. Find orders with customer names
    SELECT o.order_id, c.customer_name
    FROM Orders o
    JOIN Customers c ON o.customer_id = c.customer_id;
  1. Find products never sold
    SELECT p.*
    FROM Products p
    LEFT JOIN Order_Details od ON p.product_id = od.product_id
    WHERE od.product_id IS NULL;
  1. Get all combinations of employees and departments
    SELECT e.emp_name, d.dept_name
    FROM Employee e
    CROSS JOIN Department d;
  1. Find employees earning more than manager
    SELECT e.emp_name
    FROM Employee e
    JOIN Employee m ON e.manager_id = m.emp_id
    WHERE e.salary > m.salary;
  1. Find common records in two tables
    SELECT id FROM TableA
    INTERSECT
    SELECT id FROM TableB;
  1. Find records in A not in B
    SELECT id FROM TableA
    EXCEPT
    SELECT id FROM TableB;
  1. Find duplicate customer orders using join
    SELECT customer_id, order_date, COUNT(*)
    FROM Orders
    GROUP BY customer_id, order_date
    HAVING COUNT(*) > 1;