Solved

how does a subquery work

Posted on 2006-10-19
3
909 Views
Last Modified: 2012-08-13
I am searching for an article which tells me how a subquery works in T-SQL. I have a query like:

SELECT colA, colB
FROM TableA a
WHERE
      a.colA = (SELECT max(colA) FROM TableAB ab
                  WHERE a.colC = ab.colC)

how would this query work?
0
Comment
Question by:srinivas_vemla
  • 3
3 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
Comment Utility
SELECT colA, colB
FROM TableA a
WHERE a.colA =
                     (
                          SELECT max(colA)
                          FROM TableAB ab
                          WHERE a.colC = ab.colC
                     )

This is not subquery.  This is Co-Related Subquery.  
The co-related subquery will be executed for each row in TableA.

Eg:  TableA has 10 rows.  So the subquery will be executed 10 times for each row in TableA.


itsvtk


0
 
LVL 14

Expert Comment

by:Thandava Vallepalli
Comment Utility
Please find below the description from Books online


Correlated Subqueries
==================
Many queries can be evaluated by executing the subquery once and substituting the resulting value or values into the WHERE clause of the outer query. In queries that include a correlated subquery (also known as a repeating subquery), the subquery depends on the outer query for its values. This means that the subquery is executed repeatedly, once for each row that might be selected by the outer query.

This query finds the names of all authors who earn 100 percent of the shared royalty (royaltyper) on a book.

USE pubs
SELECT au_lname, au_fname
FROM authors
WHERE 100 IN
   (SELECT royaltyper
   FROM titleauthor
   WHERE titleauthor.au_ID = authors.au_id)

Here is the result set:

au_lname                                 au_fname            
---------------------------------------- --------------------
White                                    Johnson              
Green                                    Marjorie            
Carson                                   Cheryl              
Straight                                 Dean                
Locksley                                 Charlene            
Blotchet-Halls                           Reginald            
del Castillo                             Innes                
Panteley                                 Sylvia              
Ringer                                   Albert              

(9 row(s) affected)

Unlike most of the subqueries shown earlier, the subquery in this statement cannot be resolved independently of the main query. It needs a value for authors.au_id, but this value is a variable. It changes as Microsoft® SQL Server™ examines different rows of the authors table.

That is exactly how this query is evaluated: SQL Server considers each row of the authors table for inclusion in the results by substituting the value in each row into the inner query. For example, if SQL Server first examines the row for Cheryl Carson, the variable authors.au_id takes the value 238-95-7766, which SQL Server substitutes into the inner query.

USE pubs
SELECT royaltyper
FROM titleauthor
WHERE au_id = '238-95-7766'

The result is 100, so the outer query evaluates to:

USE pubs
SELECT au_lname, au_fname
FROM authors
WHERE 100 IN (100)

Because this is true, the row for Cheryl Carson is included in the results. Go through the same procedure with the row for Abraham Bennet; you'll see that this row is not included in the results.

itsvtk
0
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 250 total points
Comment Utility
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now