0% found this document useful (0 votes)
15 views3 pages

Subqueries Subquery Definition and How They Are Written in The Select Statement

The document discusses different types of subqueries in SQL, including subqueries used with IN or NOT IN, comparison operators like ALL and ANY, and EXISTS and NOT EXISTS. It provides examples of each type of subquery and explains how they are structured and used.

Uploaded by

09whitedevil90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Subqueries Subquery Definition and How They Are Written in The Select Statement

The document discusses different types of subqueries in SQL, including subqueries used with IN or NOT IN, comparison operators like ALL and ANY, and EXISTS and NOT EXISTS. It provides examples of each type of subquery and explains how they are structured and used.

Uploaded by

09whitedevil90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

lOMoARcPSD|39225634

Subqueries - Subquery definition and how they are written in


the SELECT statement.
Intro To Database Appl (Montgomery College)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by White Devil ([email protected])
lOMoARcPSD|39225634

SUBQUERIES
A subquery is a SELECT statement that returns a single value and is nested inside a SELECT, INSERT,
UPDATE or DELETE statement or inside another subquery.

A subquery can be used anywhere an expression is allowed. A subquery is also called an inner query or
inner select, while the statement containing a subquery is called an outer select.

In the following example, a subquery is nested in the WHERE clause of the outer SELECT statement:

Subquery – Example
USE Northwind
SELECT ProductName
FROM Products
WHERE UnitPrice = {
SELECT UnitPrice
FROM Products
WHERE ProductName = ‘Sir Rodney’s Scones’}

If a table only appears in a subquery and not in the outer query, then columns from that table cannot be
included in the output (the select list of the outer query).

Types of Subqueries
Subqueries can be specified in many places within a SELECT statement. Statements that include a
subquery usually take one of the following formats:

WHERE <expression> [NOT] IN (<subquery>)


WHERE <expression> <comparison_operator> [ANY | ALL] (<subquery>)
WHERE [NOT] EXISTS (<subquery>)

Subqueries that are used with IN or NOT IN


The result of a subquery introduced with IN (or with NOT IN) is a list of zero or more values.

After the subquery returns the result, the outer query makes use of it.

In the following example, a subquery is nested inside the WHERE clause, and the IN keyword is used:

Subquery - Example
USE Pubs
SELECT Pub_name
FROM Publishers
WHERE Pub_id IN
(
SELECT Pub_id
FROM Titles
WHERE Type = ‘business’ )

Subqueries introduced with the NOT IN keywords also return a list of zero or more values.

Downloaded by White Devil ([email protected])


lOMoARcPSD|39225634

The query is exactly the same as the one in subqueries with IN, except that NOT IN is substituted for IN.

Subqueries that Are Used with Comparison Operators


Comparison operators that introduce a subquery can be modified with the keyword ALL or ANY.

Subqueries introduced with a modified comparison operator return a list of zero or more values and can
include a GROUP BY or HAVING clause.

These subqueries can be restated with EXISTS.

The ALL and ANY keywords each compare a scalar value with a single-column set of values.

The ALL keyword applies to every value, and the ANY keyword applies to at least one value.

In the following example, the greater than (>) comparison operator is used with the ANY keyword:

Subquery - Example
USE Pubs
SELECT Title
FROM Titles
WHERE Advance > ANY
(
SELECT Advance
FROM Publishers INNER JOIN Titles ON Titles.Pub_id = Publishers.Pub_id AND Pub_name = ‘Algodata
Infosystems’
)

Subqueries that Are Used with EXISTS and NOT EXISTS


When a subquery is introduced with the keyword EXISTS, it functions as an existence test.

The WHERE clause of the outer query tests for the existence of rows returned by the subquery.

The subquery does not actually produce any data; instead, it returns a value of TRUE or FALSE.

In the following example, the WHERE clause in the outer SELECT statement contains the subquery and
uses the EXISTS keyword.

Subquery – Example
USE Pubs
SELECT Pub_name
FROM Publishers
WHERE EXISTS
(
SELECT *
FROM Titles
WHERE Titles.Pub_id = Publishers.Pub_id AND Type = ‘Business’
)

Downloaded by White Devil ([email protected])

You might also like