You are on page 1of 15

JOINS

JOINS

• The most used operator in the relational algebra.


• Allows us to establish connections among data in
different relations, taking advantage of the “valuebased”
nature of the relational model.
Two main versions of the join:
• “natural join” : takes attribute names into account;
• “theta join”
Both join operations are denoted by the symbol ⋈.
• Join is a combination of a Cartesian product followed by a
selection process. A Join operation pairs two tuples from
different relations, if and only if a given join condition is
satisfied.
• When we select the data from multiple tables having
relationship then we use the concept of joins.
• 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.
FULL OUTER JOIN
LEFT OUTER JOIN
CARTESIAN PRODUCT JOIN
CUSTOMER TABLE

customer_id first_name last_name email address city state zipcode

1 George Washington gwashington 3200 Mt Mount VA 22121


@usa.gov Vernon Vernon
Hwy
2 John Adams jadams@usa. 1250 Quincy MA 02169
gov Hancock St

3 Thomas Jefferson tjefferson@u 931 Thomas Charlottes VA 22902


sa.gov Jefferson ville
Pkwy

4 James Madison jmadison@u 11350 Orange VA 22960


sa.gov Constitution
Hwy

5 James Monroe jmonroe@us 2050 James Charlottes VA 22902


a.gov Monroe ville
Parkway
ORDER TABLE

order_id order_date amount customer_id

1 07/04/1776 $234.56 1

2 03/14/1760 $78.50 3

3 05/23/1784 $124.00 2

4 09/03/1790 $65.50 3

5 07/21/1795 $25.50 10

6 11/27/1787 $14.40 9
INNER JOIN

select first_name, last_name, order_date, order_amount from


customers c inner join orders o on c.customer_id =
o.customer_id;

first_name last_name order_date order_amount


George Washington 07/4/1776 $234.56

John Adams 05/23/1784 $124.00

Thomas Jefferson 03/14/1760 $78.50

Thomas Jefferson 09/03/1790 $65.50


LEFT JOIN

select first_name, last_name, order_date, order_amount


from customers c left join orders o on c.customer_id =
o.customer_id;

first_name last_name order_date order_amount


George Washington 07/04/1776 $234.56
John Adams 05/23/1784 $124.00
Thomas Jefferson 03/14/1760 $78.50
Thomas Jefferson 09/03/1790 $65.50
James Madison NULL NULL
James Monroe NULL NULL
RIGHT JOIN

select first_name, last_name, order_date, order_amount


from customers c right join orders o on
c.customer_id = o.customer_id

first_name last_name order_date order_amount


George Washington 07/04/1776 $234.56
Thomas Jefferson 03/14/1760 $78.50
John Adams 05/23/1784 $124.00
Thomas Jefferson 09/03/1790 $65.50
NULL NULL 07/21/1795 $25.50
NULL NULL 11/27/1787 $14.40
FULL JOIN

select first_name, last_name, order_date, order_amount


from customers c full join orders o on c.customer_id =
o.customer_id
first_name last_name order_date order_amount

George Washington 07/04/1776 $234.56


Thomas Jefferson 03/14/1760 $78.50
John Adams 05/23/1784 $124.00
Thomas Jefferson 09/03/1790 $65.50
NULL NULL 07/21/1795 $25.50
NULL NULL 11/27/1787 $14.40
James Madison NULL NULL
James Monroe NULL NULL

You might also like