Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
4Activity
0 of .
Results for:
No results containing your search query
P. 1
Sub Queries

Sub Queries

Ratings: (0)|Views: 100|Likes:
Published by G.C.Reddy

More info:

Published by: G.C.Reddy on Feb 13, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/03/2010

pdf

text

original

 
www.gcreddy.com
SUB QUERIES
A sub query can be defined as a group of nested SELECT statements inside aSELECT, INSERT, UPDATE or DELETE statement. Sub query can also be used insidethe WHERE or HAVING clauses of the outer SELECT, INSERT, UPDATE or DELETEstatements. SELECT statements containing one or more sub queries are callednested queries.The command syntax is:A subquery must be enclosed within parentheses and cannot use ORDER BY,COMPUTE BY or FOR BROWSE clauses. SQL Server does not implement anyrestriction on level of nesting while using subqueries, SQL Server imposesrestrictions on the number of tables or views used in a subquery or a join.SQL Server evaluates the inner query first and returns the result to the outer queryfor the final result set.The outer query always depends on the evaluation result of the subquery.Subqueries can be divided into three catefories depending upon the values theyreturn;
Sub queries that operate on lists: this type of query returns single-column-multiple values results and are implemented using the IN clause. The syntaxis as follows:
Subqueries that are introduced with an unmodified comparison o-erator: thistype of query returns single column-single value results for outer queryevaluation and is implemented using unmodified comparisonoperators(operators without the ANY or ALL keywords) the syntax is asfollows:
Subqueries that check for the existence of data: this type of query checks forthe existence of records in a table that are used in the inner query, and
www.gcreddy.net
1
(SELECT [ALL|DISTINCT] suquery_select_list[FROM {table_nmae | view_name}[[,table_name2|view_name2}][..,{table_name16|view_name16{]][WHERE clause]GROUP BY clause][HAVING clause])WHERE expression [NOT] IN (subquery)
WHERE expression comparison_operator [ANY|ALL] (subquery)
 
www.gcreddy.com
returns either a TRUE or a FALSE VALUE based on the existence of data. Thisis implemented using the EXISTS keyword. The syntax is as follows:\ 
SubQueries With IN
A subquery introduced with IN returns zero or more values. Consider thefollowing example where all author ID’S, from the TITLEAUTHOR table, are displayedwhose books are sold:
SQL Server returns a list of all title IDs to the main query then lists all theauthors, whose books are sold, in the result set.
Consider the following example where the server returns a list of publisher IDs to themain query, and then determines whether each publisher’s pub_id is in that list:The inner query is evaluated first and then the result set is sent to the outer query.Consider another subquery with the IN clause:The inner query is evaluated first and then the result set is sent to the outer query.The NOT IN clause is used in the same way as the IN clause. Consider the followingexample.
www.gcreddy.net
2
 
WHERE [NOT] EXISTS (subquery)SELECT au_idFROM titleauthorWHERE title_id IN (SELECT title_id FROM sales)
SELECT publisher=pub_nameFROM publishersWHERE pub_id IN ( SELECT pub_id FROM titles WHERE type=’business’)SELECT type=type, Average=AVG(ytd_sales)FROM titlesWHERE type IN (SELECT type FROM titlesWHERE title=” the busy executive’s database guide” or title=’Is Anger the Enemy?’)GROUUP BY type
SELECT pub_id, pub_nameFROM publishersWHERE pub_id NOT IN (SELECT pub_id FROM titlesWHERE type=’mod_cook’)
 
www.gcreddy.com
Sub Queries with EXISTS
The subquery, when used with the EXISTS clause, always returns data interms of TRUE OR FALSE and passes the status to the outer query to produce theresults set. The subquery returns a TRUE value if the result set contains any rows.The query introduced with the EXISTS keyword differs from other queries. TheEXISTS keyword is not preceded by any column name, constant or there expressionand it contains an asterisk (*) in the SELECT list.Aggregate functions can also be used in subqueries. Consider the following examplewhich displays the titles of all those books for which the advance is more than theaverage advance of business related books.
Subquery Restrictions:
The restrictions imposed are:
The column list of the select statement of a subquery introduced with thecomparison operator can include only one column.
The column used in the WHERE clause of the outer query should becompatible with the column used in the select list of the inner query.
The DISTINCT keyword cannot be used with the subqueries that include theGROUP BY clause.
The ORDER BY clause, GROUP BY clause and INTO keyword cannot be used ina subquery, because a subquery cannot manipulate its result internally.
Do not specify more than one column name in the subquery introduced withthe EXISTS keyword.
A view created with a subquery cannot be updated.
www.gcreddy.net
3
 
1. SELECT pub_nameFROM publishersWHERE EXISTS (SELECT * FROM titles WHERE type=’business’)2. SELECT pub_nameFROM publishersWHERE EXISTS (SELECT * FROM publishers WHERE City=’Paris’)SELECT Title=titleFROM titlesWHERE advance>(SELECT AVG (advance) from titlesWHERE type=’business’)

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->