Complex SQL Queries Examples
Complex SQL Queries Examples
Answer: Select distinct Salary from Employee e1 where 2=Select count(distinct Salary)
from Employee e2 where e1.salary<=e2.salary;
Answer :
Select * from Employee a where rowid <>( select max(rowid) from Employee b
where a.Employee_num=b.Employee_num);
3.How to fetch monthly Salary of Employee if annual salary is given?(click here for
Explaination)
Answer:
4.What is the Query to fetch first record from Employee table? (90% asked Complex SQL
Queries Examples)
Answer:
Answer:
6.What is Query to display first 5 Records from Employee table?(90% asked Complex SQL
Queries Examples)
Answer:
6.What is Query to display last 5 Records from Employee table?(90% asked Complex SQL
Queries Examples)
Answer:
union
select * from (Select * from Employee e order by rowid desc) where rownum <=5;
select min(salary)from(select distinct salary from emp order by salary desc)where rownum<=3;
9.How to Display Odd rows in Employee table?(90% asked Complex SQL Queries Examples)
Answer:
Select * from(Select rownum as rno,E.* from Employee E) where Mod(rno,2)=1;
10.How to Display Even rows in Employee table?
Answer:
Select * from(Select rownum as rno,E.* from Employee) where Mod(rno,2)=0;
Answer:
select * from (Select Dense_Rank() over ( order by salary desc) as Rnk,E.* from Employee E) where
Rnk=3;
Click Here to Get Information on Rank and Dense_Rank
12.How Can i create table with same structure of Employee table?(90% asked Complex SQL
Queries Examples)
Answer:
Create table Employee_1 as Select * from Employee where 1=2;
13.Display first 50% records from Employee table?
Answer:
select rownum, e.* from emp e where rownum<=(select count(*)/2 from emp);
14.Display last 50% records from Employee table?
Answer:
minus
Select rownum,E.* from Employee E where rownum<=(Select count(*)/2) from Employee);
15.How Can i create table with same structure with data of Employee table?
Answer:
Create table Employee1 as select * from Employee;
16.How do i fetch only common records between 2 tables.
Answer:
Intersect
22.How to fetch all the records from Employee whose joining year is 2017?
Answer:
Oracle:
MS SQL:
Answer:
Select Dept_id,max(salary) from Employee group by Dept_id;
24.How Do you find all Employees with its managers?(Consider there is manager id also in
Employee table)
Answer:
25.Display the name of employees who have joined in 2016 and salary is greater than 10000?
Answer:
Select name from Employee where Hire_Date like ‘2016%’ and salary>10000;
**
***
Answer:
We cannot use dual table to display output given above. To display output use any table. I am using Student table.
Answer :
SELECT
Email
FROM
Employee
where NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}’, ‘i’);
Answer:
Tip: User needs to know the concept of Hierarchical queries. Click here to get concept of hierarchical
queries
29.How to remove duplicate rows from table?(100% asked in Complex SQL Queries for
Interviews)
Answer:
30.How to find count of duplicate rows? (95% asked in SQL queries for Interviews )
Answer:
Group by rollno
1. How can we swap the gender column in a employee table ie: male should replace
by female and vice versa.
2. How to delete duplicate rows in a table.
3. Difference between index seek and index scan.
4. How to write your DOB using SQL date function.
5. Difference between cluster and non cluster index.
6. How to find 2nd or 3rd max and min salary of an employee.
7. How view is important for the security purpose.
8. Different type of joins and datatypes.
9. Difference between row number , rank and dense rank.
10. They can also give you any case scenario.
11. Difference between commit, rollback and save point.
12. Difference between with(nolock ) and readpast.
13. Explain the scenario of blocking and deadlock.
14. What is the use of nocount command.
15. Different type of triggers.