0% found this document useful (0 votes)
10 views9 pages

Conference Presentation Resume Tips

The document provides a comprehensive list of frequently asked SQL queries in interviews, covering various topics such as finding salaries, identifying duplicates, and analyzing employee data. It includes both basic and advanced queries, with examples for each scenario. The queries focus on operations like joins, aggregations, and ranking, making it a useful resource for interview preparation.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Conference Presentation Resume Tips

The document provides a comprehensive list of frequently asked SQL queries in interviews, covering various topics such as finding salaries, identifying duplicates, and analyzing employee data. It includes both basic and advanced queries, with examples for each scenario. The queries focus on operations like joins, aggregations, and ranking, making it a useful resource for interview preparation.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

30/03/2025, 19:38 Conference Presentation Resume Tips

Here are some frequently asked SQL queries in interviews:

1. Find the Second Highest Salary


sql Copy Edit

SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT


MAX(salary) FROM employees);

2. Find the Nth Highest Salary

sql Copy Edit

SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET (N-1);

3. Find Duplicate Records in a Table

sql Copy Edit

SELECT name, COUNT(*) FROM employees GROUP BY name HAVING COUNT(*) > 1;

4. Find Employees Who Earn More Than Their Manager


sql Copy Edit

SELECT e.name, e.salary, m.salary AS manager_salary FROM employees e JOIN employees m ON


e.manager_id = m.id WHERE e.salary > m.salary;

5. Find Employees Hired in the Last 6 Months

sql Copy Edit

SELECT * FROM employees WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

6. Find Employees with the Highest Salary in Each Department

sql Copy Edit

SELECT department_id, name, salary FROM employees e WHERE salary = (SELECT MAX(salary) FROM
employees WHERE department_id = e.department_id);

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 1/9
30/03/2025, 19:38 Conference Presentation Resume Tips

7. Get the Department Name Along with Employee Count

sql Copy Edit

SELECT d.department_name, COUNT(e.id) AS employee_count FROM employees e JOIN departments d


ON e.department_id = d.id GROUP BY d.department_name;

8. Delete Duplicate Records (Keep Only One)

sql Copy Edit

DELETE FROM employees WHERE id NOT IN ( SELECT MIN(id) FROM employees GROUP BY name,
salary, department_id );

9. Find Employees Who Don’t Have a Manager


sql Copy Edit

SELECT * FROM employees WHERE manager_id IS NULL;

10. Get Employees Who Have the Same Salary


sql Copy Edit

SELECT e1.name AS emp1, e2.name AS emp2, e1.salary FROM employees e1, employees e2 WHERE
e1.salary = e2.salary AND e1.id <> e2.id;

These are some of the most commonly asked SQL queries in interviews. Let me know if you need more!
🚀

Compare with Claude

yes i need

Here are more frequently asked SQL queries in interviews:

11. Find the Third Highest Salary


sql Copy Edit

SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 2;

OR using DENSE_RANK() :

sql Copy Edit

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 2/9
30/03/2025, 19:38 Conference Presentation Resume Tips

SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rnk FROM
employees ) temp WHERE rnk = 3;

12. Find Employees Who Have a Higher Salary Than the Average Salary
sql Copy Edit

SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

13. Find Common Employees Between Two Tables


sql Copy Edit

SELECT name FROM employees_2023 INTERSECT SELECT name FROM employees_2024;

OR using JOIN:

sql Copy Edit

SELECT e1.name FROM employees_2023 e1 JOIN employees_2024 e2 ON e1.name = e2.name;

14. Find Employees Who Joined on the Same Date

sql Copy Edit

SELECT hire_date, COUNT(*) AS count FROM employees GROUP BY hire_date HAVING COUNT(*) > 1;

15. Retrieve the First and Last Record in a Table


sql Copy Edit

-- First record SELECT * FROM employees ORDER BY id ASC LIMIT 1; -- Last record SELECT *
FROM employees ORDER BY id DESC LIMIT 1;

16. Find the Second Most Repeated Department

sql Copy Edit

SELECT department_id, COUNT(*) AS department_count FROM employees GROUP BY department_id


ORDER BY department_count DESC LIMIT 1 OFFSET 1;

17. Find All Employees Who Don't Have a Department

sql Copy Edit

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 3/9
30/03/2025, 19:38 Conference Presentation Resume Tips

SELECT * FROM employees WHERE department_id IS NULL;

18. Find Employees Who Have Worked for More Than 5 Years
sql Copy Edit

SELECT * FROM employees WHERE DATEDIFF(CURDATE(), hire_date) / 365 > 5;

19. Display Employee Names in a Single Comma-Separated String

sql Copy Edit

SELECT GROUP_CONCAT(name SEPARATOR ', ') AS employee_names FROM employees;

20. Swap Two Column Values Without Using a Temp Variable

sql Copy Edit

UPDATE employees SET col1 = col1 + col2, col2 = col1 - col2, col1 = col1 - col2;

21. Retrieve the Last 5 Records from a Table

sql Copy Edit

SELECT * FROM employees ORDER BY id DESC LIMIT 5;

22. Show Employees With a Salary Between 50,000 and 100,000

sql Copy Edit

SELECT * FROM employees WHERE salary BETWEEN 50000 AND 100000;

23. Get the Top 5 Highest Salaries Without Using LIMIT

sql Copy Edit

SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM
employees ) ranked WHERE rnk <= 5;

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 4/9
30/03/2025, 19:38 Conference Presentation Resume Tips

24. Find Employees Who Have No Leaves This Year

sql Copy Edit

SELECT * FROM employees e LEFT JOIN leaves l ON e.id = l.employee_id AND YEAR(l.leave_date)
= YEAR(CURDATE()) WHERE l.employee_id IS NULL;

25. Find Employees Who Have the Same Salary as Someone Else

sql Copy Edit

SELECT e1.name, e1.salary FROM employees e1 JOIN employees e2 ON e1.salary = e2.salary AND
e1.id <> e2.id;

These are some of the most commonly asked SQL queries in interviews.
Let me know if you need more advanced queries or explanations! 🚀

Compare with Claude

yes give me

Here are more advanced SQL queries frequently asked in interviews:

26. Find the Employee(s) with the Maximum Salary in Each Department

sql Copy Edit

SELECT e.department_id, e.name, e.salary FROM employees e WHERE salary = ( SELECT


MAX(salary) FROM employees WHERE department_id = e.department_id );

27. Find Employees Who Have More Than One Manager

sql Copy Edit

SELECT employee_id, COUNT(manager_id) AS manager_count FROM employees GROUP BY employee_id


HAVING COUNT(manager_id) > 1;

28. Find Departments with More Than 5 Employees

sql Copy Edit

SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id


HAVING COUNT(*) > 5;

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 5/9
30/03/2025, 19:38 Conference Presentation Resume Tips

29. Get the Running Total of Salaries

sql Copy Edit

SELECT name, salary, SUM(salary) OVER (ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND
CURRENT ROW) AS running_total FROM employees;

30. Find the Cumulative Sum of Employees Joining Each Year


sql Copy Edit

SELECT hire_year, COUNT(*) AS num_employees, SUM(COUNT(*)) OVER (ORDER BY hire_year) AS


cumulative_sum FROM ( SELECT YEAR(hire_date) AS hire_year FROM employees ) sub GROUP BY
hire_year;

31. Find Employees Without Any Direct Reports

sql Copy Edit

SELECT * FROM employees e WHERE NOT EXISTS ( SELECT 1 FROM employees WHERE manager_id =
e.employee_id );

32. Find Employees Who Have the Same Name

sql Copy Edit

SELECT name, COUNT(*) AS name_count FROM employees GROUP BY name HAVING COUNT(*) > 1;

33. Find the Last Login Date for Each Employee

sql Copy Edit

SELECT employee_id, MAX(login_time) AS last_login FROM logins GROUP BY employee_id;

34. Find Employees Who Were Hired in the Last 6 Months

sql Copy Edit

SELECT * FROM employees WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

35. Get the Employee(s) with the Second Highest Salary in Each Department

sql Copy Edit

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 6/9
30/03/2025, 19:38 Conference Presentation Resume Tips

SELECT department_id, name, salary FROM ( SELECT department_id, name, salary, DENSE_RANK()
OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank FROM employees ) ranked
WHERE rank = 2;

36. Find the Difference Between the Highest and Lowest Salary

sql Copy Edit

SELECT MAX(salary) - MIN(salary) AS salary_difference FROM employees;

37. Count Employees Who Were Hired Each Year

sql Copy Edit

SELECT YEAR(hire_date) AS hire_year, COUNT(*) AS employee_count FROM employees GROUP BY


hire_year ORDER BY hire_year;

38. Find the Department with the Highest Average Salary

sql Copy Edit

SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ORDER


BY avg_salary DESC LIMIT 1;

39. Find All Employees Who Report to a Given Manager


sql Copy Edit

SELECT * FROM employees WHERE manager_id = (SELECT employee_id FROM employees WHERE name =
'John Doe');

40. Get the Rank of Each Employee by Salary

sql Copy Edit

SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees;

41. Find Employees Who Have Been Paid More Than Their Manager

sql Copy Edit

SELECT e1.name AS employee, e1.salary, e2.name AS manager, e2.salary AS manager_salary FROM


employees e1 JOIN employees e2 ON e1.manager_id = e2.employee_id WHERE e1.salary >
e2.salary;

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 7/9
30/03/2025, 19:38 Conference Presentation Resume Tips

42. Get the Names of Employees Who Have Never Been Promoted

sql Copy Edit

SELECT e.name FROM employees e LEFT JOIN promotions p ON e.employee_id = p.employee_id


WHERE p.employee_id IS NULL;

43. Find Employees Whose Name Starts and Ends with a Specific Letter

sql Copy Edit

SELECT * FROM employees WHERE name LIKE 'A%A';

44. Find Employees Who Are in the Same Department as Their Manager

sql Copy Edit

SELECT e.name AS employee, e.department_id, m.name AS manager FROM employees e JOIN


employees m ON e.manager_id = m.employee_id WHERE e.department_id = m.department_id;

45. Retrieve Employees Who Have at Least Two Direct Reports


sql Copy Edit

SELECT manager_id, COUNT(*) AS direct_reports FROM employees GROUP BY manager_id HAVING


COUNT(*) >= 2;

46. Find Employees Who Have Taken the Most Leaves

sql Copy Edit

SELECT employee_id, COUNT(*) AS leave_count FROM leaves GROUP BY employee_id ORDER BY


leave_count DESC LIMIT 1;

47. Find Employees Who Have a Salary Within 10% of the Highest Salary

sql Copy Edit

SELECT * FROM employees WHERE salary >= (SELECT MAX(salary) * 0.9 FROM employees);

48. Retrieve Employees Who Have Worked on More Than One Project

sql

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 8/9
30/03/2025, 19:38 Conference Presentation Resume Tips

Copy Edit
SELECT employee_id, COUNT(DISTINCT project_id) AS project_count FROM employee_projects
GROUP BY employee_id HAVING COUNT(DISTINCT project_id) > 1;

49. Find Departments That Have No Employees


sql Copy Edit

SELECT d.department_id, d.name FROM departments d LEFT JOIN employees e ON d.department_id


= e.department_id WHERE e.employee_id IS NULL;

50. Find the Most Common Job Title in a Company

sql Copy Edit

SELECT job_title, COUNT(*) AS count FROM employees GROUP BY job_title ORDER BY count DESC
LIMIT 1;

These advanced SQL queries cover joins, ranking, analytics, subqueries, aggregations, and
optimization techniques often tested in interviews. 🚀

https://chatgpt.com/c/67e4caae-14e8-8010-96ab-03c43ec58566 9/9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy