SlideShare a Scribd company logo
2
Most read
Complex Queries in SQL ( Oracle )
These questions are the most frequently asked in interviews.
1. To fetch ALTERNATE records from a table. (EVEN NUMBERED)
select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp);
2. To select ALTERNATE records from a table. (ODD NUMBERED)
select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp);
3. Find the 3rd MAX salary in the emp table.
select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal
<= e2.sal);
4. Find the 3rd MIN salary in the emp table.
select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal
>= e2.sal);
5. Select FIRST n records from a table.
select * from emp where rownum <= &n;
6. Select LAST n records from a table
select * from emp minus select * from emp where rownum <= (select count(*) - &n from
emp);
7. List dept no., Dept name for all the departments in which there are no employees in the
department.
select * from dept where deptno not in (select deptno from emp);
alternate solution: select * from dept a where not exists (select * from emp b where a.deptno
= b.deptno);
altertnate solution: select empno,ename,b.deptno,dname from emp a, dept b where
a.deptno(+) = b.deptno and empno is null;
8. How to get 3 Max salaries ?
select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal
<= b.sal) order by a.sal desc;
9. How to get 3 Min salaries ?
select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal
>= b.sal);
10. How to get nth max salaries ?
select distinct hiredate from emp a where &n = (select count(distinct sal) from emp b where
a.sal >= b.sal);
11. Select DISTINCT RECORDS from emp table.
select * from emp a where rowid = (select max(rowid) from emp b where
a.empno=b.empno);
12. How to delete duplicate rows in a table?
delete from emp a where rowid != (select max(rowid) from emp b where a.empno=b.empno);
13. Count of number of employees in department wise.
select count(EMPNO), b.deptno, dname from emp a, dept b where a.deptno(+)=b.deptno
group by b.deptno,dname;
14. Suppose there is annual salary information provided by emp table. How to fetch
monthly salary of each and every employee?
select ename,sal/12 as monthlysal from emp;
15. Select all record from emp table where deptno =10 or 40.
select * from emp where deptno=30 or deptno=10;
16. Select all record from emp table where deptno=30 and sal>1500.
select * from emp where deptno=30 and sal>1500;
17. Select all record from emp where job not in SALESMAN or CLERK.
select * from emp where job not in ('SALESMAN','CLERK');
18. Select all record from emp where ename in 'BLAKE','SCOTT','KING'and'FORD'.
select * from emp where ename in('JONES','BLAKE','SCOTT','KING','FORD');
19. Select all records where ename starts with ‘S’ and its lenth is 6 char.
select * from emp where ename like'S____';
20. Select all records where ename may be any no of character but it should end with ‘R’.
select * from emp where ename like'%R';
21. Count MGR and their salary in emp table.
select count(MGR),count(sal) from emp;
22. In emp table add comm+sal as total sal .
select ename,(sal+nvl(comm,0)) as totalsal from emp;
23. Select any salary <3000 from emp table.
select * from emp where sal> any(select sal from emp where sal<3000);
24. Select all salary <3000 from emp table.
select * from emp where sal> all(select sal from emp where sal<3000);
25. Select all the employee group by deptno and sal in descending order.
select ename,deptno,sal from emp order by deptno,sal desc;
26. How can I create an empty table emp1 with same structure as emp?
Create table emp1 as select * from emp where 1=2;
27. How to retrive record where sal between 1000 to 2000?
Select * from emp where sal>=1000 And sal<2000
28. Select all records where dept no of both emp and dept table matches.
select * from emp where exists(select * from dept where emp.deptno=dept.deptno)
29. If there are two tables emp1 and emp2, and both have common record. How can I fetch
all the recods but common records only once?
(Select * from emp) Union (Select * from emp1)
30. How to fetch only common records from two tables emp and emp1?
(Select * from emp) Intersect (Select * from emp1)
31. How can I retrive all records of emp1 those should not present in emp2?
(Select * from emp) Minus (Select * from emp1)
32. Count the totalsa deptno wise where more than 2 employees exist.
SELECT deptno, sum(sal) As totalsal
FROM emp
GROUP BY deptno
HAVING COUNT(empno) > 2

More Related Content

DOCX
Top 40 sql queries for testers
DOC
SQL practice questions - set 3
DOC
80 different SQL Queries with output
DOC
SQL practice questions set - 2
PDF
API Management Solution Powerpoint Presentation Slides
PDF
API Business Models
DOCX
SQL Differences SQL Interview Questions
PPTX
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Top 40 sql queries for testers
SQL practice questions - set 3
80 different SQL Queries with output
SQL practice questions set - 2
API Management Solution Powerpoint Presentation Slides
API Business Models
SQL Differences SQL Interview Questions
Understanding SQL Trace, TKPROF and Execution Plan for beginners

What's hot (20)

PDF
DOCX
All questions
PDF
Basic Sql Handouts
DOCX
SQL-RDBMS Queries and Question Bank
DOCX
Sql Queries
DOCX
Q on subquery
DOC
Sql queires
PDF
SQL practice questions set
PDF
Sql queries questions and answers
DOC
Sql queries with answers
PPT
Sql query [select, sub] 4
PPTX
SQL - DML and DDL Commands
DOC
Sql task
PDF
DBMS 5 | MySQL Practice List - HR Schema
PPTX
Sql subquery
PDF
[APJ] Common Table Expressions (CTEs) in SQL
 
PPT
Sql – Structured Query Language
DOC
Plsql task
DOC
Plsql task answers
All questions
Basic Sql Handouts
SQL-RDBMS Queries and Question Bank
Sql Queries
Q on subquery
Sql queires
SQL practice questions set
Sql queries questions and answers
Sql queries with answers
Sql query [select, sub] 4
SQL - DML and DDL Commands
Sql task
DBMS 5 | MySQL Practice List - HR Schema
Sql subquery
[APJ] Common Table Expressions (CTEs) in SQL
 
Sql – Structured Query Language
Plsql task
Plsql task answers
Ad

Viewers also liked (20)

PDF
SQL 101 for business experts and stakeholders
PDF
PDF
120cioig
PDF
PPTX
Mastering Python lesson 5a_lists_list_operations
PPTX
Geek Sync | Rewriting Bad SQL Code 101
PPTX
SQL Functions
DOC
Complete Sql Server querries
PDF
Developing Software using Python and Django to solve real life problems
PPT
Sql operators & functions 3
DOC
Ascp training manual_v1.2
PPT
SQL querys in detail || Sql query slides
PPTX
SQL Join Basic
PPTX
introdution to SQL and SQL functions
PPTX
SQL | Computer Science
KEY
Graphs in the Database: Rdbms In The Social Networks Age
PPTX
100 sql queries
PPTX
SQL Server Learning Drive
ODT
Sql queries interview questions
KEY
Trees In The Database - Advanced data structures
SQL 101 for business experts and stakeholders
120cioig
Mastering Python lesson 5a_lists_list_operations
Geek Sync | Rewriting Bad SQL Code 101
SQL Functions
Complete Sql Server querries
Developing Software using Python and Django to solve real life problems
Sql operators & functions 3
Ascp training manual_v1.2
SQL querys in detail || Sql query slides
SQL Join Basic
introdution to SQL and SQL functions
SQL | Computer Science
Graphs in the Database: Rdbms In The Social Networks Age
100 sql queries
SQL Server Learning Drive
Sql queries interview questions
Trees In The Database - Advanced data structures
Ad

Similar to Complex queries in sql (20)

PPTX
My SQL.pptx
DOC
ORACLE NOTES
DOC
Chapter 1
DOCX
Database Query Using SQL_ip.docx
PPTX
SQL Data Manipulation language and DQL commands
DOC
PPTX
Commands
DOCX
Trig
PDF
Orcl sql queries
PPTX
SQL.pptx
PDF
Sql queries
PPT
Oracle tips and tricks
DOCX
It6312 dbms lab-ex2
PDF
MySQL-commands.pdf
DOC
21390228-SQL-Queries.doc
PPTX
Interacting with Oracle Database
PDF
Introduction to oracle functions
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PDF
KScope19 - SQL Features
PDF
Sql queries
My SQL.pptx
ORACLE NOTES
Chapter 1
Database Query Using SQL_ip.docx
SQL Data Manipulation language and DQL commands
Commands
Trig
Orcl sql queries
SQL.pptx
Sql queries
Oracle tips and tricks
It6312 dbms lab-ex2
MySQL-commands.pdf
21390228-SQL-Queries.doc
Interacting with Oracle Database
Introduction to oracle functions
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
KScope19 - SQL Features
Sql queries

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Lesson notes of climatology university.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Institutional Correction lecture only . . .
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
O7-L3 Supply Chain Operations - ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Lesson notes of climatology university.
Module 4: Burden of Disease Tutorial Slides S2 2025
102 student loan defaulters named and shamed – Is someone you know on the list?
Final Presentation General Medicine 03-08-2024.pptx
Presentation on HIE in infants and its manifestations
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
Institutional Correction lecture only . . .
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

Complex queries in sql

  • 1. Complex Queries in SQL ( Oracle ) These questions are the most frequently asked in interviews. 1. To fetch ALTERNATE records from a table. (EVEN NUMBERED) select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from emp); 2. To select ALTERNATE records from a table. (ODD NUMBERED) select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from emp); 3. Find the 3rd MAX salary in the emp table. select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal <= e2.sal); 4. Find the 3rd MIN salary in the emp table. select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal >= e2.sal); 5. Select FIRST n records from a table. select * from emp where rownum <= &n; 6. Select LAST n records from a table select * from emp minus select * from emp where rownum <= (select count(*) - &n from emp); 7. List dept no., Dept name for all the departments in which there are no employees in the department. select * from dept where deptno not in (select deptno from emp); alternate solution: select * from dept a where not exists (select * from emp b where a.deptno = b.deptno); altertnate solution: select empno,ename,b.deptno,dname from emp a, dept b where a.deptno(+) = b.deptno and empno is null; 8. How to get 3 Max salaries ? select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal <= b.sal) order by a.sal desc; 9. How to get 3 Min salaries ? select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b where a.sal >= b.sal); 10. How to get nth max salaries ? select distinct hiredate from emp a where &n = (select count(distinct sal) from emp b where a.sal >= b.sal); 11. Select DISTINCT RECORDS from emp table. select * from emp a where rowid = (select max(rowid) from emp b where a.empno=b.empno); 12. How to delete duplicate rows in a table? delete from emp a where rowid != (select max(rowid) from emp b where a.empno=b.empno); 13. Count of number of employees in department wise. select count(EMPNO), b.deptno, dname from emp a, dept b where a.deptno(+)=b.deptno group by b.deptno,dname; 14. Suppose there is annual salary information provided by emp table. How to fetch monthly salary of each and every employee?
  • 2. select ename,sal/12 as monthlysal from emp; 15. Select all record from emp table where deptno =10 or 40. select * from emp where deptno=30 or deptno=10; 16. Select all record from emp table where deptno=30 and sal>1500. select * from emp where deptno=30 and sal>1500; 17. Select all record from emp where job not in SALESMAN or CLERK. select * from emp where job not in ('SALESMAN','CLERK'); 18. Select all record from emp where ename in 'BLAKE','SCOTT','KING'and'FORD'. select * from emp where ename in('JONES','BLAKE','SCOTT','KING','FORD'); 19. Select all records where ename starts with ‘S’ and its lenth is 6 char. select * from emp where ename like'S____'; 20. Select all records where ename may be any no of character but it should end with ‘R’. select * from emp where ename like'%R'; 21. Count MGR and their salary in emp table. select count(MGR),count(sal) from emp; 22. In emp table add comm+sal as total sal . select ename,(sal+nvl(comm,0)) as totalsal from emp; 23. Select any salary <3000 from emp table. select * from emp where sal> any(select sal from emp where sal<3000); 24. Select all salary <3000 from emp table. select * from emp where sal> all(select sal from emp where sal<3000); 25. Select all the employee group by deptno and sal in descending order. select ename,deptno,sal from emp order by deptno,sal desc; 26. How can I create an empty table emp1 with same structure as emp? Create table emp1 as select * from emp where 1=2; 27. How to retrive record where sal between 1000 to 2000? Select * from emp where sal>=1000 And sal<2000 28. Select all records where dept no of both emp and dept table matches. select * from emp where exists(select * from dept where emp.deptno=dept.deptno) 29. If there are two tables emp1 and emp2, and both have common record. How can I fetch all the recods but common records only once? (Select * from emp) Union (Select * from emp1)
  • 3. 30. How to fetch only common records from two tables emp and emp1? (Select * from emp) Intersect (Select * from emp1) 31. How can I retrive all records of emp1 those should not present in emp2? (Select * from emp) Minus (Select * from emp1) 32. Count the totalsa deptno wise where more than 2 employees exist. SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno HAVING COUNT(empno) > 2