Inner Join

An inner join in SQL returns rows where there is at least one match on both tables. Let's assume that we have the following two tables,

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

We want to find out sales by store, and we only want to see stores with sales listed in the report. To do this, we can use the following SQL statement using INNER JOIN:

SELECT A1.store_name STORE, SUM(A2.Sales) SALES
FROM Geography A1
INNER JOIN Store_Information A2
ON A1.store_name = A2.store_name
GROUP BY A1.store_name

Result:

STORE   SALES
Los Angeles   $1800
San Diego   $250
Boston   $700

By using INNER JOIN, the result shows 3 stores, even though we are selecting from the Geography table, which has 4 rows. The row "New York" is not selected because it is not present in the Store_Information table.

Next: SQL Outer Join