SQL Subquery
SQL Subquery
Using SubQuery
SQL Subquery
1.1 Objective
1.2 Introduction
a SELECT clause
a FROM clause
a WHERE clause
Subqueries can be used with the comparison operators such as =, <, >, >=, <=, and also
with the multiple row operators such as IN, ANY or ALL.
Execution Process:
Inner query executes first and passes the result to outer query.
Syntax:
SELECT select_list
FROM table
WHERE expression comparison_operator
(SELECT select_list
FROM table);
Inner query should return only one column when comparison operators are used in the
outer query.
The column used in the WHERE clause of outer query should be compatible with the
column information returned by the inner query.
GROUP BY and HAVING clauses cannot be used in the inner query when comparison
operators are used in the outer query.
If inner query returns a null value to the outer query, the outer query will not return any
rows when using certain comparison operators in a WHERE clause.
Subqueries that return more than one row can only be used with multiple value
operators, such as the IN operator.
The ntext, text, and image data types cannot be used in the select list of subqueries.
Note: If an error occurs while executing a subquery, execute the inner query
separately to check and fix the error.
Comparison Operators (<, >, <=, >=, =, <>) can be used only when the inner query
returns a single value.
2.
List Operators (IN, NOT IN) can be used if the inner query returns a single column
with multiple values.
3.
Modified Comparison Operators (ANY or ALL) can be used if the inner query returns
multiple values for outer query evaluation. ANY, ALL operators always used in combination
with comparison operators.
Ex: =any, <any, >any, <all, >all.
4.
Existence Operators (Exits or NOT Exists) can be used to check the existence of
records in the inner query, and returns TRUE or FALSE based on the existence of data.
Note: The select list of a subquery introduced with EXISTS, by convention,
has an asterisk (*) instead of a single column name.
FROM tblemployees
WHERE dept_no =
(SELECT dept_no
FROM tblDepartment
WHERE dept_name='sales')
Note: When single row operator is used, and if a subquery returns more than one row
then the query will become invalid and an error will be returned.
Multi row operator Ex: Use IN, ANY, or ALL operator in outer query to handle a subquery that
returns multiple rows.
Display the employee details of Learning and Sales Department.
SELECT emp_id, first_name, last_name, salary, designation
FROM tblemployees
WHERE dept_no IN
(SELECT dept_no
FROM tblDepartment
WHERE dept_name IN ('sales',learning))
In correlated subquery, the inner query processing depends on the execution of outer query.
Depending on the results of the correlated subquery execution, it will determine if the row of
the outer query is returned in the final result set.
In this type of queries, a table alias must be used to specify which table reference is to be
used.
Execution Process:
Outer query executes first and passes the result to inner query.
For each row of Outer query result, inner query will be executed.
Display the Departments information where at least one employee is working.
SELECT dept_no, dept_name, location
FROM tbldepartment d1
WHERE EXISTS
(SELECT * FROM tblEmployees e1 WHERE e1.dept_no=d1.dept_no)
Display the list of all employees whose salary is above the average salary for each
employees department.
SELECT emp_id, first_name, last_name
FROM tblEmployees e1
WHERE salary >
(SELECT AVG(salary)
FROM tblEmployees e2
WHERE e1.dept_no=e2.dept_no)