Q15. Employees in Mumbai-based departments
List employees whose department is located in Mumbai, with department name and location.
SELECT e.name, d.dept_name, d.location
FROM employees e
JOIN departments d ON d.dept_id = e.dept_id
WHERE d.location = 'Mumbai'
ORDER BY e.emp_id;
-- Gauri, Harsh, Isha, Jay, Kavya -> Sales | Mumbai
-- Omkar, Pooja, Qasim, Riya -> Finance | Mumbai
-- (9 rows)Q16. Employee count for every department (LEFT JOIN)
Show every department with its employee count. Departments with no employees must show 0.
SELECT d.dept_name, COUNT(e.emp_id) AS emp_count
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.dept_id
GROUP BY d.dept_id, d.dept_name
ORDER BY emp_count DESC, d.dept_name;
-- dept_name | emp_count
-- Engineering | 7
-- Sales | 5
-- Finance | 4
-- HR | 3
-- Marketing | 0Q17. Customers who never ordered (anti-join)
Find customers who have never placed an order.
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
-- customer_id | name
-- 12 | LeelaQ18. Employee and manager names (self join)
For employees in the Finance department (dept_id 4), show the employee name and their manager's name. Show 'No Manager' when there is none.
SELECT e.name AS employee,
COALESCE(m.name, 'No Manager') AS manager
FROM employees e
LEFT JOIN employees m ON m.emp_id = e.manager_id
WHERE e.dept_id = 4
ORDER BY e.emp_id;
-- employee | manager
-- Omkar | No Manager
-- Pooja | Omkar
-- Qasim | Omkar
-- Riya | PoojaQ19. Order lines for one customer (multi-table join)
Show every order line for the customer 'Bina': order id, product name, quantity and line total (quantity x price).
SELECT o.order_id, p.name AS product, oi.quantity,
oi.quantity * p.price AS line_total
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE c.name = 'Bina'
ORDER BY o.order_id, oi.item_id;
-- order_id | product | quantity | line_total
-- 2 | Smartphone | 1 | 30000
-- 7 | Water Bottle | 3 | 1500
-- 13 | Smartphone | 1 | 30000
-- 13 | Headphones | 1 | 3000
-- 21 | Smartphone | 2 | 60000
-- 21 | Pen Set | 3 | 750Q20. Revenue per customer
Show each customer's total spend on delivered orders, highest first. Skip customers with no delivered orders.
SELECT c.name, SUM(oi.quantity * p.price) AS total_spend
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.status = 'Delivered'
GROUP BY c.customer_id, c.name
ORDER BY total_spend DESC;
-- name | total_spend
-- Aman | 140500
-- Bina | 125250
-- Gopal | 60000
-- Imran | 14500
-- Eshan | 7000
-- Falguni | 5400
-- Chetan | 5000
-- Kunal | 2000
-- Divya | 1800Q21. Pairs of customers in the same city (self join)
List every pair of customers who live in the same city. Each pair must appear only once.
SELECT a.name AS customer_1, b.name AS customer_2, a.city
FROM customers a
JOIN customers b
ON a.city = b.city
AND a.customer_id < b.customer_id
ORDER BY a.city, a.customer_id, b.customer_id;
-- Falguni | Imran | Bangalore
-- Aman | Chetan | Delhi
-- Aman | Gopal | Delhi
-- Aman | Kunal | Delhi
-- Chetan | Gopal | Delhi
-- Chetan | Kunal | Delhi
-- Gopal | Kunal | Delhi
-- Bina | Eshan | Mumbai
-- Bina | Jaya | Mumbai
-- Eshan | Jaya | Mumbai
-- Divya | Heena | Pune
-- (11 rows)