0% found this document useful (0 votes)
19 views

SQL solutions

The document announces a new SQL Mastery Series aimed at enhancing SQL skills through practical, hands-on learning with 110 questions based on a simple EMP table and three other tables. It emphasizes the importance of focusing on a manageable dataset to facilitate understanding of various SQL concepts and techniques. Participants are encouraged to follow along, practice, and engage with the content to refine their SQL expertise.
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)
19 views

SQL solutions

The document announces a new SQL Mastery Series aimed at enhancing SQL skills through practical, hands-on learning with 110 questions based on a simple EMP table and three other tables. It emphasizes the importance of focusing on a manageable dataset to facilitate understanding of various SQL concepts and techniques. Participants are encouraged to follow along, practice, and engage with the content to refine their SQL expertise.
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/ 59

Exciting SQL Mastery Series: Get Ready to Dive Deep!

Are you ready to elevate your SQL skills?

I am thrilled to announce the launch of brand-new series designed to help


you master SQL through practical, hands-on learning.

In this series, we will tackle 110 exciting SQL questions based on a simple EMP
table associated with 3 other tables.

Why Focus on Just 4 Tables?

Simplicity breeds clarity! By concentrating on a small, manageable dataset, we


can dive deep into various SQL concepts and techniques, making it easier for
you to understand and apply them in real-world scenarios.

What to Expect:

• Hands-On Queries: Each question is crafted to enhance your


understanding of SQL through practical examples.
• Diverse Challenges: From basic operations to complex joins, you’ll tackle
a range of queries to strengthen your SQL skills.

The 110 Exciting Questions Include:

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'.

Follow me on LinkedIn – Shivakiran kotur


6) Display those employees whose manager's name is JONES.
7) Display those employees whose salary is more than 3000 after a 20%
increment.
8) Display all employees along with their department names.
9) Display employees who are working in the sales department.
10) Display employee name, department name, salary, and
commission for those employees whose salary is between 2000 and 5000
and location is 'Chicago'.
11) Display those employees whose salary is greater than their
manager's salary.
12) Display those employees who are working in the same department
where their manager works.
13) Display those employees who are not working under any manager.
14) Display grade and employee names for department numbers 10 or
30 where the grade is not 4 and they joined before 31-Dec-1982.
15) Update the salary of each employee by a 10% increment if they are
not eligible for commission.
16) Delete those employees who joined before 31-Dec-1982 and
whose department location is New York or Chicago.
17) Display employee name, job, department name, and location for
all who are working as managers.
18) Display those employees whose manager's name is JONES and also
display their manager’s name.
19) Display the name and salary of employee 'FORD' if their salary
equals their grade.

Follow me on LinkedIn – Shivakiran kotur


20) Display employee name, job, department name, manager name,
salary, and grade, and arrange them by department.
21) List all employees' names, jobs, salaries, grades, and department
names except 'CLERK'. Sort by salary in descending order.
22) Display employee name, job, and manager. Include also employees
without a manager.
23) Find the top 5 earners in the company.
24) Display the names of those employees who have the highest
salary.
25) Display those employees whose salary equals the average of the
maximum and minimum salaries.
26) Select the count of employees in each department where the
count is greater than 3.
27) Display department names where at least 3 employees are
working.
28) Display names of managers whose salary is more than the average
salary of employees.
29) Display names of managers whose salary is more than the average
salary of employees.
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.
31) Display those employees whose salary is less than their manager's
salary but more than the salary of any other manager.

Follow me on LinkedIn – Shivakiran kotur


32) Display all employee names with the total salary of the company
for each employee.
33) Find the least 5 earners in the company.
34) Find the number of employees whose salary is greater than their
manager's salary.
35) Display those managers who are not working under the president
but are working under other managers.
36) Delete those departments where no employee is working.
37) Delete records from the employee table where the department
number is not available in the department table.
38) Display those employee names whose salary is outside the ranges
defined in the salary grade table.
39) Display employee name, salary, commission, and net pay where
the net pay is greater than any other employee's salary in the company.
40) Display the names of those employees who are going to retire on
31-Dec-99, if the maximum job period is 30 years.
41) Display those employees whose salary is an odd value.
42) Display those employees whose salary contains at least 3 digits.
43) Display those employees who joined the company in the month of
December.
44) Display those employees whose names contain 'A'.
45) Display those employees whose department number is available in
the salary table.
46) Display those employees where the first 2 characters of the hire
date match the last 2 characters of their salary.

Follow me on LinkedIn – Shivakiran kotur


47) Display those employees whose 10% of salary equals the year of
joining.
48) Display those employees who are working in sales or research
departments.
49) Display the grade of employee 'JONES'.
50) Display the first 50% of characters of employee names in
lowercase and the remaining in uppercase.
51) Display those employees who joined the company before the 15th
of this month.
52) Delete records where the number of employees in a particular
department is less than 2.
53) Delete those employees who joined the company 10 years ago
from today.
54) Display the department name where the number of characters in
the name equals the number of employees in another department.
55) Display the name of the department where no employee is
working.
56) Display those employees who are working as managers.
57) Count the number of employees working as managers using set
operators.
58) Display the names of employees who joined on the same date.
59) Display the manager whose grade is equal to any number in the
salaries but not equal to the first number in the salary.
60) Count the number of employees working as managers using set
operators.

Follow me on LinkedIn – Shivakiran kotur


61) Display the department name of those who joined on the same
date.
62) Display the manager who has the maximum number of employees
working under them.
63) List employee names and their salaries increased by 15%.
64) Produce the output of the EMP table as "EMPLOYEE_AND_JOB" for
employee names and jobs.
65) List all employees with their hire dates in the format ‘June 4,
1988’.
66) Print a list of employees displaying ‘Just salary’ if more than 1500,
‘target’ if exactly 1500, and ‘OFF target’ if less.
67) Given a string of the format ‘nn/nn’, verify if the first and last 2
characters are numbers and the middle character is ‘/’. Print ‘yes’ if valid,
‘no’ if not. Test with values ‘112/54’, ‘01/1a’, and ‘99/88’.
68) Employees hired on the 15th of any month are paid on the last
Friday of that month. Those hired after the 15th are paid the last Friday
of the following month. Print a list of employees, their hire date, and first
pay date.
69) Display those employees whose salary contains the first digits of
their department number.
70) Display those managers who are earning less than any of their
employees.
71) Print the details of all employees who are subordinates to ‘Blake’.
72) Display those who are working as managers using a correlated
subquery.

Follow me on LinkedIn – Shivakiran kotur


73) Display those employees whose manager’s name is JONES.
74) Find out how many managers are there without listing them.
75) Find out the average salary and total remuneration for each job
type, remembering that salespersons earn commission.
76) Check whether all employees’ numbers are indeed unique.
77) List the lowest-paid employees working for each manager. Exclude
any groups where the minimum salary is less than Rs.1000. Sort the
output by salary.
78) List employee names, jobs, annual salary, department number,
department name, and grade who earn 36,000 annually or who are not
clerks.
79) Find out the job that was filled in the first half of 1982 and the
same job filled during the first half of 1983.
80) Find all the employees who joined the company before their
managers.
81) List all employees by name and number along with their manager’s
name and number.
82) Find out the employees who earn the highest salary in each job
type, sorted in ascending salary order.
83) Find all the employees who earn the minimum salary for their job,
sorted in ascending order.
84) Print a list of employees who earn more than the average salary of
their department.
85) Display employees who have the same grade as any of their
colleagues.

Follow me on LinkedIn – Shivakiran kotur


86) Display those employees whose names are exactly the same length
as their manager's name.
87) Find employees whose salaries are not greater than any
employee’s salary but greater than the salaries of their managers.
88) Display the department with the most employees who earn more
than 2500.
89) Display employees whose salaries are lower than the highest salary
in their department.
90) Find out which department has more employees than the number
of characters in its name.
91) Display employees who are not assigned to any department.
92) Find the department number with the maximum number of
employees.
93) Find employees who earn more than the average salary of all
employees in their department.
94) List the employees who have a different job than the one they had
when they joined.
95) Find out the highest salary paid in the company.
96) Find out the department with the maximum number of employees
whose salary is greater than 3000.
97) Find the employees whose salary is more than the average salary
of their job type.
98) Display employees whose name starts with ‘A’ or ‘B’.
99) List the employees who have a commission but are not working in
the sales department.

Follow me on LinkedIn – Shivakiran kotur


100) Find all employees whose manager’s name starts with ‘S’.
101) Display all employees working as managers who earn more than
the average salary of all employees.
102) Find out the department with the highest total salary.
103) List employees who have not yet been promoted.
104) Find employees whose salary is not equal to the average salary of
their department.
105) List employees who have a commission but no direct reports.
106) Display employees whose salaries are exactly the average salary of
their department.
107) Find out which department has the highest number of employees
earning more than the average salary.
108) Find the total salary of employees in each department.
109) Display those employees whose salary is higher than their grade.
110) Find the maximum salary of employees in each department and
the name of the highest-paid employee.

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

Follow me on LinkedIn – Shivakiran kotur


--Table 1 Employee Table
CREATE TABLE EMP (
EMPNO INT PRIMARY KEY, -- Employee Number
ENAME VARCHAR(50), -- Employee Name
JOB VARCHAR(50), -- Job Title
MGR INT, -- Manager's Employee Number
HIREDATE DATE, -- Hire Date
SAL DECIMAL(10, 2), -- Salary
COMM DECIMAL(10, 2), -- Commission
DEPTNO INT, -- Department Number
CONSTRAINT FK_MGR FOREIGN KEY (MGR) REFERENCES EMP(EMPNO)
);

--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

CREATE TABLE JOBHISTORY (


EMPNO INT, -- Employee Number
JOB VARCHAR(50), -- Job Title
STARTDATE DATE, -- Start Date of Job
ENDDATE DATE, -- End Date of Job
DEPTNO INT, -- Department Number
CONSTRAINT FK_EMPNO FOREIGN KEY (EMPNO) REFERENCES EMP(EMPNO),
CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
);

Follow me on LinkedIn – Shivakiran kotur


INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES
(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');

INSERT INTO SALGRADE (GRADE, LOSAL, HISAL) VALUES


(1, 700, 1200),
(2, 1201, 1400),
(3, 1401, 2000),
(4, 2001, 3000),
(5, 3001, 9999);

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);

INSERT INTO JOB_HISTORY (EMPNO, START_DATE, END_DATE, JOB, DEPTNO) VALUES


(7369, '2019-01-01', '2020-12-31', 'INTERN', 20),
(7369, '2021-01-01', '2022-12-31', 'ASSISTANT CLERK', 20),
(7499, '2018-03-01', '2019-12-31', 'JUNIOR SALESMAN', 30),
(7499, '2020-01-01', '2021-12-31', 'SALESMAN', 30),
(7521, '2017-05-15', '2019-05-15', 'TRAINEE', 30),
(7521, '2019-05-16', '2020-12-31', 'SALESMAN', 30),
(7566, '2015-04-02', '2018-12-31', 'ASSISTANT MANAGER', 20),
(7566, '2019-01-01', '2021-12-31', 'MANAGER', 20),
(7698, '2016-05-01', '2019-04-30', 'ASSISTANT MANAGER', 30),
(7698, '2019-05-01', '2022-12-31', 'MANAGER', 30),
(7782, '2016-06-09', '2018-06-08', 'SUPERVISOR', 10),
(7782, '2018-06-09', '2021-06-08', 'MANAGER', 10),
(7839, '2010-11-17', '2015-11-16', 'VICE PRESIDENT', 10),
(7839, '2015-11-17', '2023-12-31', 'PRESIDENT', 10),
(7902, '2017-12-03', '2019-12-02', 'SENIOR ANALYST', 20),
(7902, '2019-12-03', '2022-12-02', 'ANALYST', 20),
(7934, '2016-01-23', '2018-01-22', 'JUNIOR CLERK', 10),
(7934, '2018-01-23', '2020-01-22', 'CLERK', 10);

Follow me on LinkedIn – Shivakiran kotur


SQL Mastery Series – 110 Question using 4 table – Set 1
Emp Table:

DEPT

Salgrade

JobHistory

Follow me on LinkedIn – Shivakiran kotur


-- 1) Display all employees with their department names.
SELECT E.ENAME, D.DNAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO;

-- 2) Display employees along with their manager names.


SELECT E.ENAME AS EMPLOYEE_NAME, M.ENAME AS MANAGER_NAME
FROM EMP E
LEFT JOIN EMP M ON E.MGR = M.EMPNO;

-- 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;

Follow me on LinkedIn – Shivakiran kotur


-- 4) Display employee names and their annual salary (SAL * 12).
SELECT ENAME, SAL * 12 AS ANNUAL_SALARY
FROM EMP;

-- 5) Display the total salary for each department.


SELECT DEPTNO, SUM(SAL) AS TOTAL_SALARY
FROM EMP
GROUP BY DEPTNO;

-- 6) Find the highest salary in each department.


SELECT DEPTNO, MAX(SAL) AS HIGHEST_SALARY
FROM EMP
GROUP BY DEPTNO;

-- 7) Display department-wise employee count.


SELECT DEPTNO, COUNT(*) AS EMPLOYEE_COUNT
FROM EMP
GROUP BY DEPTNO;

Follow me on LinkedIn – Shivakiran kotur


-- 8) Display the names of employees who earn more than the average salary of their
department.
SELECT ENAME FROM EMP E
WHERE SAL > (
SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO
);

-- 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;

Follow me on LinkedIn – Shivakiran kotur


--Table 1 Employee Table
CREATE TABLE EMP (
EMPNO INT PRIMARY KEY, -- Employee Number
ENAME VARCHAR(50), -- Employee Name
JOB VARCHAR(50), -- Job Title
MGR INT, -- Manager's Employee Number
HIREDATE DATE, -- Hire Date
SAL DECIMAL(10, 2), -- Salary
COMM DECIMAL(10, 2), -- Commission
DEPTNO INT, -- Department Number
CONSTRAINT FK_MGR FOREIGN KEY (MGR) REFERENCES EMP(EMPNO)
);

--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

CREATE TABLE JOBHISTORY (


EMPNO INT, -- Employee Number
JOB VARCHAR(50), -- Job Title
STARTDATE DATE, -- Start Date of Job
ENDDATE DATE, -- End Date of Job
DEPTNO INT, -- Department Number
CONSTRAINT FK_EMPNO FOREIGN KEY (EMPNO) REFERENCES EMP(EMPNO),
CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO)
);

Follow me on LinkedIn – Shivakiran kotur


INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES
(10, 'ACCOUNTING', 'NEW YORK'),
(20, 'RESEARCH', 'DALLAS'),
(30, 'SALES', 'CHICAGO'),
(40, 'OPERATIONS', 'BOSTON');

INSERT INTO SALGRADE (GRADE, LOSAL, HISAL) VALUES


(1, 700, 1200),
(2, 1201, 1400),
(3, 1401, 2000),
(4, 2001, 3000),
(5, 3001, 9999);

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);

CREATE TABLE JOB_HISTORY (


EMPNO INT,
START_DATE DATE,
END_DATE DATE,
JOB VARCHAR(50),
DEPTNO INT,
PRIMARY KEY (EMPNO, START_DATE)
);

INSERT INTO JOB_HISTORY (EMPNO, STARTDATE, ENDDATE, JOB, DEPTNO) VALUES


(7369, '2019-01-01', '2020-12-31', 'INTERN', 20),
(7369, '2021-01-01', '2022-12-31', 'ASSISTANT CLERK', 20),

Follow me on LinkedIn – Shivakiran kotur


(7499, '2018-03-01', '2019-12-31', 'JUNIOR SALESMAN', 30),
(7499, '2020-01-01', '2021-12-31', 'SALESMAN', 30),
(7521, '2017-05-15', '2019-05-15', 'TRAINEE', 30),
(7521, '2019-05-16', '2020-12-31', 'SALESMAN', 30),
(7566, '2015-04-02', '2018-12-31', 'ASSISTANT MANAGER', 20),
(7566, '2019-01-01', '2021-12-31', 'MANAGER', 20),
(7698, '2016-05-01', '2019-04-30', 'ASSISTANT MANAGER', 30),
(7698, '2019-05-01', '2022-12-31', 'MANAGER', 30),
(7782, '2016-06-09', '2018-06-08', 'SUPERVISOR', 10),
(7782, '2018-06-09', '2021-06-08', 'MANAGER', 10),
(7839, '2010-11-17', '2015-11-16', 'VICE PRESIDENT', 10),
(7839, '2015-11-17', '2023-12-31', 'PRESIDENT', 10),
(7902, '2017-12-03', '2019-12-02', 'SENIOR ANALYST', 20),
(7902, '2019-12-03', '2022-12-02', 'ANALYST', 20),
(7934, '2016-01-23', '2018-01-22', 'JUNIOR CLERK', 10),
(7934, '2018-01-23', '2020-01-22', 'CLERK', 10);

Follow me on LinkedIn – Shivakiran kotur


SQL Mastery Series – 110 Question using 4 table – Set 2 Emp
Table:

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)

o expression: The value or column to check for NULL.


o replacement_value: The value to return if the expression
is NULL.

-- 16) Display employees who do not have a manager.


SELECT ENAME
FROM EMP
WHERE MGR 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 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';

--Optimized way using joins


SELECT E1.ENAME
FROM EMP E1
JOIN EMP E2 ON E1.DEPTNO = E2.DEPTNO
WHERE E2.ENAME = 'SMITH'
AND E1.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';

-- 18) Display the names of employees who do the same job as


‘ALLEN’.
SELECT ENAME
FROM EMP
WHERE JOB = (
SELECT JOB
FROM EMP
WHERE ENAME = 'ALLEN'
);
--Give optimized query in comment

-- 19) Find employees whose job title contains the letter ‘M’.
SELECT ENAME , Job
FROM EMP
WHERE JOB LIKE '%M%';

-- 20) Display the details of employees whose salary is between


1000 and 2000.
SELECT *
FROM EMP
WHERE SAL BETWEEN 1000 AND 2000;

-- 21) Find employees who joined in the year 1981.


SELECT ENAME, HIREDATE
FROM EMP
WHERE YEAR(HIREDATE) = 1981;
-- 22) Find employees whose salary is higher than their manager's
salary.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE E.SAL > M.SAL;

--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%';

-- 24) Display employee details for employees who do not earn a


commission.
SELECT *
FROM EMP
WHERE COMM IS NULL;

-- 25) Display employee names where the department name is ‘SALES’.


SELECT E.ENAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.DNAME = 'SALES';

-- 26) Select the count of employees in each department where the


count is greater than 2.
SELECT DEPTNO, COUNT(*) AS EMPLOYEE_COUNT
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) > 2;
-- 27) Display department names where at least 3 employees are
working.
SELECT D.DNAME
FROM DEPT D
JOIN EMP E ON D.DEPTNO = E.DEPTNO
GROUP BY D.DNAME
HAVING COUNT(*) >= 3;

-- 28) Display names of managers whose salary is more than the


average salary of employees.
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 EMP);

-- Optimized query using joins


SELECT M.ENAME
FROM EMP M
JOIN EMP E ON M.EMPNO = E.MGR
GROUP BY M.ENAME, M.SAL
HAVING M.SAL > (SELECT AVG(SAL) FROM EMP);

--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);

--Give optimized query in comment section

-- 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;

-- 33) Find the least 5 earners in the company.


SELECT TOP 5 ENAME, SAL
FROM EMP
ORDER BY SAL ASC;

-- 34) Find the number of employees whose salary is greater than


their manager's salary.
SELECT COUNT(*) as emps
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE E.SAL > M.SAL;
-- 35) Display those managers who are not working under the
president but are working under other managers.
SELECT M.ENAME
FROM EMP M
WHERE M.EMPNO IN (SELECT DISTINCT MGR FROM EMP WHERE MGR IS NOT
NULL)
AND M.MGR IS NOT NULL
AND M.MGR <> (SELECT EMPNO FROM EMP WHERE JOB = 'PRESIDENT');

--Optimized way using Joins


SELECT DISTINCT M.ENAME
FROM EMP M
JOIN EMP S ON M.EMPNO = S.MGR
WHERE M.MGR IS NOT NULL
AND M.MGR <> (SELECT EMPNO FROM EMP WHERE JOB = 'PRESIDENT');

-- 36) Delete those departments where no employee is working.


DELETE FROM DEPT
WHERE DEPTNO NOT IN (SELECT DISTINCT DEPTNO 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);

-- 38) Display those employee names whose salary is outside the


ranges defined in the salary grade table.
SELECT ENAME
FROM EMP E
WHERE NOT EXISTS (
SELECT 1
FROM SALGRADE S
WHERE E.SAL BETWEEN S.LOSAL AND S.HISAL
);
--Using Joins Optimized way
SELECT E.ENAME
FROM EMP E
LEFT JOIN SALGRADE S
ON E.SAL BETWEEN S.LOSAL AND S.HISAL
WHERE S.GRADE IS NULL;

-- 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;

-- 42) Display those employees whose salary contains at least 3


digits.
SELECT ENAME
FROM EMP
WHERE LEN(SAL) >= 3;

-- 43) Display those employees who joined the company in the month
of December.
SELECT ENAME
FROM EMP
WHERE MONTH(HIREDATE) = 12;

-- 44) Display those employees who joined on 1-Jan-81.


SELECT ENAME
FROM EMP
WHERE HIREDATE = '1981-01-01';

-- 45) Display the names of employees who are working in a


department located in CHICAGO.
SELECT ENAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.LOC = 'CHICAGO';

-- 46) Display the average salary of employees, but exclude the


average salary of clerks from the result.
SELECT AVG(SAL) AS AVERAGE_SALARY
FROM EMP
WHERE JOB <> 'CLERK';

-- 47) Display the names of employees who are working in a


department with the highest average salary.
SELECT ENAME
FROM EMP
WHERE DEPTNO = (
SELECT TOP 1 DEPTNO
FROM EMP
GROUP BY DEPTNO
ORDER BY AVG(SAL) DESC
);

-- 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;

-- 50) Display employees who are working in the same department as


‘JONES’ or ‘SCOTT’.
SELECT ENAME
FROM EMP
WHERE DEPTNO IN (
SELECT DEPTNO
FROM EMP
WHERE ENAME IN ('JONES', 'SCOTT')
);

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
);

-- 52) Display the names of employees who earn a commission.


SELECT ENAME
FROM EMP
WHERE COMM IS NOT NULL;

-- 53) Display the names of employees whose salary is the same as


the lowest salary in their department.
SELECT ENAME
FROM EMP E
WHERE SAL = (
SELECT MIN(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO
);

-- 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;

-- 58) Display the names of employees who do not earn any


commission.
SELECT ENAME
FROM EMP
WHERE COMM IS NULL;

-- 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
);

-- 60) Display the names of employees who work in a department


located in CHICAGO or DALLAS.
SELECT ENAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.LOC IN ('CHICAGO', 'DALLAS');

-- 61) Display the names of employees whose manager is working in


the same department as ‘KING’.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE M.DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME = 'KING');

-- 62) Display the names of employees whose commission is less than


200.
SELECT ENAME
FROM EMP
WHERE COMM < 200;
-- 63) Display the names of employees whose salary is less than or
equal to their manager’s salary.
SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE E.SAL <= M.SAL;

-- 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';

-- 68) Display the names of employees who are working in the


RESEARCH department.
SELECT ENAME
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
WHERE D.DNAME = 'RESEARCH';

-- 69) Display the names of employees whose salary is less than


1500 and they do not earn any commission.
SELECT ENAME
FROM EMP
WHERE SAL < 1500
AND COMM IS NULL;

-- 70) Display the names of employees who have joined after


‘ALLEN’.
SELECT ENAME
FROM EMP
WHERE HIREDATE > (SELECT HIREDATE FROM EMP WHERE ENAME = 'ALLEN');

--optimized using joins


SELECT E1.ENAME
FROM EMP E1
JOIN EMP E2 ON E1.HIREDATE > E2.HIREDATE
WHERE E2.ENAME = 'ALLEN';
--using cte
WITH ALLEN_HIREDATE AS (
SELECT HIREDATE
FROM EMP
WHERE ENAME = 'ALLEN'
)
SELECT ENAME
FROM EMP
WHERE HIREDATE > (SELECT HIREDATE FROM ALLEN_HIREDATE);

-- 71) Print the details of all employees who are subordinates to


‘Blake’.
SELECT *
FROM EMP
WHERE MGR = (SELECT EMPNO FROM EMP WHERE ENAME = 'BLAKE');

--using join (optimized)


SELECT E1.*
FROM EMP E1
JOIN EMP E2 ON E1.MGR = E2.EMPNO
WHERE E2.ENAME = 'BLAKE';

-- 72) Display those who are working as managers using a correlated


subquery.
SELECT ENAME
FROM EMP E
WHERE EXISTS (SELECT 1 FROM EMP M WHERE E.EMPNO = M.MGR);

--optimized using joins


SELECT DISTINCT E.ENAME
FROM EMP E
JOIN EMP M ON E.EMPNO = M.MGR;

-- 73) Display those employees whose manager’s name is JONES.


SELECT E.ENAME
FROM EMP E
JOIN EMP M ON E.MGR = M.EMPNO
WHERE M.ENAME = 'JONES';

-- 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;

-- 77) List the lowest-paid employees working for each manager.


Exclude any groups where the minimum salary is less than Rs.1000.
Sort the output by salary.
SELECT E.ENAME, E.SAL, E.MGR
FROM EMP E
WHERE E.SAL = (SELECT MIN(SAL)
FROM EMP
WHERE MGR = E.MGR)
AND E.SAL >= 1000
ORDER BY E.SAL;

--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;

-- 78) List employee names, jobs, annual salary, department number,


department name, and grade who earn 36,000 annually or who are not
clerks.
SELECT E.ENAME, E.JOB, (E.SAL * 12) AS ANNUAL_SALARY, E.DEPTNO,
D.DNAME, G.GRADE
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
JOIN SALGRADE G ON E.SAL BETWEEN G.LOSAL AND G.HISAL
WHERE (E.SAL * 12) = 36000 OR E.JOB <> 'CLERK';
--using cte
WITH EmployeeDetails AS (
SELECT E.ENAME, E.JOB, (E.SAL * 12) AS ANNUAL_SALARY, E.DEPTNO,
D.DNAME, G.GRADE
FROM EMP E
JOIN DEPT D ON E.DEPTNO = D.DEPTNO
JOIN SALGRADE G ON E.SAL BETWEEN G.LOSAL AND G.HISAL
)
SELECT ENAME, JOB, ANNUAL_SALARY, DEPTNO, DNAME, GRADE
FROM EmployeeDetails
WHERE ANNUAL_SALARY = 36000 OR JOB <> 'CLERK';

-- 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);

-- 91) Display employees who are not assigned to any department.


SELECT ENAME
FROM EMP
WHERE DEPTNO IS NULL;

-- 92) Find the department number with the maximum number of


employees.
SELECT TOP 1 DEPTNO
FROM EMP
GROUP BY DEPTNO
ORDER BY COUNT(*) DESC;

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;

-- 94) Write an SQL query to retrieve employees whose hire date is


earlier than their direct manager’s hire date.
SELECT e.ENAME AS Employee, e.JOB AS Employee_Job, e.HIREDATE AS
Employee_HireDate,
m.ENAME AS Manager, m.JOB AS Manager_Job, m.HIREDATE AS
Manager_HireDate
FROM EMP e
JOIN EMP m ON e.MGR = m.EMPNO
WHERE e.HIREDATE < m.HIREDATE;

-- 95) Find out the highest salary paid in the company.


SELECT MAX(SAL) AS HIGHEST_SALARY
FROM EMP;

-- 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;

-- 98) Display employees whose name starts with ‘A’ or ‘B’.


SELECT ENAME
FROM EMP
WHERE ENAME LIKE 'A%' OR ENAME LIKE 'B%';

-- 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%';

-- 101) Display all employees working as managers who earn more


than the average salary of all employees.
SELECT ENAME, JOB, SAL
FROM EMP
WHERE JOB = 'MANAGER'
AND SAL > (SELECT AVG(SAL) FROM EMP);

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;

-- 104) Find employees whose salary is not equal to the average


salary of their department.
SELECT ENAME, SAL, DEPTNO
FROM EMP E
WHERE SAL <> (SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO);

-- 105) List employees who have a commission but no direct reports.


SELECT E.ENAME, E.COMM
FROM EMP E
LEFT JOIN EMP M ON E.EMPNO = M.MGR
WHERE E.COMM IS NOT NULL
AND M.EMPNO IS NULL;

-- 106) Display employees whose salaries are exactly the average


salary of their department.
SELECT ENAME, SAL, DEPTNO
FROM EMP E
WHERE SAL = (SELECT AVG(SAL)
FROM EMP
WHERE DEPTNO = E.DEPTNO);

-- 107) Find out which department has the highest number of


employees earning more than the average salary.
SELECT TOP 1 DEPTNO
FROM EMP
WHERE SAL > (SELECT AVG(SAL) FROM EMP)
GROUP BY DEPTNO
ORDER BY COUNT(*) DESC;
-- 108) Find the total salary of employees in each department.
SELECT DEPTNO, SUM(SAL) AS TOTAL_SALARY
FROM EMP
GROUP BY DEPTNO;

-- 109) Display those employees whose salary is higher than their


grade.
SELECT E.ENAME, E.SAL, G.GRADE
FROM EMP E
JOIN SALGRADE G ON E.SAL > G.HISAL;

-- 110) Find the maximum salary of employees in each department and


the name of the highest-paid employee.
SELECT E.DEPTNO, MAX(E.SAL) AS MAX_SALARY, E.ENAME AS
HIGHEST_PAID_EMPLOYEE
FROM EMP E
JOIN (SELECT DEPTNO, MAX(SAL) AS MAX_SAL
FROM EMP
GROUP BY DEPTNO) M ON E.DEPTNO = M.DEPTNO AND E.SAL =
M.MAX_SAL
GROUP BY E.DEPTNO, E.ENAME;

--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

You might also like