Most Asked SQL Queries in Interviews (With Logic & Best Practices)

Table of contents
- 1️⃣ Get the 2nd Highest Salary
- 2️⃣ Find Duplicate Records
- 3️⃣ Count Employees Per Department
- 4️⃣ Employees Who Didn't Submit Reports
- 5️⃣ Customers With More Than 2 Orders
- 6️⃣ Top 3 Highest-Paid Employees
- 7️⃣ Join Orders With Customers
- 8️⃣ Orders From the Last 7 Days
- 9️⃣ Users Without Phone Numbers
- 🔟 Departments With Avg Salary > 50K
- 1️⃣1️⃣ Employees and Their Managers (Self Join)
- 1️⃣2️⃣ Customers With No Orders
- 1️⃣3️⃣ Month-Wise Sales Summary
- 1️⃣4️⃣ Update NULL City to Default
- 1️⃣5️⃣ Delete Duplicate Rows (Keep One)
- 💡 Pro Interview Tips

SQL interviews don’t require 100+ queries — just 25–30 practical ones that reflect real-world scenarios. But don’t just memorize them — understand the logic behind each.
This guide covers the most commonly asked SQL queries in technical interviews for roles like backend developers, data analysts, and full-stack engineers.
1️⃣ Get the 2nd Highest Salary
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
📌 Logic: Get the maximum salary that is less than the highest.
2️⃣ Find Duplicate Records
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
📌 Logic: Group by the field (e.g. email), then filter groups that occur more than once.
3️⃣ Count Employees Per Department
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
4️⃣ Employees Who Didn't Submit Reports
SELECT name
FROM employees
WHERE id NOT IN (SELECT employee_id FROM reports);
📌 Logic: Use NOT IN
with a subquery to filter missing entries.
5️⃣ Customers With More Than 2 Orders
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 2;
6️⃣ Top 3 Highest-Paid Employees
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 3;
7️⃣ Join Orders With Customers
SELECT o.id, o.date, c.name
FROM orders o
JOIN customers c ON o.customer_id = c.id;
📌 Logic: Use JOIN
to fetch customer names for each order.
8️⃣ Orders From the Last 7 Days
SELECT *
FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL 7 DAY;
9️⃣ Users Without Phone Numbers
SELECT *
FROM users
WHERE phone IS NULL;
🔟 Departments With Avg Salary > 50K
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
📌 Tip: Use HAVING
with aggregates like AVG()
.
1️⃣1️⃣ Employees and Their Managers (Self Join)
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;
1️⃣2️⃣ Customers With No Orders
SELECT c.name
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
WHERE o.id IS NULL;
1️⃣3️⃣ Month-Wise Sales Summary
SELECT MONTH(order_date) AS month, SUM(amount) AS total_sales
FROM orders
GROUP BY MONTH(order_date);
1️⃣4️⃣ Update NULL City to Default
UPDATE users
SET city = 'Unknown'
WHERE city IS NULL;
1️⃣5️⃣ Delete Duplicate Rows (Keep One)
DELETE FROM users
WHERE id NOT IN (
SELECT MIN(id)
FROM users
GROUP BY email
);
📌 Logic: Keep the record with the minimum ID per group (e.g. email).
💡 Pro Interview Tips
✅ Explain your logic out loud:
"First we JOIN the tables, then FILTER the results, and finally AGGREGATE based on the group..."
✅ Avoid SELECT *
— Always select only needed columns
✅ Use aliases for clarity (e
, o
, c
, etc.)
✅ Ask clarifying questions if unsure — interviews are collaborative
Mastering SQL isn’t about memorizing syntax — it’s about understanding how data flows, how to query with intent, and how to communicate your logic clearly.
Practice these queries
Save this article
Share it with your friends preparing for SQL interviews
👉 Bookmark this blog for quick revision before your next interview!
👉 Found this helpful? Follow DevDaily for more developer blogs every week!
✍ Written by Rishabh Mishra
Subscribe to my newsletter
Read articles from Rishabh Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Rishabh Mishra
Rishabh Mishra
Hey, I’m Rishabh — a developer who writes code like poetry and breaks things just to rebuild them better. .NET Full Stack Dev | Razor, C#, MVC, SQL, Angular — my daily playground. I believe in “learning out loud” — so I write about dev struggles, breakthroughs, and the weird bugs that teach the best lessons. From building ERP apps to tinkering with UI/UX — I turn business logic into beautiful experiences. Self-growth > Comfort zone | Debugging is my meditation Let’s turn curiosity into code — one blog at a time.