Top 7 SQL Interview Questions Asked at MAANG Companies
If you're targeting companies like Meta, Amazon, Apple, Netflix, and Google — SQL is not just about writing queries.
It’s about thinking in data, optimizing logic, and solving real-world problems.
Here are 7 must-know SQL questions (with detailed explanations) 👇
1️⃣ Find the 2nd Highest Salary
💡 Problem:
Find the second highest salary from an employee table.
✅ Solution:
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
🧠 Why this matters:
- Tests understanding of subqueries
-
Alternative approaches:
DENSE_RANK(),LIMIT/OFFSET
2️⃣ Find Duplicate Records
💡 Problem:
Identify duplicate rows based on email.
✅ Solution:
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
🧠 What interviewer checks:
- Understanding of GROUP BY + HAVING
- Real-world use: fraud detection, data cleaning
3️⃣ Get Top N Records Per Group
💡 Problem:
Find top 3 highest-paid employees per department.
✅ Solution:
SELECT *
FROM (
SELECT *,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk <= 3;
🧠 Why it’s important:
- Tests window functions
- Very common in Amazon & Meta interviews
4️⃣ Calculate Running Total
💡 Problem:
Find cumulative sales over time.
✅ Solution:
SELECT date,
SUM(sales) OVER (ORDER BY date) AS running_total
FROM sales;
🧠 What this shows:
- Understanding of analytical functions
- Used in dashboards and reporting
5️⃣ Find Missing Records (Gaps in Data)
💡 Problem:
Find missing IDs in a sequence.
✅ Solution:
SELECT t1.id + 1 AS missing_id
FROM table1 t1
LEFT JOIN table1 t2
ON t1.id + 1 = t2.id
WHERE t2.id IS NULL;
🧠 Why it matters:
- Tests joins + logical thinking
- Common in data integrity checks
6️⃣ Customers Who Never Ordered
💡 Problem:
Find customers who have never placed an order.
✅ Solution:
SELECT c.customer_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;
🧠 Interview focus:
- Understanding of LEFT JOIN + NULL filtering
- Real business use case
7️⃣ Find Median Salary
💡 Problem:
Calculate median salary (tricky in SQL).
✅ Solution:
SELECT AVG(salary) AS median
FROM (
SELECT salary,
ROW_NUMBER() OVER (ORDER BY salary) AS rn,
COUNT(*) OVER () AS total
FROM employees
) t
WHERE rn IN (FLOOR((total+1)/2), CEIL((total+1)/2));
🧠 Why this is advanced:
- Combines window functions + math
- Often asked in Google interviews
Comments
Post a Comment