๐ฅ SQL Interview Case Studies & Real-World Business Problems
๐ง Case Study 1: Top 3 Customers by Revenue
๐ Orders Table
order_id customer_id amount
1 101 500
2 102 1000
3 101 700
โ Business Question
Find the top 3 customers by total revenue.
โ
Solution
SELECT customer_id,
SUM(amount) AS total_revenue
FROM orders
GROUP BY customer_id
ORDER BY total_revenue DESC
LIMIT 3;
๐ง Case Study 2: Department with Highest Average Salary
โ Business Question
Which department has the highest average salary?
โ
Solution
SELECT department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;
๐ง Case Study 3: Customers Who Never Ordered
๐ Tables
Customers customer_id name
Orders order_id customer_id
โ Business Question
Find customers who never placed an order.
โ
Solution
SELECT c.customer_id,
c.name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;
๐ง Case Study 4: Second Highest Salary
โ Business Question
Find employees with the second highest salary.
โ
Solution
SELECT *
FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary)
FROM employees
)
);
๐ง Case Study 5: Monthly Sales Trend
โ Business Question
Calculate monthly sales.
โ
Solution
SELECT YEAR(order_date) AS year,
MONTH(order_date) AS month,
SUM(amount) AS sales
FROM orders
GROUP BY YEAR(order_date),
MONTH(order_date)
ORDER BY year, month;
๐ฏ Practice Tasks
1๏ธโฃ Find top-selling product
2๏ธโฃ Find employee with highest salary in each department
3๏ธโฃ Find customers with more than 5 orders
4๏ธโฃ Find month with highest sales
5๏ธโฃ Find departments having more than 10 employees
โก Mini Challenge ๐ฅ
E-commerce Scenario
Tables:
Customers customer_id name
Orders order_id customer_id amount order_date
Business Question
Find the top 5 customers by total spending in the last 12 months.
๐ฅ Interview Tip
Most SQL interviews are NOT about syntax.
They're about:
โ
Understanding business problem
โ
Choosing the right approach
โ
Writing efficient SQL
Double Tap โค๏ธ For More
Post #4615
2.46K
- โค 11