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

SQL Master Set 2

The document contains SQL queries for various employee-related tasks, including retrieving employee names from the SALES department, finding the second highest salary, calculating average salaries by job title, and displaying employee details with commission information. It also includes optimizations for certain queries and explanations of SQL functions like ISNULL. Additionally, it provides links to further resources for data engineering interview questions.

Uploaded by

Shubham
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 views4 pages

SQL Master Set 2

The document contains SQL queries for various employee-related tasks, including retrieving employee names from the SALES department, finding the second highest salary, calculating average salaries by job title, and displaying employee details with commission information. It also includes optimizations for certain queries and explanations of SQL functions like ISNULL. Additionally, it provides links to further resources for data engineering interview questions.

Uploaded by

Shubham
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/ 4

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

You might also like