Académique Documents
Professionnel Documents
Culture Documents
A join 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.
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,
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.
We have a table called suppliers 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 called orders with three fields (order_id, supplier_id, and order_date).
It contains the following data:
The rows for Microsoft and Nvidia from the supplier table would be omitted, since the
supplier_id's 10002 and 10003 do not exist in both tables.
Another type of join is called an outer join. This type of join returns all rows from one table and only those
rows from a secondary table where the joined fields are equal (join condition is met).
For example,
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.
We have a table called suppliers 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 called orders with three fields (order_id, supplier_id, and order_date).
It contains the following data:
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 your database that were absent on December 4, 2003, you
would use an inner join between the two examples tables "Student"
and "Attendance":
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
only get the students in the speicified course (for example
'ENGLISH 9'), but you would use an outer join against Attendance
because you want ALL students in the course, not just students
with attendance information on 12/4/2003.
When using outer joins, fields will be set to NULL if data does
not exist in the outer-joined table.
LEFT OUTER Same as LEFT JOIN. Symbolic representation: "=*" (in a WHERE
JOIN clause).
The LEFT OUTER JOIN and the RIGHT OUTER JOIN are in most respects functionally
identical (with reversed syntax), and thus are frequently referred to collectively as
one-way outer joins.
The INNER JOIN, LEFT OUTER JOIN, and RIGHT OUTER JOIN can take one (and only
one) of the following qualifiers: a NATURAL keyword prefix, an ON clause, or a
USING clause. Thus a NATURAL JOIN is any of these three types of join prefixed with
the NATURAL keyword.
For further details, see OUTER JOIN Limitations in Using Caché SQL.
For a NATURAL join, or a join with a USING clause, only simple base table references
are supported for either operand.
One-Way Outer Joins
Caché supports one-way outer joins: LEFT OUTER JOIN and RIGHT OUTER JOIN.
Caché does not support two-way outer joins.
With standard "inner" joins, when rows of one table are linked with rows of a second
table, a row in the first table that finds no corresponding row in the second table is
excluded from the output table.
1. The ANSI standard syntax: LEFT OUTER JOIN and RIGHT OUTER JOIN. SQL
Standard syntax puts the outer join in the FROM clause of the SELECT
statement, rather than the WHERE clause, as shown in the following example:
2. The ODBC Specification outer join extension syntax, using the escape-syntax
{oj ... }
3. Symbolic outer join extension syntax, using a condition such as A=*B in the
WHERE clause. A left outer join is specified using the symbol =* in place of =
in the WHERE clause. (Note that this is the reverse of the syntax used by
Microsoft SQL Server.) A right outer join is specified using the symbol *= in
place of = in the WHERE clause.
These three formats are interchangeable and can also be mixed. However, we
recommend the use of ANSI standard syntax, as it is the only one compatible with
ODBC (and portable to the latest Microsoft products). Additionally, it can specify
many operations not specifiable in principle with symbolic syntax.
Examples of Outer Joins
As an illustration of the difference between an inner join and a one-way outer join,
consider the query:
SELECT Patient.PName,Doctor.DName
FROM Patient,Doctor
WHERE Patient.Doctor = Doctor.Doctor
The FROM clause specifies a cross join, and the WHERE clause restricts the results to
an inner join. Thus, this query returns one row for each row of the "Patient" table
that has a non-null "Doctor" field. By contrast, examine the following left outer join
query:
SELECT Patient.PName,Doctor.DName
FROM Patient,Doctor
WHERE Patient.Doctor =* Doctor.Doctor
Though it differs only in the =* operator, this query returns all the rows of the
previous query plus the rows of the "Patient" table that have null value for the
Basically Inner Joins combines all the records in the first table with all the
records in the second table and then selects those rows depending on the
criteria that is present after the the ON keyword in the query.
The most important thing to remember in Inner Joins is that only those records
from both the tables are combined where there is a corresponding value in both
the tables. You will understand this point clearly when you read about Outer
Joins.
There are 2 types of Outer Joins - Left Join and Right Join. Consider an
example of Left Join
This query would now (considering the new authors table) return 5 rows of data.
The last row would correspond to the record of the author named Kiran Pai but
there would be NULL values for the fields such as title,pages,book_id... since
there are no books written by Kiran Pai in our books table.
This is exactly what Left Joins do - they combine all the records in the first table
with those in the second table irrespective of whether there are corresponding
values in 2 tables. By corresponding I mean like in our case since there were no
books by Kiran Pai in our books table there are no corresponding values for Kiran
Pai in our books table. But inspite of there being no corresponding values all the
values were joined and thus you got the NULL values in your final query result.
This point is very important to understand Joins.. so please do read it again in
case you have not understood it clearly.
Could you explain the concept of Inner Joins once again with respect to
this Left Joins?
Yeah.. now you will understand better. As I mentioned in Left Joins all the
records in the first table are combined with data from the second table. And if
there is no corresponding data in the second table then a NULL value is inserted
into the results. Whereas in Inner Joins, records from the 2 tables are combined
only if there are corresponding values in both the tables. For example consider
Mahendra Sajjala Page 9 of 12 Joins
the following Inner Join on the books table and our new authors table -
This query would return only 4 rows of data and not 5 inspite of the new Kiran
Pai entry being present. This is because it could find no corresponding entry in
the books table for the author named Kiran Pai (there were no books written by
Kiran Pai)
This time you would NOT GET 5 rows of data, instead you would get only 4 rows
of data. Why so??
Since while joining the data from the books table with the authors table, the
books table data is considered first.. since it is a Right Join. Thus for every row
in the books table a check for corresponding value in the authoirs table would be
made. Thus while adding all the rows of the books table to the joined table,
since there were no books written by Kiran Pai, so that particular record from the
authors table was not added at all to the joined table. Remember that the joined
table is the kind of temporary table that is created while using Joins. That table
is refined depending on the criteria present in the query.
Inner Joins and Outer Joins aren't really clear in my mind right now.
Why don't you explain it once again in short ?
Actually, all that is required is a re-reading of the previous 4-5 questions. In case
you still haven't got it, there is only way out. Create actual tables using some
software and then run some sample queries on those tables. Remember not to
put corresponding entries for all your data in both of your tables.
By that I mean in case you are making 2 tables - one for artists and one for
songs. See to it that in case you have 10 artists names in the artists table, you
do not have songs sung by all of those 10 artists in your songs table. Add songs
by only 6-7 artists. When you have such a kind of database where there isn't
corresponding information for all the records in one table in the other table,
Inner Joins and Outer Joins show their real working. In case you have songs
sung by all 10 artists in your Songs table, most of your query results would be
the same irrespective of what kind of Join you use. Only the order of the rows in
the resultset might be altered. So you would be confused..you wouldn't
understand the difference between the working of different Joins.
SELECT ... , ... FROM ( .... INNER JOIN .... ON .... = .... ) INNER JOIN .... ON ....
= .... WHERE .... = .....
This is not really for beginners, but if you can get it.. you are not a beginner..
you are already on your way to be an expert.
So basically there are 3 Joins right?? Cross Join, Inner Join and Outer
Join?
There is also a Self Join, which I shall not be explaining in this article. Just
remember that it exists. It is a fairly complex topic which you may not need right
away.
Yes I can do it.. I shall first make a query and find his author_id from
the authors table and then I shall make another query and get a list of
books having that id associated with it from the books table.
Ok.. good. but aren't you using 2 queries? Can you get it in one query?
That where Sub Queries come in use. You could find the author_id for that
particular author and then also find the books written by him in the same query.
Seems complex.. but its actually simple.. (This is after all SQL In Simple English :)
The first part is simple.. you select the title of the books whenever the criteria is
met. Lets analyze the criteria..
The inner most part SELECT author_id from authors where firstname = 'Jason'
evaluates to the 1 (its just a simple query which you learnt right at the beginning).
Thus the entire query now becomes
Here we are using the 2 original tables the authors table with 2 rows and the books
table with 4 rows. Now when the joined table is created it would be consisting of 8
rows (2x4 as explained earlier). From these 8 rows only 4 would be containing
correct information and the rest of them would be just present as a result of the
various combinations that were possible. Now the books.author_id =
authors.author_id part of the WHERE clause separates these 4 correct rows and
discards the remaining rows. And finally the other part of the WHERE clause
books.author_id = 1 would selects the final single row from the 4 correct rows.
Thus you would get the names of the books written by the author named Jason
Hunter.