What are 20 SQL Queries for interview ? | 20 SQL queries for interview

  • 20 SQL Queries for interview :

    In this article I will explain 20 SQL Queries for interview purpose. These are really important queries which will ask in most of the interview questions.

MOST IMPORTANT QUERIES (90% ASKED IN INTERVIEWS)

1.Query to find Second Highest Salary of Employee?(Most important question in 20 SQL Queries for interview)

  • Answer:-

Select distinct Salary from Employee e1 where 2=Select count(distinct Salary) from Employee e2 where e1.salary<=e2.salary;

2.Query 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;

3.How 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;

Click here to get information on ROW_ID

4.What is the Query to fetch first record from Employee table?

  • Answer:-

 Select * from Employee where Rownum =1;

Click here to get What is Rownum?

5.What is the Query to fetch last record from the table?

  • Answer:-

Select * from Employee where Rowid= select max(Rowid) from Employee;

Complex SQL Queries

6.What is Query to display first 5 Records from Employee table?

  • Answer:

Select * from Employee where Rownum <= 5;

6.What 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;

Click Here to get What is Union?

7.What is Query to display Nth Record from Employee table?

Select * from Employee  where rownum = &n;

For Any issues contact :complexsql@gmail.com

8.How 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 a.salary <= b.salary) order by a.salary desc;

9.How to Display Odd rows in Employee table?

Select * from(Select rownum as rno,E.* from Employee E) where Mod(rno,2)=1;

10.How to Display Even rows in Employee table?

Select * from(Select rownum as rno,E.* from Employee) where Mod(rno,2)=0;

11.How 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;

Click Here to Get Information on Rank and Dense_Rank

12.How Can i create table with same structure of Employee table?

Create table Employee_1 as Select * from Employee where 1=2;

13.Display first 50% records from Employee table?

Select rownum,E.* from Employee E where rownum<=(Select count(*/2) from Employee);

14.Display last 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);

15.How Can i create table with same structure with data of Employee table?

Create table Employee1 as select * from Employee;

16.How do i fetch only common records between 2 tables.

Select * from Employee;

Intersect

Select * from Employee1;

For Any issues contact :complexsql@gmail.com

CLICK HERE TO GET INFORMATION ABOUT INTERSECT OPERATOR

17.Find Query to get information of Employee where Employee is not assigned to the department.

Select * from Employee where Dept_no Not in(Select Department_no from Employee);

18.How to get distinct records from the table without using distinct keyword.

select * from Employee a where  rowid = (select max(rowid) from Employee b where  a.Employee_no=b.Employee_no);

19.Select all records from Employee table whose name is ‘Amit’ and ‘Pradnya’

Select * from Employee where Name in(‘Amit’,’Pradnya’);

20.Select all records from Employee table where name not in ‘Amit’ and ‘Pradnya’

select * from Employee where name Not  in (‘Amit’,’Pradnya’);

>>>>>>>>>>> Click Here to Get your First ComplexSQL PDF<<<<<<<<<<<<<<<<<<<

HOME

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

4 weeks ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

4 weeks ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago