Wednesday, 30 March 2011

SQL SubQuery

Table Store_Information
store_nameSalesDate
Los Angeles$1500Jan-05-1999
San Diego$250Jan-07-1999
Los Angeles$300Jan-08-1999
Boston$700Jan-08-1999
 
Table Geography
region_namestore_name
EastBoston
EastNew York
WestLos Angeles
WestSan Diego
and we want to use a subquery to find the sales of all stores in the West region. To do so, we use the following SQL statement:

SELECT SUM(Sales) FROM Store_Information
WHERE Store_name IN
(SELECT store_name FROM Geography
WHERE region_name = 'West')


Result:

SUM(Sales)
2050

No comments:

Post a Comment