SQL solutions
SQL solutions
In this series, we will tackle 110 exciting SQL questions based on a simple EMP
table associated with 3 other tables.
What to Expect:
1) Display the details of those managers who do not have any person
working under them.
2) Display the details of those employees who are in the sales department
and have a grade of 3.
3) Display those employees whose job is not 'Manager' but who manage
other employees.
4) Display those employees whose names contain no fewer than 4
characters.
5) Display those departments whose names start with 'A' and locations end
with 'K'.
How to Participate:
• Follow Along: Stay tuned for each question as we release them in our
series.
• Practice: Use these questions to practice and sharpen your SQL skills.
• Engage: Share your progress and any questions you have with us.
• Apply: Utilize these queries to build and refine your SQL expertise.
Let’s embark on this SQL adventure together and unlock the full potential of
data manipulation with just 4 Tables!
Stay tuned for the first set of answers in the series! Let’s embark on this
journey to SQL mastery together
--department Table
CREATE TABLE DEPT (
DEPTNO INT PRIMARY KEY, -- Department Number
DNAME VARCHAR(50), -- Department Name
LOC VARCHAR(50) -- Location
);
-- Salary Grade
CREATE TABLE SALGRADE (
GRADE INT, -- Salary Grade
LOSAL DECIMAL(10, 2), -- Lowest Salary for Grade
HISAL DECIMAL(10, 2) -- Highest Salary for Grade
);
-- Job History
INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES
(7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10),
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
DEPT
Salgrade
JobHistory
-- 3) Display employee names, salaries, and total salaries for each department.
SELECT E.ENAME, E.SAL, SUM(E.SAL) OVER (PARTITION BY E.DEPTNO) AS
TOTAL_DEPT_SALARY
FROM EMP E;
-- 9) Display the names of employees who have the highest salary in their department.
SELECT ENAME
FROM EMP E
WHERE SAL = (
SELECT MAX(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO
);
-- 10) Display the department name and total salary for each department.
SELECT D.DNAME, SUM(E.SAL) AS TOTAL_SALARY
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
GROUP BY D.DNAME;
--department Table
CREATE TABLE DEPT (
DEPTNO INT PRIMARY KEY, -- Department Number
DNAME VARCHAR(50), -- Department Name
LOC VARCHAR(50) -- Location
);
-- Salary Grade
CREATE TABLE SALGRADE (
GRADE INT, -- Salary Grade
LOSAL DECIMAL(10, 2), -- Lowest Salary for Grade
HISAL DECIMAL(10, 2) -- Highest Salary for Grade
);
-- Job History
INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES
(7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10),
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
DEPT
Salgrade
JobHistory
-- 11) Display the names of employees who are working in the SALES
department.
SELECT ENAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.DNAME = 'SALES';
-- 12) Find the employee with the second highest salary in the
company.
SELECT TOP 1 ENAME
FROM (
SELECT ENAME, RANK() OVER (ORDER BY SAL DESC) AS RANK
FROM EMP
) AS SAL_RANKED
WHERE RANK = 2;
Optimized way
SELECT ENAME
FROM (
SELECT ENAME, SAL, DENSE_RANK() OVER (ORDER BY SAL DESC) AS RANK
FROM EMP
) AS SAL_RANKED
WHERE RANK = 2;
--Using Joins
SELECT E1.ENAME
FROM EMP E1
JOIN EMP E2 ON E1.SAL < E2.SAL
GROUP BY E1.ENAME, E1.SAL
HAVING COUNT(DISTINCT E2.SAL) = 1;
-- 13) Display the average salary for each job title round to 2
decimal.
SELECT JOB, ROUND(CAST(AVG(SAL) AS DECIMAL(10, 2)), 2) AS
AVERAGE_SALARY
FROM EMP
GROUP BY JOB;
-- 14) Display the names of employees who joined after the employee
‘SMITH’.
SELECT ENAME
FROM EMP
WHERE HIREDATE > (
SELECT HIREDATE
FROM EMP
WHERE ENAME = 'SMITH'
);
Optimized way:
WITH SMITH_HIREDATE AS (
SELECT HIREDATE
FROM EMP
WHERE ENAME = 'SMITH'
)
SELECT ENAME
FROM EMP, SMITH_HIREDATE
WHERE EMP.HIREDATE > SMITH_HIREDATE.HIREDATE;
-- 15) Display employee details along with their commission, but
show ‘0’ if no commission is given.
SELECT ENAME, SAL, ISNULL(COMM, 0) AS COMMISSION
FROM EMP;
Notes on ISNULL:
1. Definition:
o ISNULL is a function used in SQL to replace NULL values
with a specified value.
2. Syntax:
ISNULL(expression, replacement_value)
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
Check out 200+ Python Question and Answer for Data Engineer asked in Interview in
Topmate: https://topmate.io/shivakiran_kotur/1337666
SQL Mastery Series – 110 Question using 4 table – Set 3
Emp Table:
DEPT
Salgrade
JobHistory
-- 17) Display the names of employees who work in the same
department as ‘SMITH’ and dont include smith.
SELECT ENAME
FROM EMP
WHERE DEPTNO = (
SELECT DEPTNO
FROM EMP
WHERE ENAME = 'SMITH'
)
AND ENAME != 'SMITH';
--USing CTE
WITH SmithDept AS (
SELECT DEPTNO
FROM EMP
WHERE ENAME = 'SMITH'
)
SELECT ENAME
FROM EMP
WHERE DEPTNO = (SELECT DEPTNO FROM SmithDept)
AND ENAME != 'SMITH';
-- 19) Find employees whose job title contains the letter ‘M’.
SELECT ENAME , Job
FROM EMP
WHERE JOB LIKE '%M%';
--Using CTE
WITH ManagerSalaries AS (
SELECT EMPNO, SAL
FROM EMP
)
SELECT E.ENAME
FROM EMP E
JOIN ManagerSalaries M ON E.MGR = M.EMPNO
WHERE E.SAL > M.SAL;
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
Check out 200+ Python Question and Answer for Data Engineer asked in Interview in
Topmate: https://topmate.io/shivakiran_kotur/1337666
SQL Mastery Series – 110 Question using 4 table – Set 3
Emp Table:
DEPT
Salgrade
JobHistory
-- 23) Display employee details where the third character of the
name is ‘A’.
SELECT *
FROM EMP
WHERE ENAME LIKE '__A%';
--Using CTE
WITH AvgSalary AS (
SELECT AVG(SAL) AS avg_sal
FROM EMP
)
SELECT M.ENAME
FROM EMP M
WHERE M.EMPNO IN (SELECT DISTINCT MGR FROM EMP WHERE MGR IS NOT
NULL)
AND M.SAL > (SELECT avg_sal FROM AvgSalary);
-- 29) Display names of managers whose salary is more than the min
salary of employees. (Duplicate question)
SELECT M.ENAME
FROM EMP M
WHERE M.EMPNO IN (SELECT DISTINCT MGR FROM EMP WHERE MGR IS NOT
NULL)
AND M.SAL > (SELECT min(SAL) FROM EMP);
-- 30) Display employee name, salary, commission, and net pay for
those employees whose net pay is greater than or equal to any other
employee’s salary.
SELECT ENAME, SAL, COMM, (SAL + ISNULL(COMM, 0)) AS NET_PAY
FROM EMP
WHERE (SAL + ISNULL(COMM, 0)) >= ALL (SELECT SAL FROM EMP);
--using joins
SELECT E.ENAME, E.SAL, E.COMM, (E.SAL + ISNULL(E.COMM, 0)) AS
NET_PAY
FROM EMP E
JOIN (
SELECT MAX(SAL) AS MAX_SAL
FROM EMP
) AS MaxSalary
ON (E.SAL + ISNULL(E.COMM, 0)) >= MaxSalary.MAX_SAL;
--Using CTE
WITH MaxSalary AS (
SELECT MAX(SAL) AS MAX_SAL
FROM EMP
)
SELECT E.ENAME, E.SAL, E.COMM, (E.SAL + ISNULL(E.COMM, 0)) AS
NET_PAY
FROM EMP E
WHERE (E.SAL + ISNULL(E.COMM, 0)) >= (SELECT MAX_SAL FROM
MaxSalary);
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
Check out 200+ Python Question and Answer for Data Engineer asked in Interview in
Topmate: https://topmate.io/shivakiran_kotur/1337666
SQL Mastery Series – 110 Question using 4 table – Set 5
Emp Table:
DEPT
Salgrade
JobHistory
-- 31) Display those employees whose salary is less than their manager's salary but more than the
salary of any other manager.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE E.SAL < M.SAL
AND E.SAL > ANY (SELECT SAL FROM EMP WHERE EMPNO IN (SELECT DISTINCT MGR FROM EMP WHERE MGR IS NOT
NULL));
-- 32) Display all employee names with the total salary of the
company for each employee.
SELECT ENAME, (SELECT SUM(SAL) FROM EMP) AS TOTAL_COMPANY_SALARY
FROM EMP;
-- 37) Delete records from the employee table where the department
number is not available in the department table.
DELETE FROM EMP
WHERE DEPTNO NOT IN (SELECT DEPTNO FROM DEPT);
-- 39) Display employee name, salary, commission, and net pay where
the net pay is greater than any other employee's salary in the
company.
SELECT ENAME, SAL, COMM, (SAL + ISNULL(COMM, 0)) AS NET_PAY
FROM EMP
WHERE (SAL + ISNULL(COMM, 0)) > ANY (SELECT SAL FROM EMP);
-- 40) Display the names of those employees who are going to retire
on 31-Dec-99, if the maximum job period is 30 years.
SELECT ENAME
FROM EMP
WHERE DATEADD(YEAR, 30, HIREDATE) = '1999-12-31';
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
Check out 200+ Python Question and Answer for Data Engineer asked in Interview in
Topmate: https://topmate.io/shivakiran_kotur/1337666
SQL Mastery Series – 110 Question using 4 table – Set 6
Emp Table:
DEPT
Salgrade
JobHistory
-- 41) Display those employees whose salary is an odd value.
SELECT ENAME
FROM EMP
WHERE SAL % 2 = 1;
-- 43) Display those employees who joined the company in the month
of December.
SELECT ENAME
FROM EMP
WHERE MONTH(HIREDATE) = 12;
-- 48) Display the details of employees who are getting the same
salary as the minimum salary of any department.
SELECT *
FROM EMP
WHERE SAL IN (
SELECT MIN(SAL)
FROM EMP
GROUP BY DEPTNO
);
-- 49) Display the names of employees who joined in the year 1981
and are not getting any commission.
SELECT ENAME
FROM EMP
WHERE YEAR(HIREDATE) = 1981
AND COMM IS NULL;
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
Check out 200+ Python Question and Answer for Data Engineer asked in Interview in
Topmate: https://topmate.io/shivakiran_kotur/1337666
SQL Mastery Series – 110 Question using 4 table – Set 7
Emp Table:
DEPT
Salgrade
JobHistory
-- 51) Display employee names whose total earnings (salary +
commission) is greater than the average earnings of the company.
SELECT ENAME
FROM EMP
WHERE (SAL + ISNULL(COMM, 0)) > (
SELECT AVG(SAL + ISNULL(COMM, 0))
FROM EMP
);
-- 54) Display those employees who joined on the 10th of any month.
SELECT ENAME
FROM EMP
WHERE DAY(HIREDATE) = 10;
-- 55) Display the names of employees who have not joined in the
year 1981.
SELECT ENAME
FROM EMP
WHERE YEAR(HIREDATE) <> 1981;
-- 56) Display the names of employees who are not clerks and whose
salary is not more than 3000.
SELECT ENAME
FROM EMP
WHERE JOB <> 'CLERK'
AND SAL <= 3000;
-- 57) Display the names of employees who have not joined in the
month of December.
SELECT ENAME
FROM EMP
WHERE MONTH(HIREDATE) <> 12;
-- 59) Display the names of employees who earn more than the
minimum salary of their department.
SELECT ENAME
FROM EMP E
WHERE SAL > (
SELECT MIN(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO
);
-- 64) Display the names of employees who joined in the first half
of the year.
SELECT ENAME
FROM EMP
WHERE MONTH(HIREDATE) <= 6;
-- 65) Display the names of employees who have a higher salary than
any clerk.
SELECT ENAME
FROM EMP
WHERE SAL > ANY (SELECT SAL FROM EMP WHERE JOB = 'CLERK');
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
Check out 200+ Python Question and Answer for Data Engineer asked in Interview in
Topmate: https://topmate.io/shivakiran_kotur/1337666
SQL Mastery Series – 110 Question using 4 table – Set 8
Emp Table:
DEPT
Salgrade
JobHistory
-- 66) Display the names of employees who have not joined in the
month of February.
SELECT ENAME
FROM EMP
WHERE MONTH(HIREDATE) <> 2;
-- 67) Display the names of employees who earn less than 1500 but
do not work in the accounting department.
SELECT ENAME
FROM EMP
WHERE SAL < 1500
AND DEPTNO <> (SELECT DEPTNO FROM DEPT WHERE DNAME = 'ACCOUNTING');
--optimized query
SELECT E.ENAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE E.SAL < 1500
AND D.DNAME <> 'ACCOUNTING';
-- 74) Find out how many managers are there without listing them.
SELECT COUNT(DISTINCT MGR) AS NUM_MANAGERS
FROM EMP
WHERE MGR IS NOT NULL;
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
SQL Mastery Series – 110 Question using 4 table – Set 9
Emp Table:
DEPT
Salgrade
JobHistory
-- 75) Find out the average salary and total remuneration for each
job type, remembering that salespersons earn commission.
SELECT JOB,
AVG(SAL) AS AVG_SALARY,
SUM(SAL + ISNULL(COMM, 0)) AS TOTAL_REMUNERATION
FROM EMP
GROUP BY JOB;
-- 76) Check whether all employees’ numbers are indeed unique.
SELECT CASE
WHEN COUNT(EMPNO) = COUNT(DISTINCT EMPNO)
THEN 'Employee numbers are unique'
ELSE 'Duplicate employee numbers found'
END AS RESULT
FROM EMP;
--optimized Query
WITH MinSalaryByManager AS (
SELECT MGR, MIN(SAL) AS MIN_SAL
FROM EMP
GROUP BY MGR
)
SELECT E.ENAME, E.SAL, E.MGR
FROM EMP E
JOIN MinSalaryByManager M ON E.MGR = M.MGR AND E.SAL = M.MIN_SAL
WHERE E.SAL >= 1000
ORDER BY E.SAL;
-- 79) Find out the job that was filled in the first half of 1981
and the same job filled during the first half of 1983.
SELECT DISTINCT JOB
FROM EMP
WHERE (YEAR(HIREDATE) = 1981 AND MONTH(HIREDATE) <= 6)
AND JOB IN (
SELECT JOB
FROM EMP
WHERE YEAR(HIREDATE) = 1981 AND MONTH(HIREDATE) <= 6
);
--optimized query
SELECT DISTINCT JOB
FROM EMP
WHERE YEAR(HIREDATE) = 1981
AND MONTH(HIREDATE) <= 6;
-- 80) Find all the employees who joined the company before their
managers.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE E.HIREDATE < M.HIREDATE;
-- 81) List all employees by name and number along with their
manager’s name and number.
SELECT E.ENAME AS EMPLOYEE_NAME, E.EMPNO AS EMPLOYEE_NUMBER,
M.ENAME AS MANAGER_NAME, M.EMPNO AS MANAGER_NUMBER
FROM EMP E
LEFT JOIN EMP M ON E.MGR = M.EMPNO;
-- 82) Find out the employees who earn the highest salary in each
job type, sorted in ascending salary order.
SELECT E.ENAME, E.JOB, E.SAL
FROM EMP E
WHERE E.SAL = (SELECT MAX(SAL) FROM EMP WHERE JOB = E.JOB)
ORDER BY E.SAL ASC;
--optimized query
WITH RankedEmployees AS (
SELECT ENAME, JOB, SAL,
ROW_NUMBER() OVER (PARTITION BY JOB ORDER BY SAL DESC)
AS RowNum
FROM EMP
)
SELECT ENAME, JOB, SAL
FROM RankedEmployees
WHERE RowNum = 1
ORDER BY SAL ASC;
-- 83) Find all the employees who earn the minimum salary for their
job, sorted in ascending order.
SELECT E.ENAME, E.JOB, E.SAL
FROM EMP E
WHERE E.SAL = (SELECT MIN(SAL) FROM EMP WHERE JOB = E.JOB)
ORDER BY E.SAL ASC;
--optimized query
WITH RankedEmployees AS (
SELECT ENAME, JOB, SAL,
ROW_NUMBER() OVER (PARTITION BY JOB ORDER BY SAL ASC) AS
RowNum
FROM EMP
)
SELECT ENAME, JOB, SAL
FROM RankedEmployees
WHERE RowNum = 1
ORDER BY SAL ASC;
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
SQL Mastery Series – 110 Question using 4 table – Set 10
Emp Table:
DEPT
Salgrade
JobHistory
-- 84) Print a list of employees who earn more than the average
salary of their department.
SELECT E.ENAME, E.SAL
FROM EMP E
WHERE E.SAL > (SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO);
--using cte
WITH DeptAvg AS (
SELECT DEPTNO, AVG(SAL) AS AVG_SAL
FROM EMP
GROUP BY DEPTNO
)
SELECT E.ENAME, E.SAL
FROM EMP E
JOIN DeptAvg D ON E.DEPTNO = D.DEPTNO
WHERE E.SAL > D.AVG_SAL;
-- 85) Display employees who have the same DEPTNO as any of their
colleagues.
SELECT E.ENAME, E.DEPTNO
FROM EMP E
JOIN (SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) > 1) G ON E.DEPTNO = G.DEPTNO;
--using cte
WITH DeptWithMultipleEmployees AS (
SELECT DEPTNO
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) > 1
)
SELECT E.ENAME, E.DEPTNO
FROM EMP E
JOIN DeptWithMultipleEmployees G ON E.DEPTNO = G.DEPTNO;
-- 86) Display those employees whose names are exactly the same
length as their manager's name.
SELECT E.ENAME, M.ENAME AS MANAGER_NAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE LEN(E.ENAME) = LEN(M.ENAME);
-- 87) Find employees whose salaries are not greater than any
employee’s salary but lesser than the salaries of their managers.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE E.SAL < M.SAL
AND E.SAL <= (SELECT MIN(SAL) FROM EMP);
-- 88) Display the department with the most employees who earn more
than 2500.
SELECT TOP 1 D.DEPTNO, D.DNAME, COUNT(E.EMPNO) AS NUM_EMPLOYEES
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE E.SAL > 2500
GROUP BY D.DEPTNO, D.DNAME
ORDER BY COUNT(E.EMPNO) DESC;
--optimized query
WITH DeptEmployeeCount AS (
SELECT D.DEPTNO, D.DNAME, COUNT(E.EMPNO) AS NUM_EMPLOYEES
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE E.SAL > 2500
GROUP BY D.DEPTNO, D.DNAME
)
SELECT TOP 1 DEPTNO, DNAME, NUM_EMPLOYEES
FROM DeptEmployeeCount
ORDER BY NUM_EMPLOYEES DESC;
-- 89) Display employees whose salaries are lower than the highest
salary in their department.
SELECT E.ENAME, E.SAL, E.DEPTNO
FROM EMP E
WHERE E.SAL < (SELECT MAX(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO);
--using cte
WITH DeptMaxSalary AS (
SELECT DEPTNO, MAX(SAL) AS MAX_SAL
FROM EMP
GROUP BY DEPTNO
)
SELECT E.ENAME, E.SAL, E.DEPTNO
FROM EMP E
JOIN DeptMaxSalary D ON E.DEPTNO = D.DEPTNO
WHERE E.SAL < D.MAX_SAL;
-- 90) Find out which department has more employees than the number
of characters in less than department.
SELECT D.DEPTNO, D.DNAME
FROM DEPT D
JOIN (SELECT DEPTNO, COUNT(*) AS NUM_EMPLOYEES
FROM EMP
GROUP BY DEPTNO) E
ON D.DEPTNO = E.DEPTNO
WHERE E.NUM_EMPLOYEES < LEN(D.DNAME);
--using cte
WITH DeptEmployeeCount AS (
SELECT DEPTNO, COUNT(*) AS NUM_EMPLOYEES
FROM EMP
GROUP BY DEPTNO
)
SELECT D.DEPTNO, D.DNAME
FROM DEPT D
JOIN DeptEmployeeCount E ON D.DEPTNO = E.DEPTNO
WHERE E.NUM_EMPLOYEES < LEN(D.DNAME);
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
SQL Mastery Series – 110 Question using 4 table – Set 11
Emp Table:
DEPT
Salgrade
JobHistory
-- 93) Find employees who earn more than the average salary of all
employees in their department.
SELECT ENAME, SAL, DEPTNO
FROM EMP E
WHERE SAL > (SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO);
--optimized SQL
WITH DeptAvgSalary AS (
SELECT DEPTNO, AVG(SAL) AS AvgSal
FROM EMP
GROUP BY DEPTNO
)
SELECT E.ENAME, E.SAL, E.DEPTNO
FROM EMP E
JOIN DeptAvgSalary D ON E.DEPTNO = D.DEPTNO
WHERE E.SAL > D.AvgSal;
-- 96) Find out the department with the maximum number of employees
whose salary is greater than 3000.
SELECT TOP 1 DEPTNO
FROM EMP
WHERE SAL > 3000
GROUP BY DEPTNO
ORDER BY COUNT(*) DESC;
-- 97) Find the employees whose salary is more than the average
salary of their job type.
SELECT ENAME, JOB, SAL
FROM EMP E
WHERE SAL > (SELECT AVG(SAL)
FROM EMP
WHERE JOB = E.JOB);
--optimized sql
WITH JobAvgSalary AS (
SELECT JOB, AVG(SAL) AS AvgSal
FROM EMP
GROUP BY JOB
)
SELECT E.ENAME, E.JOB, E.SAL
FROM EMP E
JOIN JobAvgSalary J ON E.JOB = J.JOB
WHERE E.SAL > J.AvgSal;
-- 99) List the employees who have a commission but are not working
as Manager.
SELECT ENAME, COMM
FROM EMP
WHERE COMM IS NOT NULL
AND JOB <> 'MANAGER';
-- 100) Find all employees whose manager’s name starts with ‘J’.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE M.ENAME LIKE 'J%';
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452
SQL Mastery Series – 110 Question using 4 table – Set 12
Emp Table:
DEPT
Salgrade
JobHistory
-- 102) Find out the department with the highest total salary.
SELECT TOP 1 DEPTNO
FROM EMP
GROUP BY DEPTNO
ORDER BY SUM(SAL) DESC;
-- Write an SQL query to categorize employees as
--"Low Salary," "Medium Salary," or "High Salary" based on their
salary: below 1500, between 1500 and 3000, or above 3000.
SELECT ENAME, JOB, SAL,
CASE
WHEN SAL < 1500 THEN 'Low Salary'
WHEN SAL BETWEEN 1500 AND 3000 THEN 'Medium Salary'
ELSE 'High Salary'
END AS Salary_Category
FROM EMP;
--optimized
WITH RankedEmployees AS (
SELECT DEPTNO, ENAME, SAL,
ROW_NUMBER() OVER (PARTITION BY DEPTNO ORDER BY SAL
DESC) AS rn
FROM EMP
)
SELECT DEPTNO, ENAME AS HIGHEST_PAID_EMPLOYEE, SAL AS MAX_SALARY
FROM RankedEmployees
WHERE rn = 1;
Check out 200+ scenario based databricks and pyspark Scenario based Question in
Topmate: https://topmate.io/shivakiran_kotur/1376452