Uk 2
Uk 2
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:
Types of Subquery:
2. Multi-Row Subquery
3. Correlated Subquery
2. Multi-Row Subquery:
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.
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.
outer_alias.columnA =
inner_alias.columnB);
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.
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.