0% found this document useful (0 votes)
28 views

Uk 2

Uploaded by

umang kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Uk 2

Uploaded by

umang kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SubQuery:

A subquery is a query nested inside another query. It is also called inner query.
It is embedded within the WHERE clause, FROM clause and SELECT clause.
It can be used with SELECT, UPDATE, INSERT and DELETE.

It could be equality operator or comparison operator such as “=,>=,<= and LIKE operator”.

IMPORTANCE of Subquery:

1. Modularity: Breaking complex queries into single parts.


2. Reusability: It can be used multiple times within the same query.
3. Flexibility: It allows for more complex data retrieval operations that are difficult to
archive with standard joins.
4. Enhanced readability: It can simplify the main query by encapsulating complex
logic.
5. Hierarchical data processing: It enables processing data in a hierarchical manner,
which can be useful in certain models.

Types of Subquery:

1. Single-Row Subquery(Scalar Subquery)

2. Multi-Row Subquery

3. Correlated Subquery

4. NON – Correlated Subquery

1. Single-Row Subquery (Scalar Subquery):

It returns only one row and one column.

Syntax: SELECT column1, column2 FROM table1 WHERE column3 = (SELECT


column FROM table2 WHERE condition);

** Using WHERE clause **


Ex: select * from employee where salary = (select max(salary)
from employee);
** Using SELECT clause **

Ex: select e2.emp_id employee_ID,(select e1.name as employee_Name


from employee e1 where e1.emp_id = e2.emp_id) from employee e2;

** Using HAVING clause **

Ex: select dept_id as Department_ID, avg(salary) as average_salary from


employee group by dept_id having avg(salary) > (select avg(salary)
from employee);

2. Multi-Row Subquery:

It returns more than one row and one column.

Syntax: SELECT column1, column2 FROM table1 WHERE column3 IN (SELECT


column FROM table2 WHERE condition);

It is typically used with operators that can handle multiple


values. Operators:
1. IN
2. ANY
3. ALL
4. EXISTS

1. Using IN operator: It is used to match a value against the set of


value.It allows us to easily test if an expression matches any value in a list of values.

Ex: SELECT name FROM employee WHERE dept_id IN (SELECT dept_id FROM
e employee GROUP BY dept_id HAVING COUNT(*) > 1);

2. Using ALL operator: The ALLoperator compares a value to all values in another
set or subquery. It returns true only if the condition is true for all values.
Ex: select * from employee where salary > all(select avg(salary)
from employee) order by emp_id;

3. Using ANY operator: The ANYoperator compares a value to each value in a list
or subquery and returns true if any value meets the condition.

Ex: select salary from employee where salary < ANY (select salary
from employee where salary > 45000);

4. Using EXISTS operator: The EXISTSoperator is used to test for the existence of
any record in a subquery. It returns true if the subquery returns one or more records.

Ex: select * from employee e1 where exists (select * from employee e2


where e1.emp_id=e2.emp_id) order by emp_id;

3. Correlated Subquery:

A correlated subquery is a subquery that uses values from the outer query. Because
the subquery is evaluated once for each row processed by the outer query, it can be
more computationally expensive, but it allows for more complex and dynamic
queries.

Syntax: SELECT column1, column2 FROM table1 outer_alias WHERE columnX


operator (SELECT columnY FROM table2 inner_alias
WHERE

outer_alias.columnA =
inner_alias.columnB);

Ex: SELECT name, salary, dept_id FROM employee e1 WHERE salary


> (SELECT AVG(salary) FROM employee e2 WHERE e1.dept_id =
e2.dept_id);
3. Non-Correlated Subquery:

A non-correlated subquery is a subquery that can be executed independently of the outer query.
It does not reference columns from the outer query and can be run on its own.

Syntax: SELECT column1, column2 FROM table1 WHERE columnX operator


(SELECT columnY FROM table2 WHERE condition);

Ex: select * from employee where salary > (select


avg(salary) Average_salary from employee);

Ex: SELECT name, salary, (SELECT name FROM department WHERE


department.id = employee.dept_id) AS dept_name

FROM employee WHERE salary > (SELECT AVG(salary) FROM employee);

Subquery using INSERT:

Syntax: Insert into high_earners(emp_id, name, salary, dept_id)


select emp_id, name, salary, dept_id from employee where salary >
(select avg(salary) from employee);

Subquery using UPDATE:


Syntax: update employee set salary = salary * 1.10 where salary <
(select avg(salary) from employee);

Subquery using DELETE:

Syntax: delete from employee where salary < (select avg(salary)


from employee);
LISTAGG:

The LISTAGGfunction in Oracle SQL is used to aggregate values from multiple rows into
a single string, with a specified delimiter separating the values. This function is useful
for concatenating values from a group of rows into a single string.

Ex: select dept_id, LISTAGG(name,',') within group(order by name) as


Employees from employee group by dept_id;

You might also like