In competitive technical interviews, standard SQL knowledge isnβt enough β you must be able to handle tricky SQL queries that test your problem-solving and logic-building skills.
This guide covers the most commonly asked and tricky SQL queries for interview preparation, ideal for both freshers and experienced candidates aiming to crack placement tests, MNC interviews, and analytics roles.
π Why Focus on Tricky SQL Queries for Interviews?
Companies like TCS, Infosys, Wipro, Amazon, and Google ask tricky SQL problems to assess how well you think with data. These often involve pattern matching, subqueries, window functions, and data grouping.
β Top Tricky SQL Queries for Interview
1. Find the second highest salary in a table
EditSELECT MAX(Salary)
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
Why it’s tricky: Requires using a nested query and thinking beyond
ORDER BY
.
2. Get employee(s) with the same salary
EditSELECT Salary
FROM Employee
GROUP BY Salary
HAVING COUNT(*) > 1;
3. Find duplicate records in a table
EditSELECT Name, COUNT(*)
FROM Employee
GROUP BY Name
HAVING COUNT(*) > 1;
4. Find employees with the top 3 salaries
EditSELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 3;
5. Fetch department-wise highest salary
EditSELECT Department, MAX(Salary) AS MaxSalary
FROM Employee
GROUP BY Department;
6. Find employees who never had a manager
EditSELECT *
FROM Employee
WHERE ManagerID IS NULL;
7. List employees who joined in the last 90 days
EditSELECT *
FROM Employee
WHERE JoinDate >= DATE_SUB(CURDATE(), INTERVAL 90 DAY);
8. Find the nth highest salary
EditSELECT DISTINCT Salary
FROM Employee e1
WHERE N - 1 = (
SELECT COUNT(DISTINCT Salary)
FROM Employee e2
WHERE e2.Salary > e1.Salary
);
9. Select alternate rows from a table
EditSELECT *
FROM (
SELECT *, ROW_NUMBER() OVER () AS RowNum
FROM Employee
) AS temp
WHERE RowNum % 2 = 1;
10. Find employees whose names start and end with the same letter
SELECT *
FROM Employee
WHERE LEFT(Name, 1) = RIGHT(Name, 1);
11. Retrieve all managers (employees who are also managers of others)
SELECT DISTINCT ManagerID
FROM Employee
WHERE ManagerID IS NOT NULL;
12. Detect gaps in a sequence
SELECT ID + 1 AS MissingID
FROM Employee e
WHERE NOT EXISTS (
SELECT 1 FROM Employee WHERE ID = e.ID + 1
);
13. Get all employees except top 5 earners
SELECT *
FROM Employee
ORDER BY Salary DESC
LIMIT 100000 OFFSET 5;
14. Find employees working in more than one department
SELECT EmpID
FROM EmpDept
GROUP BY EmpID
HAVING COUNT(DISTINCT DeptID) > 1;
15. Get running total of salaries
SELECT Name, Salary,
SUM(Salary) OVER (ORDER BY ID) AS RunningTotal
FROM Employee;
π§ Expert Tips to Tackle Tricky SQL Queries for Interviews
- Break down the problem into sub-queries.
- Use commenting and indentation to stay organized.
- Practice solving the same question in multiple ways.
- Master window functions,
JOIN
,GROUP BY
, and correlated subqueries.
β FAQs β Tricky SQL Queries for Interview
Q1: Are these tricky SQL queries asked in real interviews?
Yes, these are based on actual interview questions from TCS, Infosys, Wipro, Amazon, etc.
Q2: Do I need to know advanced SQL for these?
Many of these queries can be solved with intermediate knowledge if you understand the problem well.
Q3: Where can I practice tricky SQL queries?
You can use online SQL editors like:
- SQLZoo
- LeetCode (Database Section)
- Mode Analytics SQL Editor
- HackerRank SQL Challenges
π Final Words
Cracking SQL interviews isn’t about memorizing queries. It’s about understanding patterns and building query logic. By practicing these tricky SQL queries for interview, you’re preparing to think like a real data professional.
π Bookmark this blog