0% found this document useful (0 votes)
26 views5 pages

Lab Session 7

This document discusses different types of joins in SQL including inner, outer, and cross joins. It provides examples of joins between tables to retrieve and combine related data. Inner joins return rows that match the join condition from both tables. Outer joins return all rows from one or both tables, including non-matching rows. Cross joins return the cartesian product of all rows from both tables. The document also provides exercises for the reader to write queries using various join types.

Uploaded by

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

Lab Session 7

This document discusses different types of joins in SQL including inner, outer, and cross joins. It provides examples of joins between tables to retrieve and combine related data. Inner joins return rows that match the join condition from both tables. Outer joins return all rows from one or both tables, including non-matching rows. Cross joins return the cartesian product of all rows from both tables. The document also provides exercises for the reader to write queries using various join types.

Uploaded by

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

LAB SESSION # 7: WORKING WITH JOINS

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.

When implementing joins, consider the following facts and figures:

• 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.

• You can include a series of joins within a SELECT statement.

• If a table has a composite primary key, you must reference the entire key in the ON clause when
you join tables.

There are three types of joins:

• 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.

EXAMPLES OF INNER JOINS


You can join the titles and publishers tables to create a result set that shows the publisher name for
each title. In an inner join, titles for which you do not have publisher information are not included in the
result set, nor are publishers with no titles. The resulting SQL for such a join might look like this:

USE PUBS

SELECT TITLE, PUB_NAME FROM TITLES INNER JOIN PUBLISHERS

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

SELECT PRODUCTNAME, COMPANYNAME FROM PRODUCTS INNER JOIN SUPPLIERS

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

SELECT COMPANYNAME, ORDERDATE FROM ORDERS INNER JOIN CUSTOMERS

ON ORDERS.CUSTOMERID = CUSTOMERS.CUSTOMERID

WHERE ORDERDATE > '1/1/95'


OUTER JOINS

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).

• Do not use null value as a join condition.

• Abbreviations of LEFT OUTER JOIN and RIGHT OUTER JOIN are LEFT JOIN and RIGHT JOIN
respectively.

• You can use outer joins between two tables only.

EXAMPLES OF OUTER JOINS

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

SELECT COMPANYNAME, CUSTOMERS.CUSTOMERID, ORDERDATE

FROM CUSTOMERS LEFT OUTER JOIN ORDERS

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

SELECT TITLES.TITLE_ID, TITLES.TITLE, PUBLISHERS.PUB_NAME

FROM TITLES LEFT OUTER JOIN PUBLISHERS

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

SELECT TITLES.TITLE_ID, TITLES.TITLE, PUBLISHERS.PUB_NAME

FROM TITLES RIGHT OUTER JOIN PUBLISHERS

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

SELECT TITLES.TITLE_ID, TITLES.TITLE, PUBLISHERS.PUB_NAME

FROM TITLES FULL OUTER JOIN PUBLISHERS

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

SELECT SUPPLIERS.COMPANYNAME, SHIPPERS.COMPANYNAME

FROM SUPPLIERS CROSS JOIN SHIPPERS

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]

You might also like