SQL Queries for Interviews
SQL Queries for Interviews
The table EMPLOYEE will have below records after deleting the duplicates
ROWID EMPLOYEE_ID NAME SALARY
AAASnBAAEAAACrWAA
100 Jennifer 4400
A
AAASnBAAEAAACrWAA
101 Michael 13000
C
AAASnBAAEAAACrWAA
102 Pat 6000
F
AAASnBAAEAAACrWAA
103 Den 11000
H
METHOD-2: Using ROWID and Correlated subquery
Correlated subquery is used for row-by-row processing. With a normal nested
subquery, the inner SELECT query runs once and executes first. The returning
values will be used by the main query. A correlated subquery, however,
executes once for every row of the outer query. In other words, the inner query
is driven by the outer query.
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:
DELETE FROM EMPLOYEE A WHERE ROWID > (SELECT MIN(ROWID) FROM EMPLOYEE B WHERE
B.EMPLOYEE_ID = A.EMPLOYEE_ID ); Result:
The table EMPLOYEE will have below records after deleting the duplicates
ROWID EMPLOYEE_ID NAME SALARY
AAASnBAAEAAACrWAA
100 Jennifer 4400
A
AAASnBAAEAAACrWAA
101 Michael 13000
C
AAASnBAAEAAACrWAA
102 Pat 6000
F
AAASnBAAEAAACrWAA
103 Den 11000
H
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:
DELETE FROM EMPLOYEE A WHERE ROWID < (SELECT MAX(ROWID) FROM EMPLOYEE B WHERE
B.EMPLOYEE_ID = A.EMPLOYEE_ID ); Result:
The table EMPLOYEE will have below records after deleting the duplicates
ROWID EMPLOYEE_ID NAME SALARY
AAASnBAAEAAACrWAA
100 Jennifer 4400
A
AAASnBAAEAAACrWAA
101 Michael 13000
C
AAASnBAAEAAACrWAA
102 Pat 6000
F
AAASnBAAEAAACrWAA
103 Den 11000
H
3. How to read TOP 5 records from a
table using a SQL query?
Consider below table DEPARTMENTS as the source data
CREATE TABLE Departments(
Department_ID number,
Department_Name varchar(50)
);
MINUS
Table_B
COL
1
0
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:
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 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:
SELECT salary FROM(
SELECT salary FROM Employees ORDER BY salary DESC)
WHERE ROWNUM < 3; 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:
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 );
Result:
SALARY
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:
EMPLOYEE_ID NAME SALARY
102 Pat 6000
In order to find the employee with nth highest salary, replace the rownum value with n in
above query.