Download - SQL Join Keyword

Transcript
Page 1: SQL Join Keyword

PSS4W Sparepart 2013

SQL INNER JOIN Keyword

The INNER JOIN keyword returns rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

SELECT column_name(s)

FROM table_name1

INNER JOIN table_name2

ON table_name1.column_name=table_name2.column_name

PS: INNER JOIN is the same as JOIN.

SQL INNER JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:

O_Id OrderNo P_Id

1 77895 3

Page 2: SQL Join Keyword

PSS4W Sparepart 2013

2 44678 3

3 22456 1

4 24562 1

5 34764 15

Now we want to list all the persons with any orders.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo

FROM Persons

INNER JOIN Orders

ON Persons.P_Id=Orders.P_Id

ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo

Hansen Ola 22456

Hansen Ola 24562

Pettersen Kari 77895

Pettersen Kari 44678

The INNER JOIN keyword returns rows when there is at least one match in both tables. If there are rows in "Persons" that do

not have matches in "Orders", those rows will NOT be listed.

Page 3: SQL Join Keyword

PSS4W Sparepart 2013

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table

(table_name2).

SQL LEFT JOIN Syntax

SELECT column_name(s)

FROM table_name1

LEFT JOIN table_name2

ON table_name1.column_name=table_name2.column_name

PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SQL LEFT JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

Page 4: SQL Join Keyword

PSS4W Sparepart 2013

The "Orders" table:

O_Id OrderNo P_Id

1 77895 3

2 44678 3

3 22456 1

4 24562 1

5 34764 15

Now we want to list all the persons and their orders - if any, from the tables above.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo

FROM Persons

LEFT JOIN Orders

ON Persons.P_Id=Orders.P_Id

ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo

Hansen Ola 22456

Hansen Ola 24562

Pettersen Kari 77895

Pettersen Kari 44678

Page 5: SQL Join Keyword

PSS4W Sparepart 2013

Svendson Tove

The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table

(Orders).

Page 6: SQL Join Keyword

PSS4W Sparepart 2013

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no matches in the left table

(table_name1).

SQL RIGHT JOIN Syntax

SELECT column_name(s)

FROM table_name1

RIGHT JOIN table_name2

ON table_name1.column_name=table_name2.column_name

PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

SQL RIGHT JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:

O_Id OrderNo P_Id

1 77895 3

Page 7: SQL Join Keyword

PSS4W Sparepart 2013

2 44678 3

3 22456 1

4 24562 1

5 34764 15

Now we want to list all the orders with containing persons - if any, from the tables above.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo

FROM Persons

RIGHT JOIN Orders

ON Persons.P_Id=Orders.P_Id

ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo

Hansen Ola 22456

Hansen Ola 24562

Pettersen Kari 77895

Pettersen Kari 44678

34764

The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table

(Persons).

Page 8: SQL Join Keyword

PSS4W Sparepart 2013

SQL FULL JOIN Keyword

The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax

SELECT column_name(s)

FROM table_name1

FULL JOIN table_name2

ON table_name1.column_name=table_name2.column_name

SQL FULL JOIN Example

The "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

The "Orders" table:

O_Id OrderNo P_Id

1 77895 3

2 44678 3

Page 9: SQL Join Keyword

PSS4W Sparepart 2013

3 22456 1

4 24562 1

5 34764 15

Now we want to list all the persons and their orders, and all the orders with their persons.

We use the following SELECT statement:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo

FROM Persons

FULL JOIN Orders

ON Persons.P_Id=Orders.P_Id

ORDER BY Persons.LastName

The result-set will look like this:

LastName FirstName OrderNo

Hansen Ola 22456

Hansen Ola 24562

Pettersen Kari 77895

Pettersen Kari 44678

Svendson Tove

34764

Page 10: SQL Join Keyword

PSS4W Sparepart 2013

The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from the right table (Orders). If

there are rows in "Persons" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in

"Persons", those rows will be listed as well.

Page 11: SQL Join Keyword

PSS4W Sparepart 2013

Difference between inner and outer join

Assuming you're joining on columns with no duplicates, which is by far the most common case:

An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection.

An outer join of A and B gives the results of A union B, i.e. the outer parts of a venn diagram union.

Examples

Suppose you have two Tables, with a single column each, and data as follows:

A B

- -

1 3

2 4

3 5

4 6

Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.

Inner join

An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in

common.

select * from a INNER JOIN b on a.a = b.b;

select a.*,b.* from a,b where a.a = b.b;

a | b

--+--

3 | 3

4 | 4

Left outer join

Page 12: SQL Join Keyword

PSS4W Sparepart 2013

A left outer join will give all rows in A, plus any common rows in B.

select * from a LEFT OUTER JOIN b on a.a = b.b;

select a.*,b.* from a,b where a.a = b.b(+);

a | b

--+-----

1 | null

2 | null

3 | 3

4 | 4

Full outer join

A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a

corresponding datum in B, then the B portion is null, and vice versa.

select * from a FULL OUTER JOIN b on a.a = b.b;

a | b

-----+-----

1 | null

2 | null

3 | 3

4 | 4

null | 6

null | 5

Page 13: SQL Join Keyword

PSS4W Sparepart 2013

Other Explanation :

Inner Join:

Full Outer Join:

Page 14: SQL Join Keyword

PSS4W Sparepart 2013

A Visual Explanation of SQL Joins

I thought Ligaya Turmelle's post on SQL joins was a great primer for novice developers. Since SQL joins appear to be set-based, the use of Venn diagrams to explain them seems, at first blush, to be a natural fit. However, like the commenters to her post, I found that the Venn diagrams didn't quite match the SQL join syntax reality in my testing.

I love the concept, though, so let's see if we can make it work. Assume we have the following two tables. Table A is on the left, andTable B is on the right. We'll populate them with four records each.

id name id name

-- ---- -- ----

1 Pirate 1 Rutabaga

2 Monkey 2 Pirate

3 Ninja 3 Darth Vader

4 Spaghetti 4 Ninja

Let's join these tables by the name field in a few different ways and see if we can get a conceptual match to those nifty Venn diagrams.

Page 15: SQL Join Keyword

PSS4W Sparepart 2013

SELECT * FROM TableA

INNER JOIN TableB

ON TableA.name = TableB.name

id name id name

-- ---- -- ----

1 Pirate 2 Pirate

3 Ninja 4 Ninja

Inner join produces only the set of records that match in both Table A and Table B.

SELECT * FROM TableA

FULL OUTER JOIN TableB

ON TableA.name = TableB.name

id name id name

-- ---- -- ----

1 Pirate 2 Pirate

2 Monkey null null

3 Ninja 4 Ninja

4 Spaghetti null null

null null 1 Rutabaga

null null 3 Darth Vader

Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where

Page 16: SQL Join Keyword

PSS4W Sparepart 2013

available. If there is no match, the missing side will contain null.

SELECT * FROM TableA

LEFT OUTER JOIN TableB

ON TableA.name = TableB.name

id name id name

-- ---- -- ----

1 Pirate 2 Pirate

2 Monkey null null

3 Ninja 4 Ninja

4 Spaghetti null null

Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.

Page 17: SQL Join Keyword

PSS4W Sparepart 2013

SELECT * FROM TableA

LEFT OUTER JOIN TableB

ON TableA.name = TableB.name

WHERE TableB.id IS null

id name id name

-- ---- -- ----

2 Monkey null null

4 Spaghetti null null

To produce the set of records only in Table A, but not in Table B, we perform the same left outer join, thenexclude the records we don't want from the right side via a where clause.

SELECT * FROM TableA

FULL OUTER JOIN TableB

ON TableA.name = TableB.name

WHERE TableA.id IS null

OR TableB.id IS null

id name id name

-- ---- -- ----

2 Monkey null null

4 Spaghetti null null

null null 1 Rutabaga

null null 3 Darth Vader

To produce the set of records unique to Table A and Table B, we perform the same full outer join, thenexclude the records we don't want from both sides via a where

Page 18: SQL Join Keyword

PSS4W Sparepart 2013

clause.

There's also a cartesian product or cross join, which as far as I can tell, can't be expressed as a Venn diagram:

SELECT * FROM TableA

CROSS JOIN TableB

This joins "everything to everything", resulting in 4 x 4 = 16 rows, far more than we had in the original sets. If you do the math, you can see why this is a very dangerous join to run against large tables.

Page 19: SQL Join Keyword

PSS4W Sparepart 2013

Page 20: SQL Join Keyword

PSS4W Sparepart 2013 Referensi :

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

http://www.w3schools.com/sql/sql_join.asp

http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-join