TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #3246 68
๐Ÿ”ฅ SQL Interview Case Studies (Advanced Business Scenarios) ๐Ÿ’ฏ

๐Ÿง  Case Study 1: Find Repeat Customers
๐Ÿ“Š Orders Table
order_id customer_id
1 101
2 102
3 101

โ“ Business Question
Find customers who placed more than 1 order.

โœ… Solution
SELECT customer_id,
COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 1;

๐Ÿง  Case Study 2: Highest Paid Employee Per Department
โ“ Business Question
Find highest paid employee in every department.

โœ… Solution
WITH RankedEmployees AS (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY department
ORDER BY salary DESC
) rn
FROM employees
)
SELECT *
FROM RankedEmployees
WHERE rn = 1;

๐Ÿง  Case Study 3: Find Inactive Customers
โ“ Business Question
Customers who haven't ordered in the last 6 months.

โœ… Solution
SELECT c.customer_id,
c.name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.name
HAVING MAX(o.order_date) <
DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

๐Ÿง  Case Study 4: Product Generating Highest Revenue
๐Ÿ“Š Sales Table
product_id amount

โœ… Solution
SELECT product_id,
SUM(amount) AS revenue
FROM sales
GROUP BY product_id
ORDER BY revenue DESC
LIMIT 1;

๐Ÿง  Case Study 5: Employee Salary Above Department Average

โœ… Solution
WITH DeptAvg AS (
SELECT department,
AVG(salary) avg_salary
FROM employees
GROUP BY department
)

SELECT e.*
FROM employees e
JOIN DeptAvg d
ON e.department = d.department
WHERE e.salary > d.avg_salary;

๐Ÿง  Case Study 6: Running Total Sales
โ“ Business Question
Calculate cumulative sales.

โœ… Solution
SELECT order_date,
amount,
SUM(amount) OVER(
ORDER BY order_date
) AS running_total
FROM sales;

๐Ÿง  Case Study 7: Top 3 Products Per Category

โœ… Solution
WITH RankedProducts AS (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY category
ORDER BY sales DESC
) rn
FROM products
)
SELECT *
FROM RankedProducts
WHERE rn <= 3;

๐ŸŽฏ Practice Tasks
1๏ธโƒฃ Find customer with highest number of orders
2๏ธโƒฃ Find lowest salary employee per department
3๏ธโƒฃ Find products never sold
4๏ธโƒฃ Find departments with average salary > company average
5๏ธโƒฃ Find month with lowest sales

โšก Mini Challenge ๐Ÿ”ฅ
Banking Scenario
Tables:
Accounts
account_id customer_name

Transactions
transaction_id account_id amount transaction_date

Question
๐Ÿ‘‰ Find the top 3 customers with highest total transaction amount in the last 1 year.

๐Ÿ”ฅ Interview Tip
When solving case studies:
1๏ธโƒฃ Understand business question
2๏ธโƒฃ Identify tables
3๏ธโƒฃ Identify JOINs
4๏ธโƒฃ Identify Aggregations
5๏ธโƒฃ Decide whether Window Function is needed

Double Tap โค๏ธ For More
  • โค 1
More from @crackingthecodinginterview
  1. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  2. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  3. Oct 7, 2026๐Ÿš€ DSA Topics Every Programmer Should Know ๐Ÿ’ป๐Ÿ”ฅ ๐Ÿ“ฆ 1. Arrays โœ” Traversal โœ” Searching โœ” Sorโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  6. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’