0% found this document useful (0 votes)
76 views2 pages

Inner Join: Listing 5.7

An inner join combines two tables and returns rows that have matching values in the columns used to link the tables. Specifically, an inner join evaluates the join condition for each row in both tables and includes the row in the results only if the condition is met. For example, an inner join of a Products table and a Suppliers table on their supplierid columns would return each product and its matching supplier.

Uploaded by

dost_999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Inner Join: Listing 5.7

An inner join combines two tables and returns rows that have matching values in the columns used to link the tables. Specifically, an inner join evaluates the join condition for each row in both tables and includes the row in the results only if the condition is met. For example, an inner join of a Products table and a Suppliers table on their supplierid columns would return each product and its matching supplier.

Uploaded by

dost_999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

156

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

Chapter 5. Querying Multiple Tables: JOIN s


9MishiKobeNikuTokyoTraders
10IkuraTokyoTraders
11QuesoCabralesCooperativadeQuesos'LasCabras'
12QuesoManchegoLaPastoraCooperativadeQuesos'LasCabras'
13KonbuMayumi's
14TofuMayumi's
15GenenShouyuMayumi's
Figure 5.1 shows a graphical representation of the INNERJOINperformed in Listing 5.7. In
this figure, you
can see how an INNERJOINis processed: For every row in the first table, SQL Server goes
through the
second table trying to find a corresponding row based on the join column (supplieridin this
case), and if a
row matches,it is returned in the result set.

You might also like