The document discusses subqueries in SQL, detailing their usage within SELECT, INSERT, UPDATE, and DELETE statements, as well as various operators and constraints applicable to them. Key points include the requirement for subqueries to be enclosed in parentheses, rules against using certain functions or operators within subqueries, and methods for testing their results regarding emptiness and duplicates. Additionally, examples are provided to illustrate the application of subqueries for set membership and comparison.
1. Set Membership
2.Set Comparison
3. Test for Empty Relation
4. Test for The Absence of
Duplicate Tuples
Presented By
Tutsmaster.org
2.
A Subquery or Inner query or Nested query is
a query within another SQL query and
embedded within the WHERE clause.
A sub query is used to return data that will be
used in the main query as a condition to
further restrict the data to be retrieved.
Subqueries can be used with the SELECT,
INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=,
<=, IN, BETWEEN etc.
3.
Subqueries mustbe enclosed within parentheses.
A subquery can have only one column in the SELECT clause,
unless multiple columns are in the main query for the subquery
to compare its selected columns.
An ORDER BY cannot be used in a subquery, although the main
query can use an ORDER BY. The GROUP BY can be used to
perform the same function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with
multiple value operators, such as the IN operator.
The SELECT list cannot include any references to values that
evaluate to a BLOB (Binary Large Object), ARRAY, CLOB((Character
Large Object), or NCLOB.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery; however,
the BETWEEN operator can be used within the subquery.
5.
It isused to check if value of expression is
matching a set of values produced by a sub
query. For sub query set membership
condition, In Keyword is used.
Example
SELECT NAME FROM EMPLOYEES
WHERE EMPLOYEE IN (
SELECT COMPANY FROM COMPANIES
WHERE AVG_SALES > TOTAL_SALES
6.
It isused to compare value of expression in
marching a set produced by a sub query. For
sub query set comparison, operation (> and
<) are used
Example
SELECT NAME FROM EMPLOYEES
WHERE EMPLOYEE IN
(SELECT COMPANY FROM COMPANIES
WHERE AVG_SALES > TOTAL_SALES)=
(SELECT COMPANY FROM COMPANIES
WHERE BRANCH=‘TEXAS’ )
7.
SQL includesa feature for testing whether a
sub query has any tuples in its result. EXISTS
uses a sub query as a condition, where the
condition is True if the sub query returns any
rows and False if the sub query does not
return any rows; this is a non intuitive feature
with few unique uses.
EXIST- This operator evaluate to true if the
resulting set is not empty.
NOT EXIST – This operator evaluated to true if
the resulting set is empty.
TEST EMPTY RELATIONS
The uniqueconstruct returns true if the
argument subquery contains no duplicate tuples.
SQL includes a feature for testing whether a sub
query has any duplicate tuples in its result The
UNIQUE construct return the value true if the
argument sub query contains no duplicate tuples.
Using the UNIQUE construct, we can write the
query ‘Find all customer
Example
SELECT NAME FROM EMPLOYEE
WHERE UNIQUE(
SELECT SALARY FROM EMPLOYE = 100000;
)