0% found this document useful (0 votes)
52 views17 pages

12 - Chapter 6 - Subqueries

This document discusses different types of subqueries in SQL, including subqueries that return scalar values, conditions involving entire relations or tuples, correlated subqueries, and subqueries used in the FROM clause. Correlated subqueries must be evaluated once for each tuple in the outer query and should be avoided when possible due to performance issues. The document provides examples to illustrate each type of subquery.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views17 pages

12 - Chapter 6 - Subqueries

This document discusses different types of subqueries in SQL, including subqueries that return scalar values, conditions involving entire relations or tuples, correlated subqueries, and subqueries used in the FROM clause. Correlated subqueries must be evaluated once for each tuple in the outer query and should be avoided when possible due to performance issues. The document provides examples to illustrate each type of subquery.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Database Systems

Session 13
Chapter 6 – Subqueries
Objectives

1 Know what are subqueries and when to use them

2 Know what are correlated subqueries

3 Understand why we should not over-use the correlated subqueries


Contents

1 Subqueries that produce scalar values

2 Conditions involving relations

3 Conditions involving tuples

4 Correlated subqueries

5 Subqueries in FROM clauses


Sub-Queries

 In SQL, a query that is part of another is called a subquery


 There are a number of ways that subqueries can be used:
 Subqueries return a single constant which can be compared with
another value in a WHERE clause.
 Subqueries return relations which can be used in WHERE clauses.
 Subqueries appear in FROM clauses, followed by a tuple variable
representing the tuples in the result of the subquery
Example: Sub-queries
Sub-Queries Syntax

SELECT [ ALL | DISTINCT ]


    [ TOP n [ PERCENT ] ]
* | {column_name | expression [alias],…}
FROM tableName
WHERE expression OPERATOR
(SELECT select_list
FROM table)

 The subquery (inner query) executes once before


the main query.
 The result of the subquery is used by the main query
(outer query).
1 – Subqueries that produce scalar values

 An atomic value that can appear as one


component of a tuple is referred to as a scalar.
 Sometimes, a SELECT-FROM-WHERE
expression produce only one scalar value:
 The salary of Abel
 The maximum Price of Products
 If so, we can use this SELECT-FROM-WHERE
expression, surrounded by parentheses, as if it
were a constant
Example: Sub-queries that produce scalar values
2 – Conditions involving relations

 There are a number of SQL operators that we


can apply to a relation R and produce a boolean
result:
 EXISTS R: is a condition that is true if and only if R is
not empty
 s IN R: is true if and only if s is equal to one of the
values in R
 s > ALL R: is true if and only if s is greater than every
value in unary relation R
 s > ANY R: is true if and only if s is greater than at
least one value in unary relation R
 All the above operators can be negated by
putting NOT in front of the entire expression
3 – Conditions involving tuples

A tuple in SQL is represented by a


parenthesized list of scalar values
Exp:
 (123, ‘Foo’)
 (456,’Dump’)
 (name, address, networth)
If a tuple t has the same number of
components as a relation R, then it makes
sense to compare t and R in expressions
involving IN, ALL, ANY operators
Example: Conditions involving tuples

 movies(title,year, length, genre, studioName)


 starsIn(movieTitle, movieYear, starName)
 Find StudioName of movies for which ‘Harrison Ford’
played.

 Note: You can not run the above code in SQL Server but in
Oracle, DB2, MySQL
4 - Correlated Sub-Queries

 A correlated subquery is one way of reading every row in a


table and comparing values in each row against related data.
 It is used whenever a sub-query must return a different result
or set of results for each candidate row considered by the
main query.
Example: Correlated Sub-queries
The differences between
simple subqueries and correlated subqueries

 The simple subqueries can be evaluated once


and for all, and the result used in a higher-level
query
 A correlated subqueries require the subqueries
to be evaluated many times, once for each
assignment of a value to some term in the
subqueries that come from a tuple variable
outside the subqueries
 So, you should avoid using correlated
subqueries whenever possible
Example: Correlated Sub-queries
5 – Subqueries in FROM clauses

 Another use for subqueries is as relations in a


FROM clause:

 SELECT name
FROM movieExec,
(SELECT producerC#
FROM movies, starsIn
WHERE title = movieTitle
AND year = movieYear
AND starName = ‘Harrison
Ford’
) as Prod
WHERE cert# = Prod.producerC#

You might also like