DBMS - Unit 3 - Notes (Subquery - Nested & Correlated)
DBMS - Unit 3 - Notes (Subquery - Nested & Correlated)
• department table
Employee table
• Suppose you want to find out the ename, job,sal of the
employees whose salaries are less than that of an employee
whose empno= 7876 from EMP table. Now you need to
perform two queries in order to get the desired result.
1. We will find out the salary of the employee whose
empno=7876. the query is as under:
The example consists of three query blocks: the outer query and two inner queries.
The inner query blocks are executed first, producing the query results ST_CLERK and
2600, respectively.
The outer query block is then processed and uses the values returned by the inner queries
to complete its search conditions.
USING GROUP FUNCTIONS IN A
SUBQUERY
The example on the slide displays the employee last name, job ID, and
salary of all employees whose salary is equal to the minimum salary.
The MIN group function returns a single value (2500) to the outer query.
PROBLEMS WITH SUBQUERIES
A common problem with subqueries is no rows being returned by the inner query.
The outer query takes the results of the subquery (null) and uses these results in its
WHERE clause. The outer query finds no employee with a job ID equal to null, and so
returns no rows.
If a job existed with a value of null, the row is not returned because comparison of two null
values yields a null, therefore the WHERE condition is not true.
MULTIPLE ROW SUBQUERY
SYMBOL MEANING
IN Equal to any member in a list.
ANY Return rows that match any value on a list.
ALL Return rows that match all the values in a list.
IN OPERATOR
• The IN operator retirns true if the comparison value is
contained in the list.
• The following statement finds the employee whose
salary is the same as the minimum salary of the
employees in the department.
ANY OPERATOR IN MULTIPLE-ROW
SUBQUERIES
The ANY operator compares a value to each value returned by a subquery.
The slide example displays employees who are not IT programmers and whose
salary is less than that of any IT programmer. The maximum salary that a
programmer earns is $9,000.
When using SOME or ANY, you often use the DISTINCT keyword to prevent
rows from being selected several times.
EXAMPLE :
The NOT operator can be used with IN, ANY, and ALL operators.
ANY OPERATOR
• The ANY operator return true if the comparison value
matches any of the values in the list.
• Display the employees whose salary is more than the
minimum salary of the employees in any department.
ALL OPERATOR
• Returns true only if the comparison value matches all the
values in the list.
• Display the employee detail with salary less than those whose
job is ‘MANAGER’.
MULTIPLE COLUMN SUBQUERY