Credits

Saturday, August 13, 2011

SQL's Join Command

One of the handy and useful implementation of SQL commands  is the use of JOIN to connect several tables to filter queries among others.

For instance, we can select records that comply a certain condition and connecting two tables at least can only be made possible through the use of JOIN. Take the example below excerpted from http://www.1keydata.com/sql/sqljoins.html :

SELECT  A1.region_name REGION, SUM(A2.Sales) SALES
FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name
GROUP BY A1.region_name



TableStore_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
Result:
REGION   SALES
East   $700
West   $2050
We can also use inner join in the given example and we can write it as: SELECT  A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1 inner join Store_Information A2 on  A1.store_name = A2.store_name GROUP BY A1.region_name

AddThis