SQL Scenario Based Interview Questions - ThinkETL
SQL Scenario Based Interview Questions - ThinkETL
SQL Scenario Based Interview Questions - ThinkETL
ThinkETL
MENU
Contents
https://thinketl.com/sql-scenario-based-interview-questions/ 1/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
7. How to find the employee with third MAX Salary using a SQL query without using
Analytic Functions?
https://thinketl.com/sql-scenario-based-interview-questions/ 2/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Below is the query to fetch the unique records using GROUP BY function.
Query:
SELECT EMPLOYEE_ID,
NAME,
SALARY
FROM EMPLOYEE
GROUP BY EMPLOYEE_ID, NAME, SALARY;
Result:
https://thinketl.com/sql-scenario-based-interview-questions/ 3/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Using ROW_NUMBER Analytic function, assign row numbers to each unique set of
records.
Query:
SELECT EMPLOYEE_ID,
NAME,
SALARY,
ROW_NUMBER() OVER(PARTITION BY EMPLOYEE_ID,NAME,SALARY ORDER BY
EMPLOYEE_ID) AS ROW_NUMBER
FROM EMPLOYEE;
Result:
https://thinketl.com/sql-scenario-based-interview-questions/ 4/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Once row numbers are assigned, by querying the rows with row number 1 will give the
unique records from the table.
Query:
Result:
https://thinketl.com/sql-scenario-based-interview-questions/ 5/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Query:
SELECT ROWID,
EMPLOYEE_ID,
NAME,SALARY,
ROW_NUMBER() OVER(PARTITION BY EMPLOYEE_ID,NAME,SALARY ORDER BY
EMPLOYEE_ID) AS ROW_NUMBER
FROM EMPLOYEE;
Result:
https://thinketl.com/sql-scenario-based-interview-questions/ 6/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Query:
Result:
ROWID
AAASnBAAEAAACrWAAB
AAASnBAAEAAACrWAAD
AAASnBAAEAAACrWAAE
AAASnBAAEAAACrWAAG
STEP-3: Delete the records from the source table using the ROWID
values fetched in previous step
Query:
Result:
The table EMPLOYEE will have below records after deleting the duplicates
In the below query, we are comparing the ROWIDs’ of the unique set of records and
keeping the record with MIN ROWID and deleting all other rows.
Query:
https://thinketl.com/sql-scenario-based-interview-questions/ 8/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Result:
The table EMPLOYEE will have below records after deleting the duplicates
The opposite of above discussed case can be implemented by keeping the record with
MAX ROWID from the unique set of records and delete all other duplicates by
executing below query.
Query:
Result:
The table EMPLOYEE will have below records after deleting the duplicates
https://thinketl.com/sql-scenario-based-interview-questions/ 9/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
DEPARTMENT_ID DEPARTMENT_NAME
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
https://thinketl.com/sql-scenario-based-interview-questions/ 10/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Query:
Result:
DEPARTMENT_ID DEPARTMENT_NAME
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
In order to select the last 5 records we need to find (count of total number of records
– 5) which gives the count of records from first to last but 5 records.
Using the MINUS function we can compare all records from DEPARTMENTS table
with records from first to last but 5 from DEPARTMENTS table which give the last 5
https://thinketl.com/sql-scenario-based-interview-questions/ 11/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Query:
MINUS
Result:
DEPARTMENT_ID DEPARTMENT_NAME
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
Table_A
https://thinketl.com/sql-scenario-based-interview-questions/ 12/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
COL
null
Table_B
COL
null
null
Normal Join:
Normal Join or Inner Join is the most common type of join. It returns
the rows that are exact match between both the tables.
The following Venn diagram illustrates a Normal join when combining two result sets:
https://thinketl.com/sql-scenario-based-interview-questions/ 13/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Query:
SELECT a.COL as A,
b.COL as B
FROM TABLE_A a JOIN TABLE_B b
ON a.COL = b.COL;
Result:
A B
1 1
1 1
0 0
The Left Outer Join returns all the rows from the left table and only the
matching rows from the right table. If there is no matching row found
https://thinketl.com/sql-scenario-based-interview-questions/ 14/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
from the right table, the left outer join will have NULL values for the
columns from right table.
The following Venn diagram illustrates a Left join when combining two result sets:
Query:
SELECT a.COL as A,
b.COL as B
FROM TABLE_A a LEFT OUTER JOIN TABLE_B b
ON a.COL = b.COL;
Result:
A B
1 1
1 1
0 0
NULL NULL
https://thinketl.com/sql-scenario-based-interview-questions/ 15/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
The Right Outer Join returns all the rows from the right table and only
the matching rows from the left table. If there is no matching row found
from the left table, the right outer join will have NULL values for the
columns from left table.
The following Venn diagram illustrates a Right join when combining two result sets:
Query:
SELECT a.COL as A,
b.COL as B
FROM TABLE_A a RIGHT OUTER JOIN TABLE_B b
ON a.COL = b.COL;
Result:
A B
1 1
https://thinketl.com/sql-scenario-based-interview-questions/ 16/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
1 1
0 0
NULL NULL
NULL NULL
The Full Outer Join returns all the rows from both the right table and
the left table. If there is no matching row found, the missing side
columns will have NULL values.
The following Venn diagram illustrates a Full join when combining two result sets:
Query:
SELECT a.COL as A,
b.COL as B
FROM TABLE_A a FULL OUTER JOIN TABLE_B b
ON a.COL = b.COL;
https://thinketl.com/sql-scenario-based-interview-questions/ 17/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
Result:
A B
1 1
1 1
0 0
NULL NULL
NULL NULL
NULL NULL
https://thinketl.com/sql-scenario-based-interview-questions/ 18/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
In order to find the second MAX salary, employee record with MAX salary needs to be
eliminated. It can be achieved by using below SQL query.
Query:
Result:
https://thinketl.com/sql-scenario-based-interview-questions/ 19/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
SALARY
11000
The above query only gives the second MAX salary value. In order to fetch the entire
employee record with second MAX salary we need to do a self-join on Employee table
based on Salary value.
Query:
WITH
TEMP AS(
SELECT MAX(salary) AS salary FROM Employees WHERE salary NOT IN (
SELECT MAX(salary) AS salary FROM Employees)
)
SELECT a.* FROM Employees a JOIN TEMP b on a.salary = b.salary
Result:
Query:
https://thinketl.com/sql-scenario-based-interview-questions/ 20/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
SELECT Employee_Id,
Name,
Salary
FROM(
SELECT Employees.*,
DENSE_RANK() OVER(ORDER BY Salary DESC) as SALARY_RANK
FROM Employees)
WHERE SALARY_RANK =2
Result:
In order to find the third MAX salary, we need to eliminate the top 2 salary records. But
we cannot use the same method we used for finding second MAX salary (not a best
practice). Imagine if we have to find the fifth MAX salary. We should not be writing a
query with four nested sub queries.
STEP-1:
https://thinketl.com/sql-scenario-based-interview-questions/ 21/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
The approach here is to first list all the records based on Salary in the descending
order with MAX salary on top and MIN salary at bottom. Next, using ROWNUM select
the top 2 records.
Query:
Result:
Salary
13000
11000
STEP-2:
Next find the MAX salary from EMPLOYEE table which is not one of top two salary
values fetched in the earlier step.
Query:
Result:
SALARY
https://thinketl.com/sql-scenario-based-interview-questions/ 22/35
10/26/22, 10:34 PM SQL Scenario based Interview Questions - ThinkETL
6000
STEP-3:
In order to fetch the entire employee record with third MAX salary we need to do a self-
join on Employee table based on Salary value.
Query:
WITH
TEMP AS(
SELECT MAX(salary) as salary FROM Employees WHERE salary NOT IN (
SELECT salary FROM(
SELECT salary FROM Employees ORDER BY salary DESC)
WHERE ROWNUM < 3)
)
SELECT a.* FROM Employees a join TEMP b on a.salary = b.salary
Result:
email address
Subscribe
https://thinketl.com/sql-scenario-based-interview-questions/ 23/35