Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views6 pages

The-Grand-Complete-Data-Science-Materials: Krishnaik06

The document contains a series of complex SQL interview questions and their corresponding answers, focusing on employee data analysis within a company. It includes queries for identifying salary distributions, department statistics, and employee rankings based on various criteria. The content is structured to assist individuals preparing for SQL interviews, particularly in data science and analytics roles.

Uploaded by

sharath.s
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)
4 views6 pages

The-Grand-Complete-Data-Science-Materials: Krishnaik06

The document contains a series of complex SQL interview questions and their corresponding answers, focusing on employee data analysis within a company. It includes queries for identifying salary distributions, department statistics, and employee rankings based on various criteria. The content is structured to assist individuals preparing for SQL interviews, particularly in data science and analytics roles.

Uploaded by

sharath.s
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/ 6

WHERE e2.salary > e1.

salary
krishnaik06 / The-Grand-Complete-Data-Science-Materials Public );

Code Issues Pull requests 4 Actions Projects Security Insights 55. Identify departments that have less than the company-wide median number of
employees.
The-Grand-Complete-Data-Science-Materials / Complete MySQL Interview Materials
Answer:
/ Complex SQL Interview Questions- Part 2.md

SELECT department_id
krishnaik06 Add files via upload last month FROM employees
GROUP BY department_id
563 lines (459 loc) · 12.9 KB HAVING COUNT(id) < (
SELECT AVG(employee_count)
FROM (
Preview Code Blame Raw SELECT department_id, COUNT(id) as employee_count
FROM employees
GROUP BY department_id
Here are more complex interview questions involving nested subqueries: ) AS subquery
);
53. Retrieve the departments where the total salary expenditure exceeds the average total
salary expenditure across all departments. 56. Get the most common job title among employees who earn above the company
average.
Answer:
Answer:
SELECT department_id
FROM employees
SELECT job_title
GROUP BY department_id
FROM employees
HAVING SUM(salary) > (
WHERE salary > (SELECT AVG(salary) FROM employees)
SELECT AVG(total_salary)
GROUP BY job_title
FROM (
ORDER BY COUNT(*) DESC
SELECT department_id, SUM(salary) as total_salary
LIMIT 1;
FROM employees
GROUP BY department_id
) AS subquery
57. Identify employees who earn more than the average salary in both their department
);
and the company.

54. Find the employee with the third highest salary without using the LIMIT clause. Answer:

Answer: SELECT id, name, salary


FROM employees e1
SELECT name, salary WHERE salary > (
FROM employees e1 SELECT AVG(salary)
WHERE 2 = ( FROM employees
SELECT COUNT(DISTINCT e2.salary) WHERE department_id = e1.department_id
FROM employees e2 )
AND salary > (
SELECT AVG(salary)
FROM employees WHERE e3.department_id = e1.department_id AND e3.salary >= e1.salary
); );

58. Retrieve the month (in numbers) with the highest total sales from a table of daily sales. 61. Retrieve employees who earn more than their respective department's median salary.

Answer: Answer:

SELECT MONTH(date) as sales_month SELECT e1.name, e1.salary, e1.department_id


FROM sales FROM employees e1
GROUP BY MONTH(date) WHERE e1.salary > (
ORDER BY SUM(amount) DESC SELECT AVG(salary)
LIMIT 1; FROM (
SELECT salary
FROM employees e2
59. Get the department that has the maximum difference between the highest and lowest WHERE e2.department_id = e1.department_id
salaries. ORDER BY salary
LIMIT 2 - (SELECT COUNT(*) FROM employees e3 WHERE e3.department_id = e1.
Answer: OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees e4 WHERE e4.department_i
) AS median_subquery
);
SELECT department_id, (MAX(salary) - MIN(salary)) as salary_difference
FROM employees
GROUP BY department_id
HAVING salary_difference = ( 62. Identify the departments where the minimum salary is greater than the maximum
SELECT MAX(max_salary - min_salary)
salary of at least one other department.
FROM (
SELECT department_id, MAX(salary) as max_salary, MIN(salary) as min_salar
Answer:
FROM employees
GROUP BY department_id
) AS subquery SELECT DISTINCT e1.department_id
); FROM employees e1
WHERE e1.salary = (
SELECT MIN(salary)
FROM employees
60. Find the employee who earns the median salary in each department. WHERE department_id = e1.department_id
)
Answer: AND e1.salary > ANY (
SELECT MAX(salary)
FROM employees
SELECT e1.department_id, e1.name, e1.salary
GROUP BY department_id
FROM employees e1
);
WHERE (
SELECT COUNT(*)
FROM employees e2
63. Find employees whose salary ranks in the top 3 within their department.
WHERE e2.department_id = e1.department_id AND e2.salary <= e1.salary
) = (
Answer:
SELECT COUNT(*)
FROM employees e3
SELECT e1.name, e1.salary, e1.department_id
FROM employees e1
WHERE (
SELECT COUNT(DISTINCT e2.salary) SELECT department_id
FROM employees e2 FROM employees
WHERE e2.department_id = e1.department_id AND e2.salary > e1.salary GROUP BY department_id
) < 3; HAVING ABS(AVG(salary) - (
SELECT AVG(median_salary)
FROM (
64. Identify the department with the most diverse salary distribution, i.e., the largest SELECT salary AS median_salary
difference between the highest and lowest salaries. FROM employees
ORDER BY salary
Answer: LIMIT 2 - (SELECT COUNT(*) FROM employees) MOD 2
OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees)
) AS median_subquery
SELECT department_id )) < 1000;
FROM employees
GROUP BY department_id
HAVING (MAX(salary) - MIN(salary)) = ( 67. Find the departments where the total number of employees is above the company's
SELECT MAX(salary_range) average.
FROM (
SELECT (MAX(salary) - MIN(salary)) as salary_range Answer:
FROM employees
GROUP BY department_id
) AS subquery SELECT department_id
); FROM employees
GROUP BY department_id
HAVING COUNT(id) > (
65. Retrieve the employees who do not have the lowest salary in their department but earn SELECT AVG(employee_count)
less than the department average. FROM (
SELECT COUNT(id) AS employee_count
Answer: FROM employees
GROUP BY department_id
) AS avg_subquery
SELECT e1.name, e1.salary, e1.department_id );
FROM employees e1
WHERE e1.salary NOT IN (
SELECT MIN(e2.salary) 68. Identify employees who earn more than the second highest earner in their respective
FROM employees e2 department.
WHERE e2.department_id = e1.department_id
) Answer:
AND e1.salary < (
SELECT AVG(e3.salary)
FROM employees e3 SELECT e1.name, e1.salary, e1.department_id
WHERE e3.department_id = e1.department_id FROM employees e1
); WHERE e1.salary > (
SELECT MAX(e2.salary)
FROM employees e2
66. Determine which departments have an average salary close to the company's median WHERE e2.department_id = e1.department_id AND e2.salary < (
salary. Assume 'close' means a difference of less than 1000. SELECT MAX(e3.salary)
FROM employees e3
Answer: WHERE e3.department_id = e1.department_id
) FROM employees
); GROUP BY department_id
) AS gap_subquery
);
69. Find the departments where the top earner makes at least twice as much as the second
top earner.

Answer: **72. Identify the employees who earn below the average salary of their peers who

**Answer**:
SELECT department_id
FROM employees ```sql
GROUP BY department_id SELECT e1.name, e1.salary, YEAR(e1.join_date) AS join_year
HAVING MAX(salary) >= 2 * ( FROM employees e1
SELECT MAX(salary) WHERE e1.salary < (
FROM employees e2 SELECT AVG(e2.salary)
WHERE e2.department_id = employees.department_id AND salary < MAX(employees.s FROM employees e2
); WHERE YEAR(e2.join_date) = YEAR(e1.join_date)
);

70. Retrieve the employees who have been in the company for longer than the average
tenure of their respective department managers. 73. Retrieve the employee who has the closest salary to their department's median but isn't
the median earner.
Answer:
Answer:
SELECT e1.name, e1.join_date
FROM employees e1 SELECT e1.name, e1.salary
WHERE DATEDIFF(CURDATE(), e1.join_date) > ( FROM employees e1
SELECT AVG(DATEDIFF(CURDATE(), e2.join_date)) WHERE e1.department_id IN (
FROM employees e2 SELECT department_id
WHERE e2.id IN ( FROM employees
SELECT manager_id )
FROM employees AND e1.salary <> (
WHERE department_id = e1.department_id SELECT AVG(median_salary)
) FROM (
); SELECT salary AS median_salary
FROM employees e2
WHERE e2.department_id = e1.department_id
71. Identify the department with the smallest gap between the lowest and average salary.
ORDER BY salary
LIMIT 2 - (SELECT COUNT(*) FROM employees e3 WHERE e3.department_id = e1.
Answer: OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees e4 WHERE e4.department_i
) AS median_subquery
SELECT department_id )
FROM employees ORDER BY ABS(e1.salary - (
GROUP BY department_id SELECT AVG(median_salary)
HAVING (AVG(salary) - MIN(salary)) = ( FROM (
SELECT MIN(gap) SELECT salary AS median_salary
FROM ( FROM employees e5
SELECT (AVG(salary) - MIN(salary)) AS gap WHERE e5.department_id = e1.department_id
ORDER BY salary 76. Find employees who earn a salary in the top 3 of their department but are not in the
LIMIT 2 - (SELECT COUNT(*) FROM employees e6 WHERE e6.department_id = e1.
top 10 company-wide.
OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees e7 WHERE e7.department_i
) AS median_subquery2
Answer:
))
LIMIT 1;
SELECT e1.name, e1.salary, e1.department_id
FROM employees e1
WHERE (
74. Determine the departments whose average tenure (time since joining) is greater than SELECT COUNT(DISTINCT e2.salary)
the company average. FROM employees e2
WHERE e2.department_id = e1.department_id AND e2.salary > e1.salary
Answer: ) < 3
AND e1.salary NOT IN (
SELECT DISTINCT salary
SELECT department_id FROM employees
FROM employees ORDER BY salary DESC
GROUP BY department_id LIMIT 10
HAVING AVG(DATEDIFF(CURDATE(), join_date)) > ( );
SELECT AVG(DATEDIFF(CURDATE(), join_date))
FROM employees
); 77. Identify employees whose salary is above the average salary of the two departments
with the highest average salaries.
75. Identify departments where more than half of the employees earn above the company's
Answer:
median salary.

Answer: SELECT e1.name, e1.salary


FROM employees e1
WHERE e1.salary > (
SELECT e1.department_id SELECT AVG(department_avg)
FROM employees e1 FROM (
WHERE e1.salary > ( SELECT department_id, AVG(salary) AS department_avg
SELECT AVG(median_salary) FROM employees
FROM ( GROUP BY department_id
SELECT salary AS median_salary ORDER BY department_avg DESC
FROM employees LIMIT 2
ORDER BY salary ) AS top_department_subquery
LIMIT 2 - (SELECT COUNT(*) FROM employees) MOD 2 );
OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees)
) AS median_subquery
) 78. Find employees who have a manager earning less than the lowest salary in their
GROUP BY e1.department_id
department.
HAVING COUNT(e1.id) > 0.5 * (
SELECT COUNT(*)
Answer:
FROM employees e2
WHERE e2.department_id = e1.department_id
); SELECT e1.name, e1.salary
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.id
WHERE e2.salary < (
SELECT MIN(e3.salary) WHERE (
FROM employees e3 SELECT DISTINCT salary
WHERE e3.department_id = e1.department_id FROM (
); SELECT salary
FROM employees e2
WHERE e2.department_id = e1.department_id
79. Identify the department with the least difference between the top earner and the ORDER BY e2.salary DESC
average salary of the department. LIMIT 3
) AS third_top_salary_subquery
Answer: ORDER BY salary
LIMIT 1 OFFSET 2
) > 2 * (
SELECT department_id SELECT AVG(e3.salary)
FROM employees FROM employees e3
GROUP BY department_id WHERE e3.department_id = e1.department_id
HAVING (MAX(salary) - AVG(salary)) = ( )
SELECT MIN(top_minus_avg) GROUP BY department_id;
FROM (
SELECT (MAX(salary) - AVG(salary)) AS top_minus_avg
FROM employees 82. Find employees who have more direct reports (subordinates) than their manager.
GROUP BY department_id
) AS difference_subquery Answer:
);

SELECT e1.name
80. Retrieve the employees who have the same rank (in terms of salary) in their department FROM employees e1
as they do in the company overall. WHERE (
SELECT COUNT(*)
Answer: FROM employees e2
WHERE e2.manager_id = e1.id
) > (
SELECT e1.name, e1.salary SELECT COUNT(*)
FROM employees e1 FROM employees e3
WHERE ( WHERE e3.manager_id = e1.manager_id
SELECT COUNT(DISTINCT e2.salary) );
FROM employees e2
WHERE e2.department_id = e1.department_id AND e2.salary > e1.salary
) = (
SELECT COUNT(DISTINCT e3.salary)
FROM employees e3
WHERE e3.salary > e1.salary
);

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

You might also like