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

Sub Queries

Subqueries allow SELECT statements to be embedded within other queries. They can take the place of a constant value, return a list of values for comparisons, or act as correlated values that vary based on each row processed. Subqueries provide an alternative way to write queries and can perform functions that may not be possible without their use, such as eliminating table joins or comparing columns across different tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

Sub Queries

Subqueries allow SELECT statements to be embedded within other queries. They can take the place of a constant value, return a list of values for comparisons, or act as correlated values that vary based on each row processed. Subqueries provide an alternative way to write queries and can perform functions that may not be possible without their use, such as eliminating table joins or comparing columns across different tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Subqueries: Subqueries are similar to SELECT chaining.

While SELECT chaining combines SELECTs on the same level in a query, however, subqueries allow SELECTs to be embedded inside other queries. They can perform several functions:

They can take the place of a constant. They can take the place of a constant yet vary based on the row being processed. They can return a list of values for use in a comparison.

Subqueries can be quite complicated. If you have trouble understanding this section, skip over it and return to it later. Subqueries as Constants A subquery, also called a subselect, can replace a constant in a query. While a constant never changes, a subquery's value is computed every time the query is executed. Although we have used table aliases in the subquery for clarity, they are not required. A column name with no table specification is automatically paired with a table in the current subquery. If no matching table is found in the current subquery, higher parts of the query are searched for a match. The state, firstname, and lastname in the subquery refer to the instance of the friend table in the subquery. The same column names in the upper query automatically refer to the friend instance in that query. If a column name matches two tables in the same subquery, an error is returned, indicating the column is ambiguous. Subqueries can also eliminate table joins. Subqueries as Correlated Values In addition to acting as constants in queries, subqueries can act as correlated values. Correlated values vary based on the row being processed. A normal subquery is evaluated once and its value used by the upper query. In a correlated subquery, the subquery is evaluated repeatedly for every row processed. Subqueries as Lists of Values The previous subqueries returned one row of data to the upper query. If any of the previous subqueries returned more than one row, an error would be generated. Normal comparison operators like equal and less-than expect a single value on the left and on the right. For example, equality expects one value on the left of the equals sign (=) and one on the right for example, col = 3. Two special comparisons, IN and NOT IN, allow multiple values to appear on the

right side. For example, the test col IN (1,2,3,4) compares col against four values. If col equals any of the four values, the comparison will return true and output the row. The test col NOT IN (1,2,3,4) will return true if col does not equal any of the four values. You can specify an unlimited number of values on the right side of an IN or NOT IN comparison. More importantly, a subquery (instead of a constant) can be placed on the right side. It can then return multiple rows. NOT IN and Subqueries with NULL Values If a NOT IN subquery returns a NULL value, the NOT IN comparison always returns false. NOT IN requires the upper column to be not equal to every value returned by the subquery. Because all comparisons with NULL return false--even inequality comparisons--NOT IN returns false. We can prevent NULL values from reaching the upper query by adding IS NOT NULL to the subquery. An IN subquery doesnot have this problem with NULLs. Subqueries Returning Multiple Columns Although most subqueries return a single column to the upper query, it is possible to handle subqueries returning more than one column. For example, the test WHERE (7, 3) IN (SELECT col1, col2 FROM subtable) returns true if the subquery returns a row with 7 in the first column and 3 in the second column. The test WHERE (uppercol1, uppercol2) IN (SELECT col1, col2 FROM subtable) performs equality comparisons between the upper query's two columns and the subquery's two columns. Multiple columns in the upper query can then be compared with multiple columns in the subquery. Of course, the number of values specified on the left of IN or NOT IN must be the same as the number of columns returned by the subquery. ANY, ALL, and EXISTS Clauses IN and NOT IN are special cases of the more generic subquery clauses ANY, ALL, and EXISTS. ANY will return true if the comparison operator is true for any value in the subquery. For example, the test col = ANY(5,7,9) returns true if col equals any of the three values. ALL requires all subquery values to compare as true, so col != ALL(5,7,9) returns true if col is not equal to all three values. IN() is the same as = ANY(), and NOT IN() is the same as <> ALL(). Normally, you can use operators like equal and greater-than only with subqueries returning one row. With ANY and ALL, however, comparisons can be made with subqueries returning multiple rows. They allow you to specify whether any or all of the subquery values, respectively, must compare as true.

EXISTS returns true if the subquery returns any rows, and NOT EXISTS returns true if the subquery returns no rows. EXISTS permits complex comparisons of upper-query values inside the subquery. For example, two upper-query variables can be compared in the subquery's WHERE clause. EXISTS and NOT EXISTS do not specify anything in the upper query, so it does not matter which columns are returned by the subquery. A subquery can represent a fixed value, a correlated value, or a list of values. You can use any number of subqueries. You can also nest subqueries inside other subqueries. In some cases, subqueries simply provide an alternative way to phrase a query. In others, a subquery is the only way to produce the desired result. Eg) /* SELECT statement built using a subquery. */ SELECT Name FROM AdventureWorks2008R2.Production.Product WHERE ListPrice=(SELECT ListPrice FROM AdventureWorks2008R2.Production.Product WHERE Name = 'Chain ring Bolts'); /* SELECT statement built using a join that returns The same result set. */ SELECT Prd1. Name FROM AdventureWorks2008R2.Production.Product AS Prd1 JOIN AdventureWorks2008R2.Production.Product AS Prd2 ON (Prd1.ListPrice = Prd2.ListPrice) WHERE Prd2. Name = 'Chain ring Bolts';

You might also like