90 likes | 205 Views
ITE 370: Nested Queries. Jeffrey P. Landry University of South Alabama. Nested Query-Defined. A nested query is a query within a query A complete SELECT statement embedded within another SELECT statement Nested queries are also called subqueries. Query Results.
E N D
ITE 370: Nested Queries Jeffrey P. Landry University of South Alabama University of South Alabama School of CIS
Nested Query-Defined • A nested query is a query within a query • A complete SELECT statement embedded within another SELECT statement • Nested queries are also called subqueries University of South Alabama School of CIS
Query Results • The results of various queries can be thought of as: • a table (multiple rows and columns) • a list of values (a single column) • or a single value (one row, one column, i.e. a cell) • Subqueries may be placed wherever you can place a table, list of values, or a single value University of South Alabama School of CIS
Subqueries that Return a Single Value • Queries that return a single value (cell) can be used wherever a literal value is allowed • Example: List all baseball players who hit more home runs than the average SELECT Round(Avg(HR)) FROM Player SELECT First, Last, HR FROM PLAYER WHERE HR > (SELECT Avg(HR) FROM Player) University of South Alabama School of CIS
Subqueries that Return a List of Values • Queries that return a list of values (from one column) can be used wherever a value list is required (eg. the IN predicate) • Example: List all National League players SELECT TeamId FROM Team WHERE League = ‘NL’ { Same as (2, 3, 4, 8, 9, …) } SELECT Last, First FROM Player WHERE TeamId IN (SELECT TeamId FROM Team WHERE League = ‘NL’) University of South Alabama School of CIS
Subqueries that Return a Table • Queries that return a table (multiple rows and columns) can be used wherever a table is required • For example, in the FROM statement • Use aliasing (AS <tablename> • Example: Players that lead their team in home runs University of South Alabama School of CIS
Example of Nested Query that Return a Table SELECT TeamId, Max(HR) -- set of team-leading FROM Player -- HR quantities GROUP BY Team -- find out who the team leaders are SELECT Last, First, Player.TeamId, HR FROM Player, (SELECT TeamId, Max(HR) AS TopHR FROM Player GROUP BY TeamId) As TopTeamHR WHERE (Player.TeamId = TopTeamHR.TeamId) AND (Player.HR = TopTeamHR.TopHR) University of South Alabama School of CIS
Nested Queries and EXISTS • EXISTS asks “does at least one row exist?” • Accepts a SELECT query as its lone parameter • Interprets SELECT in boolean terms • True means there is at least one row that satisfies the query • False means empty set is returned • The nested SELECT query must refer to the outer query (correlated query), and is executed for each row in the outer query University of South Alabama School of CIS
EXISTS example Problem: List all the teams that have no players Solution: SELECT * FROM Team WHERE NOT EXISTS (SELECT * FROM Player WHERE Player.TeamId=Team.TeamId); University of South Alabama School of CIS