Complex SQL Queries Examples with answers :
Following are some very important complex sql queries examples with
answers.I have tried to explain each and every query in detail so that
everyone will get idea of how it is executed [Link] are
some Complex SQL Queries Examples with answers in detail.
MOST IMPORTANT QUERIES (90% ASKED IN INTERVIEWS)
[Link] to find Second Highest Salary of Employee?(click for explaination)
Answer:-
Select distinct Salary from Employee e1 where 2=Select count(distinct
Salary) from Employee e2 where [Link]<=[Link];
[Link] to find duplicate rows in table?(click here for explaination)
Answer :-
Select * from Employee a where row_id != select max(row_id) for
Employee b where a.Employee_num=b.Employee_num;
[Link] to fetch monthly Salary of Employee if annual salary is given?(click
here for Explaination)
Answer:-
Select Employee_name,Salary/12 as 'Monthly Salary' from
employee;
[Link] is the Query to fetch first record from Employee table?
Answer:-
Select * from Employee where Rownum =1;
[Link] is the Query to fetch last record from the table?
Answer:-
Select * from Employee where Rowid= select max(Rowid) from
Employee;
Complex SQL Queries
[Link] is Query to display first 5 Records from Employee table?
Answer:
Select * from Employee where Rownum <= 5;
[Link] is Query to display last 5 Records from Employee table?
Answer:
Select * from Employee e where rownum <=5
union
select * from (Select * from Employee e order by rowid desc)
where rownum <=5;
[Link] is Query to display Nth Record from Employee table?
Select * from Employee where rownum = &n;
[Link] to get 3 Highest salaries records from Employee table?
select distinct salary from employee a where 3 >= (select
count(distinct salary) from emp loyee b where [Link] <=
[Link]) order by [Link] desc;
[Link] to Display Odd rows in Employee table?
Select * from(Select rownum as rno,E.* from Employee E) where
Mod(rno,2)=1;
[Link] to Display Odd rows in Employee table?
Select * from(Select rownum as rno,E.* from Employee) where
Mod(rno,2)=0;
[Link] to fetch 3rd highest salary using Rank Function?
select * from (Select Dense_Rank() over ( order by salary desc)
as Rnk,E.* from Employee E) where Rnk=3;
[Link] Can i create table with same structure of Employee table?
Create table Employee_1 as Select * from Employee where 1=2;
[Link] first 50% records from Employee table?
Select rownum,E.* from Employee E where rownum<=(Select
count(*/2) from Employee);
[Link] first 50% records from Employee table?
Select rownum,E.* from Employee E
minus
Select rownum,E.* from Employee E where rownum<=(Select
count(*/2) from Employee);
[Link] Can i create table with same structure with data of Employee
table?
Create table Employee1 as select * from Employee;