SQL JOIN
Definition:
The JOIN clause is used to combine records from two or more tables in a database. This is done by combining fields from the joined tables by using values common to each table. In other words, JOINS are used to find data from two or more tables based on the relationship between certain columns in these tables.
Standard ANSII SQL specifies four types of JOINs:
- INNER
- OUTER
- LEFT
- RIGHT
In certain cases, a TABLE or VIEW can JOIN to itself in what is called a "SELF JOIN".
The join predicate in the SQL statement is what contains the rules or conditions that identify what records are to be joined. When the SQL is evaluated, the predicate conditions are tested. If the predicate conditions are true then the combined record is produced.
An INNER JOIN is the most common join operation and is generally the default join type. An inner join creates a result by combining column values of two tables (A and B) based upon the join predicate. The join query compares each row of table A with each row of table B to find all pairs of rows which satisfy the join predicate.
The INNER JOIN example below will only return a result when there is at least one valid match in both tables. If there are rows in "Users" that do not have matches in "Sales", those rows will not be in the result set (not returned).
Inner Join Example:
The join predicate in the SQL statement is what contains the rules or conditions that identify what records are to be joined. When the SQL is evaluated, the predicate conditions are tested. If the predicate conditions are true then the combined record is produced.
An INNER JOIN is the most common join operation and is generally the default join type. An inner join creates a result by combining column values of two tables (A and B) based upon the join predicate. The join query compares each row of table A with each row of table B to find all pairs of rows which satisfy the join predicate.
The INNER JOIN example below will only return a result when there is at least one valid match in both tables. If there are rows in "Users" that do not have matches in "Sales", those rows will not be in the result set (not returned).
Inner Join Example:
SELECT Users.Last_Name, Users.First_Name, Sales.Order_Number
INNER JOIN
Sales
ORDER_BY Users.Last_Name
An OUTER JOIN does not require each record in the two joined tables to have
a matching record. The joined table keeps each record even if no other matching record exists. Outer joins can be classified further into "left outer
joins", "right outer joins", and "full outer joins",
depending on which tables the rows are retained from (left, right, or both).
The OUTER JOIN clause is a variation of the SQL JOIN clause which enables a SELECT statement to process more than one table. Use the OUTER JOIN to return results from multiple tables when there aren't any matches between the JOINed tables. The example OUTER JOIN below will select from the Users and Orders tables all of the distinct users (first and last names) and the total amount they've spent:
The OUTER JOIN clause is a variation of the SQL JOIN clause which enables a SELECT statement to process more than one table. Use the OUTER JOIN to return results from multiple tables when there aren't any matches between the JOINed tables. The example OUTER JOIN below will select from the Users and Orders tables all of the distinct users (first and last names) and the total amount they've spent:
GROUP_BY Users.FirstName,
Users.LastName;
NOTE: A "cartesian product" will result if there are no matching join columns specified. This means that a row will be returned for EVERY combination of the two tables. If one table has 1,000 rows and the second table has 3,000 rows the result set will contain 3,000,000 (three million!) rows. This is probably NOT what you want.
A SELF JOIN is a query in which a table is joined (compared) to itself. Self-joins are useful to compare values in a specific column with other values in the same column in the same table (related data that is stored in a single table). The self join can be done by using table aliases to treat one table as though it were a different table and then joining them together. Another common use for self-joins is obtaining running counts and totals in a query.
The general rule for writing self-joins is:
- Write
the query doing the selects from the same table listed twice with
different aliases
- Set
up the comparison pedicates or tests
- Remove
any cases where a given value would be equal
to itself.
This example self join selects users who have the
same ZipCode (the column names in this example are 'UserName', 'Address', 'City',
and 'ZipCode'):
SELECT DISTINCT c1.UserName, c1.Address,
c1.City, c1.ZipCode
FROM Users AS c1, Users AS c2
WHERE c1.ZipCode = c2.ZipCode
AND c1.UserName <> c2.UserName
ORDER_BY c1.ZipCode, c1.UserName;
FROM Users AS c1, Users AS c2
WHERE c1.ZipCode = c2.ZipCode
AND c1.UserName <> c2.UserName
ORDER_BY c1.ZipCode, c1.UserName;
Traditional Joins
|
|
Two Table Inner Join |
|
Three Table Inner Join |
|
Left Outer Join |
|
Right Outer Join |
|
Self Join |
|
ANSI Joins
|
|
Inner Join |
|
Left Outer Join |
|
Right Outer Join |
|
Full Outer Join |
|
Natural Join |
|
Self Join |
|
Alternative Syntax Joining on commonly named column in both tables |
|
-- does
not work
SELECT s.srvr_id, s.status, i.location_code FROM servers s INNER JOIN serv_inst i USING (s.srvr_id) WHERE rownum < 11; -- does not work either SELECT s.srvr_id, s.status, i.location_code FROM servers s INNER JOIN serv_inst i USING (srvr_id) WHERE rownum < 11; -- works SELECT srvr_id, s.status, i.location_code FROM servers s INNER JOIN serv_inst i USING (srvr_id) WHERE rownum < 11; |
|
Cartesian Join
|
|
Table And Data For
Cartesian
Product (Cross-Join) Demo |
|
Load Demo Tables |
|
Inner Join |
|
Not Inner Join |
|
Cartesian (Cross-Join) Product |
|
Intentional Cartesian (Cross-Join) Product |
|
|
Comments
Post a Comment