0% found this document useful (0 votes)
60 views25 pages

SQL Session 5v3

The document discusses different types of joins in SQL including inner joins, outer joins, and non-equi joins. It provides examples of joining two, three, and four tables using equi joins to retrieve data from multiple tables based on related columns. Various types of joins such as inner, outer, equi, and non-equi joins allow users to combine data from multiple tables to answer complex queries.

Uploaded by

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

SQL Session 5v3

The document discusses different types of joins in SQL including inner joins, outer joins, and non-equi joins. It provides examples of joining two, three, and four tables using equi joins to retrieve data from multiple tables based on related columns. Various types of joins such as inner, outer, equi, and non-equi joins allow users to combine data from multiple tables to answer complex queries.

Uploaded by

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

INTRODUCTION TO SQL- Session 5 (Week 3)

JOINS
JOINS : MULTI TABLE QUERIES
• Most useful queries request data from two or more tables in a database.
• A JOIN clause is used to combine rows from two or more tables, based on a related column
between them
• JOIN clause combines rows from two or more tables. Creates a set of rows in a temporary table.
• For example, these requests for data in the sample database draw data from two, three, and four
tables respectively
• SQL allows you to retrieve data that answers these requests through multi table queries that join
data from two or more tables.
• example :
“List all orders, showing the order number and amount, and the name and credit limit of the
customer who placed it.”
A REQUEST THAT SPANS TWO
TABLES
“List all orders, showing the
order number and amount, and
the name and credit limit of the
customer who placed it.”
There is a link between these
two tables. In each row of the
ORDERS table, the CUST
column contains the customer
number of the customer who
placed the order, which
matches the value in the
CUST_NUM column in one of
the rows in the CUSTOMERS
table.
1. Start by writing down the four column names for the “List all orders, showing the order number and
query results. Then move to the ORDERS table, and amount, and the company name and credit limit of
start with the first order. the customer who placed it.”
2. Look across the row to find the order number
(112961) and the order amount ($31,500.00), and copy
both values to the first row of query results.

3. Look across the row to find the number of the


customer who placed the order (2117), and move to the
CUSTOMERS table to find customer number 2117 by
searching the CUST_NUM column.

4. Move across the row of the CUSTOMERS table to


find the customer’s name (“J.P. Sinclair”) and credit
limit ($35,000.00), and copy them to the query results
table.

5. You’ve generated a row of query results! Move back


to the ORDERS table, and go to the next row. Repeat
the process, starting with Step 2, until you run out of
orders.
TYPES OF SQL JOIN
EQUI JOIN
 EQUI JOIN is a simple SQL join.
 Uses the equal sign(=) as the comparison operator for the condition

NON EQUI JOIN


 NON EQUI JOIN uses comparison operator other than the equal sign.
 The operators uses like >, <, >=, <= with the condition.
TYPES OF SQL EQUI JOIN
INNER JOIN
 Returns only matched rows from the participating tables.

OUTER JOIN
 Returns all rows from one table and
 Matching rows from the secondary table and
 Comparison columns should be equal in both the tables.
LIST OF SQL OUTER JOINS
LEFT JOIN OR LEFT OUTER JOIN : Returns all records from the left table, and
the matched records from the right table

RIGHT JOIN OR RIGHT OUTER JOIN : Returns all records from the right table,
and the matched records from the left table

FULL OUTER JOIN : Returns all records when there is a match in either left or right
table
SINGLE-JOINS (EQUI JOINS)
The process of forming pairs of rows by matching the contents of related columns is
called joining the tables
The resulting table (containing data from both of the original tables) is called a join
between the two tables.
A join based on an exact match between two columns is more precisely called an
equi-join.
Joins are the foundation of multitable query processing in SQL.
SQL handles multitable queries by matching columns, it should come as no surprise
that the SELECT statement for a multitable query contains a search condition that
specifies the column match
INNER JOIN
The INNER JOIN selects all rows from both participating tables as long
as there is a match between the columns.

An SQL INNER JOIN is same as JOIN clause, combining rows from


two or more tables.

Example: INNER JOIN


SELECT column_name(s)
SELECT * FROM table_A FROM table1
INNER JOIN table_B INNER JOIN table2
ON table_A.A=table_B.A; ON table1.column_name = table2.column_name;
EXAMPLES
1. List all orders showing order number, amount, customer name (“company”), and
the customer’s credit limit.
SELECT ORDER_NUM, AMOUNT, COMPANY,
CREDIT_LIMIT
SELECT ORDER_NUM, AMOUNT, COMPANY, CREDIT_LIMIT
FROM ORDERS, CUSTOMERS FROM ORDERS
WHERE CUST = CUST_NUM; INNER JOIN CUSTOMERS
ON CUST = CUST_NUM;

What’s Different ?

First, the FROM clause lists two tables


instead of just one.

Second, the search condition


CUST = CUST_NUM compares columns
from two different tables.
SALESREPS

2. List each salesperson and the city and region where they work.

SELECT NAME, CITY, REGION


FROM SALESREPS, OFFICES
WHERE REP_OFFICE = OFFICE;

OFFICES

SELECT NAME, CITY, REGION


FROM SALESREPS
INNERJOIN OFFICES
ON REP_OFFICE = OFFICE;
SALESREPS

3. List the offices and the names and titles of their managers.

SELECT CITY, NAME, TITLE


FROM OFFICES, SALESREPS
WHERE MGR = EMPL_NUM;

OFFICES
SELECT CITY, NAME, TITLE
FROM OFFICES
INNER JOIN SALESREPS
ON MGR = EMPL_NUM;
JOINS WITH ROW SELECTION
CRITERIA
1. List the offices with a target over $600,000 and their manager
information.

SELECT CITY, NAME, TITLE FROM OFFICES, SELECT CITY, NAME, TITLE
SALESREPS WHERE MGR = EMPL_NUM AND FROM OFFICES
TARGET > 600000.00; INNER JOIN SALESREPS
ON MGR = EMPL_NUM
WHERE TARGET > 600000.00;

Further filtering rows based on the where condition


4. List all the orders, showing amounts
and product descriptions.

SELECT ORDER_NUM, AMOUNT, DESCRIPTION


FROM ORDERS, PRODUCTS
WHERE MFR = MFR_ID
AND PRODUCT = PRODUCT_ID;

SELECT ORDER_NUM, AMOUNT, DESCRIPTION


FROM ORDERS
JOIN PRODUCTS
ON MFR = MFR_ID
AND PRODUCT = PRODUCT_ID;
NON-EQUI-JOINS
The term join applies to any query that combines data from two tables by comparing
the values in a pair of columns from the tables. Although joins based on equality
between matching columns (equi-joins) are by far the most common joins, SQL also
allows you to join tables based on other comparison operators.

List all combinations of salespeople and offices where the salesperson’s quota is
more than that office’s target, regardless of whether the salesperson works there.

SELECT NAME, QUOTA, CITY, TARGET SELECT NAME, QUOTA, CITY, TARGET
FROM SALESREPS, OFFICES FROM SALESREPS
WHERE QUOTA > TARGET; INNER JOIN OFFICES
WHERE QUOTA > TARGET;
COMBINE MULTIPLE JOINS
SELECT * FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id
INNER JOIN another_table
ON left_table.id = another_table.id;
JOINING THREE TABLES
5. List orders over $25,000, including the name of the salesperson who took the
order and the name of the customer who placed it.
SELECT ORDER_NUM, AMOUNT, COMPANY, NAME
FROM ORDERS, CUSTOMERS, SALESREPS
WHERE CUST = CUST_NUM
AND REP = EMPL_NUM
AND AMOUNT > 25000.00;
SELECT ORDER_NUM, AMOUNT, COMPANY, NAME
This query uses two foreign keys in the
FROM ORDERS
ORDERS table. The CUST column is a
INNER JOIN CUSTOMERS foreign key for the CUSTOMERS table,
ON CUST = CUST_NUM linking each order to the customer who
placed it. The REP column is a foreign key
INNER JOIN SALESREPS
for the SALESREPS table, linking each
ON REP = EMPL_NUM order to the salesperson who took it. This
WHERE AMOUNT > 25000.00; query links each order to its associated
customer and salesperson.
JOINING THREE TABLES
6. List the orders over $25,000, showing the name of the customer who placed the order and
the name of the salesperson assigned to that customer.
SELECT ORDER_NUM, AMOUNT, COMPANY, SELECT ORDER_NUM, AMOUNT, COMPANY,
NAME NAME
FROM ORDERS, CUSTOMERS, SALESREPS FROM ORDERS,
WHERE CUST = CUST_NUM INNER JOIN CUSTOMERS
AND CUST_REP = EMPL_NUM ON CUST = CUST_NUM
AND AMOUNT > 25000.00; INNER JOIN SALESREPS
ON CUST_REP = EMPL_NUM
AND AMOUNT > 25000.00;
A three table join
JONING FOUR TABLES
7. List the orders over $25,000, showing the name of the customer who placed the
order, the customer’s salesperson, and the office where the salesperson works.
SELECT ORDER_NUM, AMOUNT, COMPANY, NAME, CITY
SELECT ORDER_NUM, AMOUNT, COMPANY, NAME
FROM ORDERS, CUSTOMERS, SALESREPS, OFFICES FROM ORDERS
WHERE CUST = CUST_NUM INNER JOIN CUSTOMERS
AND CUST_REP = EMPL_NUM ON CUST = CUST_NUM
AND REP_OFFICE = OFFICE INNER JOIN SALESREPS
AND AMOUNT > 25000.00; ON CUST_REP = EMPL_NUM
INNER JOIN OFFICES
ON REP_OFFICE = OFFICE
AND AMOUNT > 25000.00;
PRACTICE
8. Find all orders received on a day when a new salesperson was hired.
The vast majority of multi-table queries are
SELECT ORDER_NUM, AMOUNT, ORDER_DATE, NAME
based on parent/child relationships, but SQL
FROM ORDERS, SALESREPS
does not require that the matching columns be
WHERE ORDER_DATE = HIRE_DATE;
related as a foreign key and primary key.

Any pair of columns from two tables can serve


as matching columns, provided they have
comparable data types (or data types that can be
converted to compatible types).
EXPLANATION The results of this query come
from pairs of rows in the
ORDERS and SALESREPS
tables
where the ORDER_DATE
happens to match the
HIRE_DATE for the salesperson.

Neither of these columns is a


foreign key or a primary key, and
the relationship
between the pairs of rows is
admittedly a strange one—the
only thing the matched orders
and salespeople have in common
is that they happen to have the
same dates.

However, SQL
happily joins the tables anyway.

You might also like