Anda di halaman 1dari 34

JOIN AND MULTIPLE

TABLE OPERATION
SQL Aliases

• SQL aliases are used to give a table, or a column in a table,


a temporary name.
• Aliases are often used to make column names more
readable.
• An alias only exists for the duration of the query.
Review :Table and Column Aliases (Cont.)

▰ TABLE ALIASES
▻ We are about to select data from more than one table, table aliases willbe
helpful.
▻ Table aliases created in the same way ascolumn aliases.
▰ Example OR
SELECT nim AS nomor_induk SELECT nimnomor_induk

FROM angkatan_2014 AS a FROM angkatan_2014 a

GROUP BY nomor_induk GROUP BY nomor_induk

ORDER BY nomor_induk; ORDER BY nomor_induk; 4


1
INNER JOIN

5
Outline

▰ Inner Join
▻ Cartesian Join/Product
▻ Equijoin
▻ Non-equijoin
▻ Natural Join

6
CARTESIAN JOIN

▰ Also known as cartesianproduct, cross


product, cross join, or no join
▰ Suppose we have toys and boystables
▰ QUERY:
SELECT t.toy,b.boy
FROM toys t
CROSS JOIN
Boys b 7
CARTESIAN JOIN

▰ QUERY:
SELECT t.toy, b.boy
FROM toys t
CROSS JOIN
Boys b
 The CROSS JOIN returns every row fromone
table crossed with every row from the
second
 The join above will return 20 results to
account for every possible combination 8
Try this your self

▰ Find out what happen if you run this query

SELECT b1.boy, b2.boy


FROM boys AS b1 CROSS JOIN boys as b2;

9
When to use it?

▰ You might accidentally get cartesian result


▰ Cross join are used to test the speedof your RDBMS and
its configuration
▰ What happens if I use SELECT *,what will be the result?
Try it yourself.
▰ Another way to use CROSS JOIN? Use comma instead of
CROSS JOIN
SELECT t.toys, b.boy
FROM toys t, boys b 10
Inner Join vs Cartesian Join?

▰ A cartesian join isa type of inner join.


▰ Inner join is basically just a cartesian join where
some results rows are removed by a condition in
the query.

11
Inner Join: Equijoin

▰ An inner join combines the record from two


tables using comparison operators in a
condition
▰ EQUIJOIN, inner joins test for equality
▰ For example we have (one to one) toys
and boys tables
▰Query :find out what toy each boy has
SELECT boys.boy, toys.toy
FROM boys
INNER JOIN toys
ON boys.toy_id = toys.toy_id
▰ RESULT ?? 12
Inner Join: Non Equijoin

▰ NON-EQUIJOIN, inner joins test for inequality


▰ Returns any rows that are not equal
▰ QUERY :which toy each boy doesn’t
have
SELECT boys.boy, toys.toy
FROM boys
INNER JOINTOYS
ON boys.toy_id > toys.toy_id
ORDER BY boys.boy;
 RESULTS ??
13
Natural Join

▰ Natural join only work if the column you’re joining by


has the same name in bothtables
▰ QUERY:
SELECT boys. boy,toys. toy
FROM boys
NATURAL JOIN
toys;

14
JOIN- Summary??

15
Costumer

order
2
Outer JOIN

17
Outline

▰ Outer joins
▻ Left outer joins
▻ Right outer joins

18
Outer Join

▰ An outer joins
returns all rows
from one of the
tables, along
with matching
information
from another
table.
▰ Review of
Equijoin

19
Outer Join (Cont.)

▰ Left outer joins : matches EVERY ROW in theLEFT


table with a row from the right table
▰ Key :which one is left and which one is right
table?
▻ LEFT table :the table that comes beforejoin
▻ RIGHT table :the table that comes after the
join

20
Left Outer Join

▰ Example:
SELECT g.girl,t.toy
FROM girls g
LEFT OUTER JOIN toyst
On g.toy_id = t.toy_id

 Result
21
Seems like INNER JOIN?

▰ The difference is that an outer join gives you a rowwhether


there’s a match with the other table or not.
▰ A NULL value in the results of a left outer join meansthat the
right table has no values that correspond to the left table.
▰ Example:

22
Seems like INNER JOIN? (Cont.)

23
Right Outer Join

▰ The right outer join evaluates the right table against the left table

▰ Result:

24
A join that returns both the left and right results?

▰ FULL OUTER JOIN, but it does not workwith


MYSQL, SQL Server, orACCESS.

25
Costumer

order
3
Self join

27
Self join

▰ A self join is a join in which a table is joined with itself (which is


also called Unary relationships)
▰ The syntax of the command for joining a table to itself is almost
same as that for joining two different tables

28
Self join(Cont.)

▰ The self-join allows you to query a single


table as though there were two tablesexactly
the same information in them.

30
4
UNION

31
Union

The UNION operator is used to combine the


result-set of two or more SELECT statements.

• Each SELECT statement within UNION must


have the same number of columns
• The columns must also have similar data
types
• The columns in each SELECT statement must
also be in the same order

32
Costumers

Suppliers
References
https://www.w3schools.com/

Anda mungkin juga menyukai