Lab Session 7
Lab Session 7
A join is an operation that allows you to query two or more tables to produce a result set that
incorporates rows and columns from each table. Joining is done on columns that are common to both
tables.
When tables are joined, SQL Server compares the values of specified columns row by row and then uses
the comparison results to combine the qualifying values into new rows. Joins can use two or more
tables.
To create a join, two additional keywords are used with from clause:
• The Join Keyword: Specifies which tables are to be joined and how to join them.
• The ON Keyword: Specifies the columns that the tables have in common.
• Specify the join condition based on the primary and foreign key.
• Use columns common to the specified tables to join the tables. These columns should have the
same or similar data types.
• Reference a table name if the column names of the joined tables are the same. Qualifying each
column name by using the table_name.column_name format.
• Limit the number of tables in a join because the more tables that you join, the longer SQL Server
takes to process your query.
• If a table has a composite primary key, you must reference the entire key in the ON clause when
you join tables.
• Inner Joins
• Outer Joins
• Cross Joins
INNER JOINS
Use Inner joins to obtain information from two separate tables and combine them in one result set.
Consider following guidelines for inner joins:
Inner joins are SQL Server default. You can abbreviate the INNER JOIN clause to JOIN. Specify the
columns that you want in result set in the select list.
Include a Where clause to restrict rows. Do not use a null value as a join condition.
USE PUBS
ON TITLES.PUB_ID = PUBLISHERS.PUB_ID
This example returns the names of products and the companies that supply the products. Products
without listed suppliers and suppliers without current products are not included in the result set.
USE NORTHWIND
ON PRODUCTS.SUPPLIERID = SUPPLIERS.SUPPLIERID
This example returns the names of customers who placed orders after 1/1/95. Notice that a WHERE
clause is used to restrict the rows.
USE NORTHWIND
ON ORDERS.CUSTOMERID = CUSTOMERS.CUSTOMERID
There are two types of Outer Joins: left and right. Left or Right Outer Joins combine rows from two
tables that match the join condition, plus any unmatched rows of either the left or right table as
specified in the JOIN clause. Rows that do not match the join condition display NULL in the result set.
You can also use full outer joins to display all rows in the joined tables, regardless of whether the tables
have any matching values. Consider following guidelines with these joins:
• SQL Server returns only unique rows when you use left or right outer joins.
• Use a Left Outer Join to display all rows from the first-named table (the table on the left of
expression).
• Use a right outer Join to display all rows from the second-named table (the table on the right of
expression).
• Abbreviations of LEFT OUTER JOIN and RIGHT OUTER JOIN are LEFT JOIN and RIGHT JOIN
respectively.
This example displays all customers with order dates. NULL in the orderdate column is returned for
customers who have not placed an order. Notice FISSA and Paris customers.
USE NORTHWIND
ON CUSTOMERS.CUSTOMERID = ORDERS.CUSTOMERID
The following SQL statement illustrates a left outer join between the titles and publishers tables to
include all titles, even those you do not have publisher information for:
USE PUBS
ON TITLES.PUB_ID = PUBLISHERS.PUB_ID
A right outer join between the titles and publishers tables will include all publishers, even those who
have no titles in the titles table.
USE PUBS
ON TITLES.PUB_ID = PUBLISHERS.PUB_ID
All rows in all joined tables are included, whether they are matched or not. For example, a full outer join
between titles and publishers shows all titles and all publishers, even those that have no match in the
other table.
USE PUBS
ON TITLES.PUB_ID = PUBLISHERS.PUB_ID
CROSS JOINS
Cross join display every combination of all rows in the joined tables. A common column is not required
to use cross joins. When crossed joins are used, SQL Server produces a Cartesian product in which the
number of rows in the result set is equal to the number of rows in the first table multiplied by the
number of rows in the second table. For example, if there are 8 rows in one table and 9 rows in another
table, SQL Server returns a total of 72 rows.
EXAMPLE OF CROSS JOIN
This example displays a cross join between the shippers and suppliers tables that is useful for listing all
of the possible ways that suppliers can ship their Products.
USE NORTHWIND
EXERCISE
1. Run all the example queries discussed in this session.
2. Write a query to display all employeeid and orderid from tables employees and orders [Hint: use
full outer join].
3. Write a query to display the all customerid and corresponding shipped date.[Hint: use left outer
join]
4. Write a query to display the titles and price from titles table and publishers name and city from
publishers table. [Hint: use inner joins]
5. Write a query to display all the possibilities of records from the customers and orders table.
[Hint: use cross joins]