SQL Queries
SQL Queries
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
2. How to get the nth highest salary in ?
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;
5. How to find duplicate records in a table?
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
6. How can you delete duplicate rows in ?
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;
7. How to get the common records from two tables?
SELECT *
FROM table1
INTERSECT
SELECT *
FROM table2;
8. How to retrieve the last 10 records from a table?
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;
10. How to calculate the total salary of all employees?
SELECT SUM(salary)
FROM employees;
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?
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;
19. How to find the youngest employee in the organization?
SELECT *
FROM employees
ORDER BY birth_date DESC
LIMIT 1;
20. How to fetch the first and last record from a table?
SELECT *
FROM employees
WHERE manager_id = ?;
22. How can you find the total number of departments in the company?
SELECT *
FROM employees
WHERE DATEDIFF(CURDATE(), join_date) > 1825;
26. How to find the second-largest value from a table?
SELECT MAX(column_name)
FROM table_name
WHERE column_name < (SELECT MAX(column_name) FROM table_name);
27. How to write a query to remove all records from a table but keep the
table structure?
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;
34. How to find the second highest salary for each department?
SELECT department_id, salary
FROM (SELECT department_id, salary,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary
DESC) AS rank
FROM employees) AS ranked_salaries
WHERE rank = 2;
35. Write a query to fetch employees whose salary is a multiple of 10,000.
SELECT *
FROM employees
WHERE salary % 10000 = 0;
36. How to fetch records where a column has null values?
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?
SELECT *
FROM employees
WHERE name LIKE '%n';
39. How to find all employees who work in both departments 101 and 102?
SELECT employee_id
FROM employees
WHERE department_id IN (101, 102)
GROUP BY employee_id
HAVING COUNT(DISTINCT department_id) = 2;
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);
41. How to update salaries of employees based on their department?
UPDATE employees
SET salary = CASE
WHEN department_id = 101 THEN salary * 1.10
WHEN department_id = 102 THEN salary * 1.05
ELSE salary
END;
42. How to write a query to list all employees without a department?
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
WHERE hire_date > ADDDATE(CURDATE(), INTERVAL -6 MONTH);
45. Write a query to display department-wise total and average salary.
SELECT department_id, SUM(salary) AS total_salary, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id;
46. How to find employees who joined the company in the same month and
year as their manager?
SELECT COUNT(*)
FROM employees
WHERE LEFT(name, 1) = RIGHT(name, 1);
48. How to retrieve employee names and salaries in a single string?
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?
DELETE FROM employees
WHERE DATEDIFF(CURDATE(), join_date) > 5475;
53. Write a query to list all employees working under the same manager.
SELECT *
FROM employees
WHERE manager_id = ?;
54. How to find the top 3 highest-paid employees in each department?
SELECT *
FROM (SELECT *,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary
DESC) AS rank
FROM employees) AS ranked_employees
WHERE rank <= 3;
55. Write a query to list all employees with more than 5 years of experience
in each department.
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);
61. Write a query to display employees grouped by their age brackets (e.g.,
20-30, 31-40, etc.).
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;
64. Write a query to find all employees whose email contains the domain
'@example.com'.
SELECT *
FROM employees
WHERE email LIKE '%@example.com';
65. How to retrieve the year-to-date sales for each customer?
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;
69. How to list all employees who do not have a bonus?
SELECT *
FROM employees
WHERE bonus IS NULL;
70. Write a query to display the highest, lowest, and average salary for each
job role.