INFOMAN Lesson 9
INFOMAN Lesson 9
▪ You can use comparison operators e.g., =, >, < to compare a single
value returned by the subquery with the expression in the WHERE
clause.
▪ Example query returns the customer who has the highest payment.
Subquery
▪ Example query returns the customer who has the highest payment.
Subquery
▪ First, get the average payment
by using a subquery.
▪ Then, select the payments that
are greater than the average
payment returned by the
subquery in the outer query.
Subquery
▪ MySQL subquery with IN and NOT IN
operators
▪ If a subquery returns more than one
value, you can use other operators such
as IN or NOT IN operator in the WHERE
clause.
Joins
▪ MySQL supports the following JOIN types:
INNER JOIN, OUTER JOIN (further divided into
LEFT JOIN and RIGHT JOIN), SELF JOIN, and
CROSS JOIN. The type of the JOIN defines how
tables are related in a query.
Joins
Joins
Joins
Joins
Joins
Joins
Setup the Table for Joins
Setup the Table for Joins
Setup the Table for Joins
MySQL INNER JOIN clause
▪ The following shows the basic syntax of the
inner join clause that joins two tables table_1
and table_2:
MySQL INNER JOIN clause
▪ The inner join clause joins two tables based on a
condition which is known as a join predicate.
▪ The inner join clause compares each row from the first
table with every row from the second table.
▪ If values from both rows satisfy the join condition, the
inner join clause creates a new row whose column
contains all columns of the two rows from both tables
and includes this new row in the result set. In other
words, the inner join clause includes only matching rows
from both tables.
MySQL INNER JOIN clause
Venn Diagram: Inner Join
Result:
MySQL INNER JOIN clause
▪ Venn Diagram: Inner Join
• If the join condition uses the equality operator (=) and the column
names in both tables used for matching are the same.
MySQL INNER JOIN clause
▪ Venn Diagram: Inner Join
• If the join condition used USING and the column names in both
tables used for matching are the same.
MySQL LEFT JOIN clause
▪ Similar to an inner join, a left join also requires a join
predicate.
▪ The left join selects data starting from the left table. For
each row in the left table, the left join compares with
every row in the right table.
▪ If the values in the two rows satisfy the join condition,
the left join clause creates a new row whose columns
contain all columns of the rows in both tables and
includes this row in the result set.
MySQL LEFT JOIN clause
▪ If the values in the two rows are not matched, the left join
clause still creates a new row whose columns contain
columns of the row in the left table and NULL for columns
of the row in the right table.
▪ In other words, the left join selects all data from the left
table whether there are matching rows exist in the right
table or not.
▪ In case there are no matching rows from the right table
found, the left join uses NULLs for columns of the row
from the right table in the result set.
MySQL LEFT JOIN clause
▪ Syntax:
MySQL LEFT JOIN clause
▪ Venn Diagram: Left Join
MySQL LEFT JOIN clause
▪ Venn Diagram: Left Join
MySQL RIGHT JOIN clause
▪ The right join clause is similar to the left join clause except
that the treatment of left and right tables is reversed. The
right join starts selecting data from the right table instead
of the left table.
▪ The right join clause selects all rows from the right table
and matches rows in the left table. If a row from the right
table does not have matching rows from the left table, the
column of the left table will have NULL in the final result
set.
MySQL RIGHT JOIN clause
▪ Syntax:
MySQL RIGHT JOIN clause
▪ Venn Diagram: Right Join
MySQL RIGHT JOIN clause
▪ Venn Diagram: Right Join
MySQL CROSS JOIN clause
▪ Unlike the inner join, left join, and right join, the cross join clause
does not have a join condition.
▪ The CROSS JOIN is used to generate a paired combination of each
row of the first table with each row of the second table. This join
type is also known as cartesian join.
▪ The cross join makes a Cartesian product of rows from the joined
tables. The cross join combines each row from the first table with
every row from the right table to make the result set.
▪ Suppose the first table has n rows and the second table has m
rows. The cross-join that joins the tables will return nxm rows.
MySQL CROSS JOIN clause
▪ Syntax:
MySQL CROSS JOIN clause
▪ Venn Diagram: Cross Join
members committees
Rename the column name as mem_name.
MySQL CROSS JOIN clause Because an error will occur when you use
the same column name.
members committees