7. Subquery
Subquery
(write a query nested inside another query)
a subquery is a query nested inside another query such as
SELECT, INSERT, DELETE & UPDATE
Example 1 (get films whose rental rate is higher than average rental rate)
Example 2 (get films that have returned rate between two dates)
Example 3 (get customers who have paid)
ANY
(retrieve data by comparing a value with a set of values returned by a subquery.)
The subquery must return exactly one column.
The ANY operator must be preceded by one of the following comparison operator =, <=, >, <, > and <>
The ANY operator returns true if any value of the subquery meets the condition, otherwise, it returns false.
The = ANY is equivalent to IN operator.
Example 1 (get films whose category is either Action or Drama)
ALL
(query data by comparing a value with a list of values returned by a subquery.)
With the assumption that the subquery returns some rows, the ALL operator works as follows:
column_name > ALL (subquery)
the expression evaluates to true if a value is greater than the biggest value returned by the subquery.column_name >= ALL (subquery)
the expression evaluates to true if a value is greater than or equal to the biggest value returned by the subquery.column_name < ALL (subquery)
the expression evaluates to true if a value is less than the smallest value returned by the subquery.column_name <= ALL (subquery)
the expression evaluates to true if a value is less than or equal to the smallest value returned by the subquery.column_name = ALL (subquery)
the expression evaluates to true if a value is equal to any value returned by the subquery.column_name != ALL (subquery)
the expression evaluates to true if a value is not equal to any value returned by the subquery.
Example 1 (find all films whose lengths are greater than the list of the average lengths)
EXISTS
(check for the existence of rows returned by a subquery.)
If the subquery returns at least one row, the result of EXISTS is true. In case the subquery returns no row, the result is of EXISTS is false
If the subquery returns NULL, EXISTS returns true
The result of EXISTS operator depends on whether any row returned by the subquery, and not on the row contents. Therefore, columns that appear on the SELECT clause of the subquery are not important.
Example 1 (Find customers who have at least one payment whose amount is greater than 11)
Last updated