0% found this document useful (0 votes)
43 views39 pages

Lesson3-MYSQL JOIN-2

The document discusses different types of MySQL joins including inner, left, right and cross joins. It provides examples of using inner joins to retrieve data from multiple tables, left joins to return all records from the left table, and using joins with group by and aggregate functions.

Uploaded by

neriusndyanabo
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)
43 views39 pages

Lesson3-MYSQL JOIN-2

The document discusses different types of MySQL joins including inner, left, right and cross joins. It provides examples of using inner joins to retrieve data from multiple tables, left joins to return all records from the left table, and using joins with group by and aggregate functions.

Uploaded by

neriusndyanabo
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/ 39

MY SQL JOINS

By: Sgt Salum Seif Said, Email: [email protected]


What are MySQL Joins?

• JOINS are used with SELECT


statement.
• It is used to retrieve data from multiple
tables.
• It is performed whenever you need to
fetch records from two or more tables.
Types of MySQL joins

There are four types of MySQL joins:


• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• CROSS JOIN
• INNER JOIN: Returns
records that have
matching values in both
tables
• LEFT JOIN: Returns all
records from the left table,
and the matched records
from the right table
• RIGHT JOIN: Returns all
records from the right
table, and the matched
records from the left table
• CROSS JOIN: Returns all
records from both tables
MySQL INNER JOIN

• The INNER JOIN matches each row in one


table with every row in other tables and
allows you to query rows that contain
columns from both tables.
• The INNER JOIN is an optional clause of
the SELECT statement.
• It appears immediately after the FROM
clause.
• Here is the syntax of the INNER JOIN
clause:

SELECT
select_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;
In this syntax:
• First, specify the main table that appears in
the FROM clause (t1).
• Second, specify the table that will be joined
with the main table, which appears in the
INNER JOIN clause (t2, t3,…).
• Third, specify a join condition after the ON
keyword of the INNER JOIN clause. The
join condition specifies the rule for matching
rows between the main table and the table
that appeared in the INNER JOIN clause.
• Assuming that you want to join two tables
t1 and t2.

• The following statement illustrates how to


join two tables t1 and t2 using the INNER
JOIN clause:

SELECT
select_list
FROM
t1
INNER JOIN t2 ON join_condition;
• The INNER JOIN clause compares each
row in the t1 table with every row in the t2
table based on the join condition.

• If rows from both tables cause the join


condition to evaluate to TRUE, the INNER
JOIN creates a new row whose columns
contain all columns of rows from the tables
and includes this new row in the result set.
Otherwise, the INNER JOIN just ignores the
rows.
• In case no row between tables causes the
join condition to evaluate to TRUE, the
INNER JOIN returns an empty result set.
This logic is also applied when you join
more than 2 tables.

• The following Venn diagram illustrates how


the INNER JOIN clause works:
MySQL INNER JOIN
examples
• Let’s look at the products and productlines
tables in the sample database.
• In this diagram, the table products has the
column productLine that references the
column productline of the table productlines
• The column productLine in the table
products is called the foreign key column.
• Typically, you join tables that have foreign
key relationships like the productlines and
products tables.
• Suppose you want to get:
• The productCode and productName from
the products table.
• The textDescription of product lines from
the productlines table.

SELECT productCode, productName,


textDescription
FROM
products t1
INNER JOIN productlines t2
ON t1.productline = t2.productline;
• Because the joined columns of both tables
have the same name productline, you can
use the USING syntax:

SELECT productCode, productName,


textDescription
FROM products
INNER JOIN productlines USING
(productline);

• The query returns the same result set.


However, the USING syntax is much shorter
and cleaner.
MySQL INNER JOIN WITH
GROUP BY clause
• The GROUP BY statement groups rows
that have the same values into summary
rows, like "find the number of customers in
each country".

• The GROUP BY statement is often used


with aggregate functions (COUNT(), MAX(),
MIN(), SUM(), AVG()) to group the result-set
by one or more columns.
Example
• See the following orders and orderdetails
tables:
• This query returns the order number, order
status, and total sales from the orders and
orderdetails tables using the INNER JOIN
clause with the GROUP BYclause:
SELECT
t1.orderNumber,
t1.status,
SUM(quantityOrdered * priceEach) total
FROM
orders t1
INNER JOIN orderdetails t2
ON t1.orderNumber = t2.orderNumber
GROUP BY orderNumber;
MySQL INNER JOIN – Three
tables example
• See the following products, orders and
orderdetails tables:
• This query uses two INNER JOIN clauses to
join three tables: orders, orderdetails, and
products:
SELECT orderNumber,orderDate,
orderLineNumber,productName,
quantityOrdered,priceEach
FROM orders
INNER JOIN orderdetails USING
(orderNumber)
INNER JOIN products USING (productCode)
ORDER BY
orderNumber, orderLineNumber;
MySQL INNER JOIN – Four
tables example
• See the
following orders,
orderdetails,
customers and
products tables:
• This example uses three INNER JOIN clauses to
query data from the four tables above:
SELECT
orderNumber,orderDate,customerName,orderLineN
umber,productName,quantityOrdered,priceEach
FROM orders
INNER JOIN orderdetails USING (orderNumber)
INNER JOIN products USING (productCode)
INNER JOIN customers USING (customerNumber)
ORDER BY orderNumber, orderLineNumber;
MySQL INNER JOIN USING
OTHER OPERATORS
• So far, you have seen that the join condition
used the equal operator (=) for matching
rows.

• In addition to the equal operator (=), you can


use other operators such as greater than
( >), less than ( <), and not-equal ( <>)
operator to form the join condition.
• The following query uses a less-than ( <)
join to find the sales price of the product
whose code is S10_1678 that is less than
the manufacturer’s suggested retail price
(MSRP) for that product.

SELECT orderNumber,productName,
msrp,priceEach
FROM products p
INNER JOIN orderdetails o
ON p.productcode = o.productcode
AND p.msrp > o.priceEach
WHERE p.productcode = 'S10_1678';
MySQL LEFT JOIN
• The LEFT JOIN allows you to query data
from two or more tables. Similar to the
INNER JOIN clause, the LEFT JOIN is an
optional clause of the SELECT statement,
which appears immediately after the FROM
clause.
• Suppose that you want to join two tables t1
and t2.
• The following statement shows how to use
the LEFT JOIN clause to join the two tables:
• The following statement shows how to use
the LEFT JOIN clause to join the two tables:
SELECT
select_list
FROM
t1
LEFT JOIN t2 ON
join_condition;
• When you use the LEFT JOIN clause, the
concepts of the left table and the right table
are introduced.

• In the above syntax, t1 is the left table and


t2 is the right table.

• The LEFT JOIN clause selects data starting


from the left table (t1). It matches each row
from the left table (t1) with every row from
the right table(t2) based on the
join_condition.
• If the rows from both tables cause the join
condition evaluates to true, the LEFT JOIN
combine columns of rows from both tables
to a new row and include this new row in the
result rows.
• In case the row from the left table (t1) does
not match with any row from the right
table(t2), the LEFT JOIN still combines
columns of rows from both tables into a new
row and includes the new row in the result
rows. However, it uses the NULL values for
all the columns of the row from the right
table.
• In other words, LEFT JOIN returns all rows
from the left table regardless of whether a
row from the left table has a matching row
from the right table or not.
• If there is no match, the columns of the row
from the right table will contain NULL.

• The following Venn diagram helps you


visualize how the LEFT JOIN clause works:
MySQL LEFT JOIN clause
examples
• Using MySQL LEFT JOIN clause to join
two tables
• See the following tables customers and
orders in the sample database.
• This query uses the LEFT JOIN clause to
find all customers and their orders:
SELECT
customers.customerNumber,
customerName,
orderNumber,
status
FROM
customers
LEFT JOIN orders ON
orders.customerNumber =
customers.customerNumber;
• Alternatively, you can save some typing by
using table aliases:
• MySQL aliases allows you to assign
temporary names to columns or tables in a
query.
SELECT
c.customerNumber,customerName,
orderNumber, status
FROM customers c
LEFT JOIN orders o
ON
c.customerNumber = o.customerNumber;
In this example:
• The customers is the left table and orders is
the right table.
• The LEFT JOIN clause returns all
customers including the customers who
have no order. If a customer has no order,
the values in the column orderNumber and
status are NULL.;
• Because both table customers and orders
have the same column name
(customerNumber) in the join condition with
the equal operator, you can use the USING
syntax as follows:
SELECT
customerNumber,customerName,
orderNumber,status
FROM
customers
RIGHT JOIN orders USING
(customerNumber);

You might also like