Advanced SQL Questions and Answers
Advanced SQL Questions and Answers
🔗 Links:
Instagram: https://www.instagram.com/rebellionrider
YouTube: https://www.youtube.com/@Rebellionrider
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);
SELECT CURRENT_TIMESTAMP;
WITH CTE AS (
SELECT column_name,
ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY
column_name) AS row_num FROM table_name
)
DELETE FROM CTE WHERE row_num > 1;
SELECT *
FROM table1 INTERSECT SELECT *
FROM table2;
SELECT *
FROM employees
ORDER BY employee_id DESC LIMIT 10;
9. How do you fetch the top 5 employees with the highest salaries?
SELECT *
FROM employees ORDER BY salary DESC LIMIT 5;
11. How to write a query to find all employees who joined in the year
2020?
SELECT *
FROM employees
WHERE YEAR(join_date) = 2020;
12. Write a query to find employees whose name starts with 'A'.
SELECT *
FROM employees WHERE name LIKE 'A%';
13. How can you find the employees who do not have a manager?
SELECT *
FROM employees
WHERE manager_id IS NULL;
14. How to find the department with the highest number of employees?
17. How to write a query to update the salary of all employees by 10%?
UPDATE employees
SET salary = salary * 1.1;
18. How can you find employees whose salary is between 50,000 and
1,00,000?
SELECT *
FROM employees
WHERE salary BETWEEN 50000 AND 100000;
SELECT *
FROM employees
ORDER BY birth_date DESC LIMIT 1;
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 departments in 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?
SELECT *
FROM employees
WHERE DATEDIFF(CURDATE(), join_date) > 2020;
SELECT LOWER('STRING_VALUE');
31. How to find all employees who do not have any subordinates?
SELECT *
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees WHERE
manager_id IS NOT NULL);
32. Write a query to calculate the total sales per customer in a sales table.
SELECT CASE
WHEN EXISTS (SELECT 1 FROM table_name) THEN 'Not Empty'
ELSE 'Empty' END;
SELECT *
FROM employees
WHERE salary % 10000 = 0;
SELECT *
FROM employees
WHERE column_name IS NULL;
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’.
SELECT *
FROM employees WHERE name LIKE '%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.
SELECT *
FROM employees
WHERE salary IN (SELECT salary
FROM employees GROUP BY salary HAVING COUNT(*) > 1);
SELECT *
FROM employees
WHERE department_id IS NULL;
43. Write a query to find the maximum salary and minimum salary in each
department.
SELECT *
FROM employees
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.
SELECT COUNT(*)
FROM employees
WHERE LEFT(name, 1) = RIGHT(name, 1);
49. How to find employees whose salary is higher than their manager's
salary?
SELECT *
FROM employees
WHERE department_id IN (SELECT department_id FROM employees
GROUP BY department_id HAVING COUNT(*) < 3);
51. How to write a query to find employees with the same first name?
SELECT *
FROM employees
WHERE first_name IN (SELECT first_name FROM employees
GROUP BY first_name HAVING COUNT(*) > 1);
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.
SELECT *
FROM employees WHERE manager_id = ?;
SELECT *
FROM (SELECT *,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary
DESC) AS rank
FROM employees) AS ranked_employees WHERE rank <= 3;
SELECT *
FROM employees
WHERE DATEDIFF(CURDATE(), join_date) > 1825;
56. How to list all employees in departments that have not hired anyone in
the past 2 years?
SELECT *
FROM employees
WHERE department_id IN (SELECT department_id FROM employees
GROUP BY department_id
HAVING MAX(hire_date) < ADDDATE(CURDATE(), INTERVAL -2
YEAR));
57. Write a query to find all employees who earn more than the average
salary of their department.
SELECT *
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees
WHERE department_id = e.department_id);
58. How to list all managers who have more than 5 subordinates?
SELECT *
FROM employees
WHERE employee_id IN (SELECT manager_id FROM employees
GROUP BY manager_id HAVING COUNT(*) > 5);
59. Write a query to display employee names and hire dates in the format
"Name - MM/DD/YYYY".
SELECT *
FROM employees
WHERE salary >= (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER
BY salary ASC)
FROM employees);
SELECT CASE
WHEN age BETWEEN 20 AND 30 THEN '20-30'
WHEN age BETWEEN 31 AND 40 THEN '31-40'
ELSE '41+'
END AS age_bracket, COUNT(*)
FROM employees GROUP BY age_bracket;
62. How to find the average salary of the top 5 highest-paid employees in
each department?
SELECT department_id,
(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM employees)) AS
percentage FROM employees
GROUP BY department_id;
SELECT *
FROM employees
WHERE email LIKE '%@example.com';
66. Write a query to display the hire date and day of the week for each
employee.
SELECT *
FROM employees
WHERE DATEDIFF(CURDATE(), birth_date) / 365 > 30;
68. Write a query to display employees grouped by their salary range (e.g.,
0- 20K, 20K-50K).
SELECT CASE
WHEN salary BETWEEN 0 AND 20000 THEN '0-20K'
WHEN salary BETWEEN 20001 AND 50000 THEN '20K-50K' ELSE '50K+'
END AS salary_range, COUNT(*)
FROM employees GROUP BY salary_range;
70. Write a query to display the highest, lowest, and average salary for
each job role.