Top 70 advanced SQL questions and a
Top 70 advanced SQL questions and a
-----------------------------------------------------------------------------------
---------------------------------------------------------
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
SELECT salary
FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees) AS ranked_salaries
WHERE rank = N;
--3. How do you fetch all employees whose salary is greater than the average
--salary?
SELECT *
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
--4. Write a query to display the current date and time in.
SELECT CURRENT_TIMESTAMP;
SELECT MONTHNAME(CURRENT_DATE());
--7. How to get the common records from two tables?
--9. How do you fetch the top 5 employees with the highest salaries?
--11. How to write a query to find all employees who joined in the year 2020?
--12. Write a query to find employees whose name starts with 'A'.
--13. How can you find the employees who do not have a manager?
--14. How to find the department with the highest number of employees?
16. Write a query to fetch employees having the highest salary in each
department.
17. How to write a query to update the salary of all employees by 10%?
18. How can you find employees whose salary is between 50,000 and
20. How to fetch the first and last record from a table?
21. Write a query to find all employees who report to a specific manager.
22. How can you find the total number of departmentsin the company?
23. How to find the department with the lowest average salary?
25. How to display all employees who have been in the company for more
than 5 years?
27. How to write a query to remove all records from a table but keep the
table structure?
SELECT MONTHNAME(CURRENT_DATE());
31. How to find all employees who do not have any subordinates?
32. Write a query to calculate the totalsales per customer in a sales table.
34. How to find the second highest salary for each department?
37. How to write a query to find the total number of employees in each job
title?
38. Write a query to fetch all employees whose names end with ‘n’.
39. How to find all employees who work in both departments 101 and 102?
40. Write a query to fetch the details of employees with the same salary.
46. How to find employees who joined the company in the same month and
year as their manager?
47. Write a query to count the number of employees whose names start and
end with the same letter.
49. How to find employees whose salary is higher than their managers
5O. Write a query to get employees who belong to departments with less
than 3 employees.
51. How to write a query to find employees with the same first name?
52. How to write a query to delete employees who have been in the company
for more than 15 years?
53. Write a query to list all employees working under the same manager.
55. Write a query to list all employees with more than 5 years of experience
in each department.
56. How to list all employees in departments that have not hired anyone in
the past 2 years?
57. Write a query to find all employees who earn more than the average
salary of their department.
58. How to list all managers who have more than 5 subordinates?
59. Write a query to display employee names and hire dates in the format
"Name - MM/DD/YYYY".
60. How to find employees whose salary isin the top 10%?
62. How to find the average salary of the top 5 highest-paid employees in
each department?
64. Write a query to find all employees whose email contains the domain
'@example.com'.
67. How to find all employees who are older than 30 years?
68. Write a query to display employees grouped by their salary range (e.g., 0-
20K, 20K-50K).
70. Write a query to display the highest, lowest, and average salary for each
job role.