SQL Solved Questions
SQL Solved Questions
Interviews
Credits – Manthan Godha [ Your Tech Guy ]
AAASnBAAEAAAC
102 Pat 6000 2
rWAAG
Credits – Manthan Godha [ Your Tech Guy ]
ROWID
AAASnBAAEAAACrWAAB
AAASnBAAEAAACrWAAD
AAASnBAAEAAACrWAAE
AAASnBAAEAAACrWAAG
STEP-3: Delete the records from the source table using the ROWID values fetched
in previous step
Query:
DELETE FROM EMP WHERE ROWID IN (
SELECT ROWID FROM(
SELECT ROWID,
ROW_NUMBER() OVER(PARTITION BY EMPLOYEE_ID,NAME,SALARY ORDER
BY EMPLOYEE_ID) AS ROW_NUMBER
FROM EMPLOYEE)
WHERE ROW_NUMBER > 1);
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
Credits – Manthan Godha [ Your Tech Guy ]
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
Credits – Manthan Godha [ Your Tech Guy ]
F
Credits – Manthan Godha [ Your Tech Guy ]
DEPARTMENT_ID DEPARTMENT_NAME
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
ROWNUM is a “Pseudocolumn” that assigns a number to each row returned
by a query indicating the order in which Oracle selects the row from a table.
The first row selected has a ROWNUM of 1, the second has 2, and so on.
Query:
SELECT * FROM Departments WHERE ROWNUM <= 5;
Result:
DEPARTMENT_ID DEPARTMENT_NAME
10 Administration
Credits – Manthan Godha [ Your Tech Guy ]
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
Credits – Manthan Godha [ Your Tech Guy ]
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 records of the table as result.
MINUS operator is used to return all rows in the first SELECT statement
that are not present in the second SELECT statement.
Query:
SELECT * FROM Departments
MINUS
DEPARTMENT_ID DEPARTMENT_NAME
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
Credits – Manthan Godha [ Your Tech Guy ]
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
Left Outer Join:
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 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:
Credits – Manthan Godha [ Your Tech Guy ]
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
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
1 1
0 0
NULL NULL
NULL NULL
Full Outer Join:
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:
Credits – Manthan Godha [ Your Tech Guy ]
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;
Result:
A B
1 1
1 1
0 0
NULL NULL
NULL NULL
NULL NULL
NOTE: NULL do not match with NULL
Credits – Manthan Godha [ Your Tech Guy ]
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:
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:
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: