Microsoft SQL Server 2005 Best Practices

How to use the ALL operator?

 

Execute the following script in Query Editor to obtain best selling items:

USE AdventureWorks;

WITH cteOrderQty
AS
(
SELECT sod.OrderQty
FROM Sales.SalesOrderHeader AS soh
INNER JOIN Sales.SalesOrderDetail AS sod
ON soh.SalesOrderId=sod.SalesOrderID
WHERE soh.OrderDate BETWEEN '6/30/2004' AND '1/1/2005'
)

SELECT DISTINCT p.ProductID, ProductName=p.Name
FROM Sales.SalesOrderHeader AS soh
INNER JOIN Sales.SalesOrderDetail AS sod
ON soh.SalesOrderID = sod.SalesOrderID
INNER JOIN Production.Product AS p
ON p.ProductID=sod.ProductID
WHERE soh.OrderDate BETWEEN '1/1/2003' AND '6/30/2003'
AND sod.OrderQty > ALL
(SELECT * FROM cteOrderQty )
ORDER BY ProductName
GO


 

The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page