SQLUSA
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server 2008 Training Scripts
SQL Server 2005 Training Scripts
SQL Server Training Scripts
SQL 2008 GRAND SLAM
How to apply correlated subquery with averaging?

Execute the following Microsoft SQL Server T-SQL query in SSMS Query Analyzer to list all the order ids with order quantity in the bottom quartile:

-- SQL correlated subquery

SELECT DISTINCT o.OrderId, o.ProductID

FROM   Northwind.dbo.[Order Details] o

WHERE  Quantity <= (SELECT avg(Quantity) * .25

                    FROM   Northwind.dbo.[Order Details]

                    WHERE  o.ProductID = ProductID)

                   

/*

OrderId     ProductID

10609 1

10700 1

10838 1

11003 1

11005 1

10851 2

10722 2

....

*/

 

The World Leader in SQL Server 2008 Training
Order SQL 2008 GRAND SLAM Today!
The Future is just a CLICK away! Your Future!
SQLUSA.com Home Page

Copyright 2005-2010, SMI Corp. All Rights Reserved.

SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.