SUBQUERY

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

SUB QUERY:-A query within/inside another query is called subquery.

==========
Outer query(inner query/subquery)

TYPES:- NON-CORRELATED and CORRELATED

1.SINGLE ROW SUBQUERY:-When inner query/subquery returns single row as an output.


2.MULTIPLE ROW SUBQUERY:-if it gives multiple rows as output.
3.SCALAR SUBQUERY:- subquery written in select statement.
4.INLINE VIEW:-subquery written in from statement.
5.NESTED SUBQUERY:-subquery written in where clause.
6.CORRELATED SUBQUERY:-outer query and inner query dependent on each other.
7.MULTIPLE COLUMN SUBQUERY:-multiple columns as output.

in non correlated subquery you csn write two seperate queries and merge it using
logic

1.Single Row Subquery:-


======================

Qn: Find the name of the employee who is earning max salary

SELECT MAX(SALARY) FROM EMPLOYEES;

SELECT FIRST_NAME,SALARY FROM EMPLOYEES WHERE SALARY=24000;

select first_name,salary from employees where salary=(select max(salary) from


employees);

select first_name,salary from employees where salary>(select salary from employees


where first_name='Neena')

2.Multiple row subquery:-IN,ALL,ANY(if you try to run inner query you will get
multiple rows

=======================

Fetch the name and salary of the employees who is getting more salary than the
employees working in department 60?

select salary from employees where department_id=60


select first_name,salary,department_id from employees where salary(9000,6000,....)

select first_name,salary,department_id from employees where salary >all(select


salary from employees where department_id=60);

>ALL=GREATER THAN THE GREATEST.

<all=lesser than the lowest.

>ANY=GREATER THAN THE LOWEST

<any=lesser than the greatest..

3.SCALAR SUBQUERY:-SELECT CLAUSE


==================

select 5,3 from dual;

select 5,(select 1+2 from dual) from dual;

find the comparison between the employee who is working in department 10 along with
the highest and lowest salaries in the whole group?

SELECT FIRST_NAME,
(SELECT MAX(SALARY)FROM EMPLOYEES),
SALARY,
(SELECT MIN(SALARY)FROM EMPLOYEES),
DEPARTMENT_ID
FROM EMPLOYEES WHERE DEPARTMENT_ID=10;

4.INLINE SUBQUERY:-FROM CLAUSE


==============
SELECT * FROM (SELECT FIRST_NAME,SALARY,RANK()OVER(ORDER BY SALARY DESC)AS RK FROM
EMPLOYEES)WHERE RK=1;

5.NESTED SUBQUERY:-WHERE CLAUSE -- Single row, multiple rows colums are example of
nested subquery
=================
6.CORRELATED SUBQUERY:-
=====================
Find the details of the employeeS who is working NOT IN atleast one of the
projects?

EMPL1

Eid Ename Address


1 Swathi Adyar
2 Surya Mylapore
3 Nivedha Triplicane
4 Jay OMR

PRJCT

EID PID PNAME LOCATION


1 1 BIG DATA BLR
3 2 AWS CHN
4 3 DEVOPS HYD

select * from empl1 where eid in(select eid from prjct where empl1.eid=prjct.eid);

It will always be having out to in approach


the output from emp1 table will fetch as input to another query

7.MULTIPLE COLUMNS SUBQUERY:-


============================
If the query returns more than one column,then its called multiple column
subquery..

find the names of the employees who are getting maximum salary department wise?
select first_name,salary,department_id from employees
where (salary,department_id)in (select max(salary),department_id from employees
group by department_id);

You might also like