Inner Join: Listing 5.7
Inner Join: Listing 5.7
INNERJOIN
In general, a JOINoperationcombines two or more tables, generating one result set from the
information
stored in such tables. These tables should have similar columns, commonly foreign keys, which
are the ones
used in JOINoperations to link tables. Also, as you might have noticed in previous examples,
the columns
involved in a JOINcondition don't need to have the same name.
An INNERJOINoperation between two tables returns all common rows in these two tables.
Specifically,
INNERJOINevaluates the JOINcondition for each row in both tables and if this condition is
met, the row is
included in the result set. For example, if you want to retrieve information about products and the
name of the
supplier of each product, the Productstable and the Supplierstable must be joined
(through an INNER
JOIN), thus generating a result set with all the information needed (products and suppliers).
Listing 5.7
shows the query that retrieves eachproduct and its supplier.
Listing 5.7 Using INNERJOINto Retrieve Information from Two Tables
USENorthwind
SELECTproductid,productname,companyname
FROMProductsINNERJOINSuppliers
ONProducts.supplierid=Suppliers.supplierid
GO
Partialresult
productidproductnamecompanyname
1ChaiExoticLiquids
2ChangExoticLiquids
3AniseedSyrupExoticLiquids
4ChefAnton'sCajunSeasoningNewOrleansCajunDelights
5ChefAnton'sGumboMixNewOrleansCajunDelights
6Grandma'sBoysenberrySpreadGrandmaKelly'sHomestead
7UncleBob'sOrganicDriedPearsGrandmaKelly'sHomestead
8NorthwoodsCranberrySauceGrandmaKelly'sHomestead