Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
28Activity
0 of .
Results for:
No results containing your search query
P. 1
Joins

Joins

Ratings: (0)|Views: 405 |Likes:
Published by api-3818400

More info:

Published by: api-3818400 on Oct 17, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

SQL: Joins
Ajoin is used to combine rows from multiple tables. A join is performed whenever two or more tables is
listed in the FROM clause of an SQL statement.
There are different kinds of joins. Let's take a look at a few examples.
Inner Join (simple join)
Chances are, you've already written an SQL statement that uses an inner join. It is is the most common type
of join. Inner joins return all rows from multiple tables where the join condition is met.
For example,

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;

This SQL statement would return all rows from the suppliers and orders tables where there is a matching

supplier_id value in both the suppliers and orders tables.
Let's look at some data to explain how inner joins work:
We have a table calledsuppliers with two fields (supplier_id and supplier_ name).

It contains the following data:
supplier_id
supplier_name
10000
IBM
10001
Hewlett Packard
10002
Microsoft
10003
Nvidia
We have another table calledorders with three fields (order_id, supplier_id, and order_date).
It contains the following data:
order_id
supplier_id
order_date
500125
10000
2003/05/12
500126
10001
2003/05/13
If we ran the SQL statement below:

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;

Our result set would look like this:
supplier_id
name
order_date
10000
IBM
2003/05/12
10001
Hewlett Packard
2003/05/13
The rows forMicrosoft andNvidia from the supplier table would be omitted, since the
supplier_id's 10002 and 10003 do not exist in both tables.
Mahendra Sajjala
Page 1 of 12
Joins
Outer Join
Another type of join is called an outer join. This type of join returns all rows from one table andonl y those
rows from a secondary table where the joined fields are equal (join condition is met).
For example,

select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where suppliers.supplier_id = orders.supplier_id(+);

This SQL statement would return all rows from the suppliers table and only those rows from the orders table
where the joined fields are equal.
The (+) after the orders.supplier_id field indicates that, if a supplier_id value in the suppliers table does not
exist in the orders table, all fields in the orders table will display as <null> in the result set.
The above SQL statement could also be written as follows:

select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where orders.supplier_id(+) = suppliers.supplier_id

Let's look at some data to explain how outer joins work:
We have a table calledsuppliers with two fields (supplier_id and name).
It contains the following data:
supplier_id
supplier_name
10000
IBM
10001
Hewlett Packard
10002
Microsoft
10003
Nvidia
We have a second table calledorders with three fields (order_id, supplier_id, and order_date).
It contains the following data:
order_id
supplier_id
order_date
500125
10000
2003/05/12
500126
10001
2003/05/13
If we ran the SQL statement below:

select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where suppliers.supplier_id = orders.supplier_id(+);

Our result set would look like this:
supplier_id
supplier_name
order_date
10000
IBM
2003/05/12
10001
Hewlett Packard
2003/05/13
10002
Microsoft
<null>
10003
Nvidia
<null>
Mahendra Sajjala
Page 2 of 12
Joins

The rows forMicrosoft andNvidia would be included because an outer join was used. However, you will notice that the order_date field for those records contains a <null> value.

What's the difference between an inner join and an outer join?
When you wish to get data from two related tables, you can use an
inner join or an outer join to define how the data is related.

For my examples, let's assume where is a table called
"student" to hold student information, a table called
"attendance" to hold student's attendance, and a table
called StudentSchedule that holds a student's current schedule:

Student has the following fields:
ID
FirstName
LastName
BirthDate

Attendance has the following fields:
ID
AttendanceDate
AttendanceCode
MinutesAbsent

StudentSchedule as the following fields:
ID
Course

An inner join gets data from both tables where the specified data
exists in both tables. For example, if you wanted a list of
students in yourdatabase that were absent on December 4, 2003, you
would use an inner join between the two examples tables "Student"
and "Attendance":

SELECT Student.ID, Student.FirstName, Student.LastName,
Attendance.AttendanceCode,
Attendance.MinutesAbsent FROM Student INNER JOIN Attendance
ON Student.ID=Attendance.ID
WHERE Attendance.AttendanceDate='12/4/2003'

The above statement will only return students with attendance information on the specified date. Students who do not have attendance would not display.

Like so:
ID
FirstName
LastName
AttendanceCode MinutesAbsent
10
Steve
Bartman
Tardy
22
32
Dale
Thropmorton
ExcAbsent
200

(maybe there are 200 kids in the database, but only Steve and Dale where absent on 12/4/2003. They are the only students to display)

An outer join gets data from the source table at all times, and
returns data from the outer joined table ONLY if it matches the
criteria. You would use this type of join using my examples
tables if you wanted a list of all students in a specified
course, and you wanted attendance information if it existed. You
would use an inner join between Student and StudentSchedule to

Mahendra Sajjala
Page 3 of 12
Joins

Activity (28)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
iris1567 liked this
Pragya Sharma liked this
faridsk liked this
coolamita1795 liked this
urvipatil liked this
pvn_99 liked this
Soumini Pal liked this
ANIL KS liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->