SQL Scenarion Based Basics
SQL Scenarion Based Basics
EMPLOYEE_ID NUMBER(6,0),
NAME VARCHAR2(20),
SALARY NUMBER(8,2)
);
Below is the query to fetch the unique records using GROUP BY function.
Query:
SELECT EMPLOYEE_ID,
NAME,
SALARY
FROM EMPLOYEE
Result:
Query:
SELECT EMPLOYEE_ID,
NAME,
SALARY,
Result:
Once row numbers are assigned, by querying the rows with row number 1 will
give the unique records from the table.
Query:
FROM( SELECT
EMPLOYEE_ID,
NAME,
SALARY,
ROW_NUMBER() OVER(PARTITION BY EMPLOYEE_ID,NAME,SALARY ORDER BY
EMPLOYEE_ID) AS ROW_NUMBER
FROM EMPLOYEE)
WHERE ROW_NUMBER = 1;
Result:
Query:
SELECT ROWID,
EMPLOYEE_ID,
NAME,SALARY,
FROM EMPLOYEE;
Result:
Query:
SELECT ROWID,
EMPLOYEE_ID,
NAME,
SALARY,
FROM EMPLOYEE)
Result:
ROWID
AAASnBAAEAAACrWAAB
AAASnBAAEAAACrWAAD
AAASnBAAEAAACrWAAE
AAASnBAAEAAACrWAAG
STEP-3: Delete the records from the source table using the ROWID values
fetched in previous step
Query:
SELECT ROWID,
FROM EMPLOYEE)
Result:
The table EMPLOYEE will have below records after deleting the duplicates
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
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
Department_ID number,
Department_Name varchar(50)
);
DEPARTMENT_ID DEPARTMENT_NAME
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
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.
Query:
Result:
DEPARTMENT_ID DEPARTMENT_NAME
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
COL
1
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:
Query:
SELECT a.COL as A,
b.COL as B
ON a.COL = b.COL;
Result:
A B
1 1
1 1
0 0
The following Venn diagram illustrates a Left join when combining two result
sets:
Query:
SELECT a.COL as A,
b.COL as B
ON a.COL = b.COL;
Result:
A B
1 1
1 1
0 0
NULL NULL
The following Venn diagram illustrates a Right join when combining two result
sets:
Query:
SELECT a.COL as A,
b.COL as B
ON a.COL = b.COL;
Result:
A B
1 1
1 1
0 0
NULL NULL
NULL NULL
The following Venn diagram illustrates a Full join when combining two result
sets:
Query:
SELECT a.COL as A,
b.COL as B
ON a.COL = b.COL;
Result:
A B
1 1
1 1
0 0
NULL NULL
NULL NULL
NULL NULL
EMPLOYEE_ID NUMBER(6,0),
SALARY NUMBER(8,2)
);
Query:
Result:
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(
Result:
SELECT Employee_Id,
Name,
Salary
FROM(
SELECT Employees.*,
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:
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
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(
Result:
In order to find the employee with nth highest salary, replace the
rownum value with n in above query.
References:
https://www.toptal.com/sql/interview-questions
https://intellipaat.com/blog/interview-question/sql-interview-questions/