MySQL Interview Questions.!!
MySQL Interview Questions.!!
main
53. Retrieve the departments where the total salary expenditure exceeds the
average total salary expenditure across all departments.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING SUM(salary) > (
SELECT AVG(total_salary)
FROM (
SELECT department_id, SUM(salary) as total_salary
FROM employees
GROUP BY department_id
) AS subquery
);
54. Find the employee with the third highest salary without using the LIMIT clause.
Answer:
55. Identify departments that have less than the company-wide median number of
employees.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(id) < (
SELECT AVG(employee_count)
FROM (
SELECT department_id, COUNT(id) as employee_count
FROM employees
GROUP BY department_id
) AS subquery
);
56. Get the most common job title among employees who earn above the company
average.
Answer:
SELECT job_title
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees)
GROUP BY job_title
ORDER BY COUNT(*) DESC
LIMIT 1;
57. Identify employees who earn more than the average salary in both their
department and the company.
Answer:
58. Retrieve the month (in numbers) with the highest total sales from a table of daily
sales.
Answer:
59. Get the department that has the maximum difference between the highest and
lowest salaries.
Answer:
60. Find the employee who earns the median salary in each department.
Answer:
61. Retrieve employees who earn more than their respective department's median
salary.
Answer:
62. Identify the departments where the minimum salary is greater than the maximum
salary of at least one other department.
Answer:
Answer:
64. Identify the department with the most diverse salary distribution, i.e., the largest
difference between the highest and lowest salaries.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING (MAX(salary) - MIN(salary)) = (
SELECT MAX(salary_range)
FROM (
SELECT (MAX(salary) - MIN(salary)) as salary_range
FROM employees
GROUP BY department_id
) AS subquery
);
65. Retrieve the employees who do not have the lowest salary in their department
but earn less than the department average.
Answer:
66. Determine which departments have an average salary close to the company's
median salary. Assume 'close' means a difference of less than 1000.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING ABS(AVG(salary) - (
SELECT AVG(median_salary)
FROM (
SELECT salary AS median_salary
FROM employees
ORDER BY salary
LIMIT 2 - (SELECT COUNT(*) FROM employees) MOD 2
OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees)
) AS median_subquery
)) < 1000;
67. Find the departments where the total number of employees is above the
company's average.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(id) > (
SELECT AVG(employee_count)
FROM (
SELECT COUNT(id) AS employee_count
FROM employees
GROUP BY department_id
) AS avg_subquery
);
68. Identify employees who earn more than the second highest earner in their
respective department.
Answer:
SELECT e1.name, e1.salary, e1.department_id
FROM employees e1
WHERE e1.salary > (
SELECT MAX(e2.salary)
FROM employees e2
WHERE e2.department_id = e1.department_id AND e2.salary < (
SELECT MAX(e3.salary)
FROM employees e3
WHERE e3.department_id = e1.department_id
)
);
69. Find the departments where the top earner makes at least twice as much as the
second top earner.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) >= 2 * (
SELECT MAX(salary)
FROM employees e2
WHERE e2.department_id = employees.department_id AND salary < MAX(emp
);
70. Retrieve the employees who have been in the company for longer than the
average tenure of their respective department managers.
Answer:
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING (AVG(salary) - MIN(salary)) = (
SELECT MIN(gap)
FROM (
SELECT (AVG(salary) - MIN(salary)) AS gap
FROM employees
GROUP BY department_id
) AS gap_subquery
);
**72. Identify the employees who earn below the average salary of their p
**Answer**:
```sql
SELECT e1.name, e1.salary, YEAR(e1.join_date) AS join_year
FROM employees e1
WHERE e1.salary < (
SELECT AVG(e2.salary)
FROM employees e2
WHERE YEAR(e2.join_date) = YEAR(e1.join_date)
);
73. Retrieve the employee who has the closest salary to their department's median
but isn't the median earner.
Answer:
74. Determine the departments whose average tenure (time since joining) is greater
than the company average.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(DATEDIFF(CURDATE(), join_date)) > (
SELECT AVG(DATEDIFF(CURDATE(), join_date))
FROM employees
);
75. Identify departments where more than half of the employees earn above the
company's median salary.
Answer:
SELECT e1.department_id
FROM employees e1
WHERE e1.salary > (
SELECT AVG(median_salary)
FROM (
SELECT salary AS median_salary
FROM employees
ORDER BY salary
LIMIT 2 - (SELECT COUNT(*) FROM employees) MOD 2
OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees)
) AS median_subquery
)
GROUP BY e1.department_id
HAVING COUNT(e1.id) > 0.5 * (
SELECT COUNT(*)
FROM employees e2
WHERE e2.department_id = e1.department_id
);
76. Find employees who earn a salary in the top 3 of their department but are not in
the top 10 company-wide.
Answer:
77. Identify employees whose salary is above the average salary of the two
departments with the highest average salaries.
Answer:
78. Find employees who have a manager earning less than the lowest salary in their
department.
Answer:
79. Identify the department with the least difference between the top earner and the
average salary of the department.
Answer:
SELECT department_id
FROM employees
GROUP BY department_id
HAVING (MAX(salary) - AVG(salary)) = (
SELECT MIN(top_minus_avg)
FROM (
SELECT (MAX(salary) - AVG(salary)) AS top_minus_avg
FROM employees
GROUP BY department_id
) AS difference_subquery
);
80. Retrieve the employees who have the same rank (in terms of salary) in their
department as they do in the company overall.
Answer:
81. Determine the departments where the third-highest earner makes more than
double the department's average salary.
Answer:
SELECT department_id
FROM employees e1
WHERE (
SELECT DISTINCT salary
FROM (
SELECT salary
FROM employees e2
WHERE e2.department_id = e1.department_id
ORDER BY e2.salary DESC
LIMIT 3
) AS third_top_salary_subquery
ORDER BY salary
LIMIT 1 OFFSET 2
) > 2 * (
SELECT AVG(e3.salary)
FROM employees e3
WHERE e3.department_id = e1.department_id
)
GROUP BY department_id;
82. Find employees who have more direct reports (subordinates) than their manager.
Answer:
SELECT e1.name
FROM employees e1
WHERE (
SELECT COUNT(*)
FROM employees e2
WHERE e2.manager_id = e1.id
) > (
SELECT COUNT(*)
FROM employees e3
WHERE e3.manager_id = e1.manager_id
);