0 Comments

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

  1. Break down the problem into sub-queries.
  2. Use commenting and indentation to stay organized.
  3. Practice solving the same question in multiple ways.
  4. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts