SQL Session 5v3
SQL Session 5v3
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.
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.
What’s Different ?
2. List each salesperson and the city and region where they work.
OFFICES
3. List the offices and the names and titles of their managers.
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;
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.
However, SQL
happily joins the tables anyway.