0% found this document useful (0 votes)
711 views8 pages

20 SQL Queries For Interview - Complex SQL Queries For Interview

The document discusses 20 SQL queries that are commonly asked during interviews. It provides the questions and answers for queries related to finding the second highest salary, duplicate rows, monthly salary from annual salary, top/bottom records, odd/even rows, using ranking functions, creating tables with the same structure, common/distinct records between tables, records not assigned to a department, and more. The purpose is to explain important SQL queries that may be asked during technical interviews.

Uploaded by

Sanchit Singla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
711 views8 pages

20 SQL Queries For Interview - Complex SQL Queries For Interview

The document discusses 20 SQL queries that are commonly asked during interviews. It provides the questions and answers for queries related to finding the second highest salary, duplicate rows, monthly salary from annual salary, top/bottom records, odd/even rows, using ranking functions, creating tables with the same structure, common/distinct records between tables, records not assigned to a department, and more. The purpose is to explain important SQL queries that may be asked during technical interviews.

Uploaded by

Sanchit Singla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A


Home About Us Privacy Policy Contact Us

SQL WORLD
SQL Query

LEAR N SQL FAST… Jobs Hiring Immediately

SQL COMPLEX QUERIES SQL INTERVIEW QUESTIONS

20 SQL Queries for interview | Complex


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)

www.complexsql.com/20-sql-queries-for-interview/ 1/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A

1.Query to nd 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 nd 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;

SPONSORED SEARCHES

Basic Interview Questions

Question Answer

3.How to fetch  monthly Salary of Employee if annual salary is


given?(click here for Explaination)

Answer:-

www.complexsql.com/20-sql-queries-for-interview/ 2/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

HOMEEmployee_name,Salary/12
   Select SQL QUERIES ORACLE asORACLE BI Salary’
‘Monthly UNIX POSTGRESQL INTERVIEW Q/A

from employee;

Click here to get information on ROW_ID

4.What is the Query to fetch rst 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 rst 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:

www.complexsql.com/20-sql-queries-for-interview/ 3/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

HOME
Select SQL
* from QUERIES e where
Employee ORACLE ORACLE
rownum <=5 BI UNIX POSTGRESQL INTERVIEW Q/A

union

SPONSORED SEARCHES

Oracle SQL Interview

SQL Queries

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?

www.complexsql.com/20-sql-queries-for-interview/ 4/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

HOME
select SQL
* from QUERIES
(Select ORACLE over
Dense_Rank() ORACLE BIby UNIX
( order POSTGRESQL INTERVIEW Q/A

 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 rst 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;

www.complexsql.com/20-sql-queries-for-interview/ 5/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

ForHOME
AnySQLissues
QUERIES contact
ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A

: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<<<<<<<<<<<<<<<<<<<

www.complexsql.com/20-sql-queries-for-interview/ 6/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A

HOME

2 Replies to “20 SQL Queries for


interview | Complex SQL
Queries for Interview”

1. harsh says:

March 20, 2019 at 11:42 am


www.complexsql.com/20-sql-queries-for-interview/ 7/8
4/11/2019 20 SQL Queries for interview | Complex SQL Queries for Interview

Query to nd second highest Sal.


HOME SQL QUERIES ORACLE ORACLE BI UNIX POSTGRESQL INTERVIEW Q/A

Create table emp(id number,


name varchar2(10),
sal number);

insert into emp values(1,’Aam’,100);


insert into emp values(2,’Bam’,200);
insert into emp values(3,’cam’,300);
insert into emp values(4,’Dam’,400);
insert into emp values(5,’Eam’,400);
insert into emp values(6,’Fam’,500);
insert into emp values(7,’Gam’,500);
insert into emp values(8,’Ham’,600);
insert into emp values(9,’Iam’,700);
insert into emp values(10,’Jam’,700);
insert into emp values(11,’Kam’,800);

Select distinct e1.sal as high_sal from emp e1 where 2=


(Select count(distinct e2.sal) from emp e2 where
e2.sal<=e1.sal);

Amit S says:

March 26, 2019 at 11:30 am


Thanks Harsh..for explaining this query..

www.complexsql.com/20-sql-queries-for-interview/ 8/8

You might also like