Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
25Activity
0 of .
Results for:
No results containing your search query
P. 1
Joins

Joins

Ratings: (0)|Views: 172|Likes:
Published by lavkush2oct
DBMS concepts on joins
DBMS concepts on joins

More info:

Published by: lavkush2oct on Jan 27, 2010
Copyright:Attribution Non-commercial

Availability:

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

11/19/2011

pdf

text

original

 
Join (SQL)
AnSQL 
JOIN
clause combines records from two or more tablesin a database.
It creates a setthat can be saved as a table or used as is. A
JOIN
is a means for combining fields from two tables by using values common to each. ANSI standard SQL specifies four types of 
JOIN
s:
INNER
,
OUTER
,
LEFT
, and
RIGHT
. In special cases, a table (base table, view, or joined table) can
JOIN
toitself in a
 self-join
.A programmer writes a
JOIN
  predicateto identify the records for joining. If the evaluated predicate is true the combined record is then produced in the expected format, for example arecord set or a temporary table. Novices should beware that the default
JOIN
in many systems is the
inner join
(this can beconfusing because
 join ... on
can seem to imply an ordering, but it is actually the
 join
that appliesto the table names, and the
on
that applies to the criteria;
 join
is itself ambiguous between order-independence and order-dependence).
Sample tables
All subsequent explanations on join types in this article make use of the following two tables.The rows in these tables serve to illustrate the effect of different types of joins and join- predicates. In the following tables,
Department.DepartmentID
is the  primary key, while
Employee.DepartmentID
Employee Table
LastNameDepartmentID
Rafferty31 Jones33Steinberg33Robinson34Smith34 John
 
NULL
Department Table
DepartmentIDDepartmentName
31Sales
 
33Engineering34Clerical35Marketing
 Note: The "Marketing" Department currently has no listed employees. Also, employee "John"has not been assigned to any Department yet.
[edit] Inner join
An
inner join
is the most common join operation used in applications, and represents the default join-type. Inner join creates a new result table by combining column values of two tables (A andB) based upon the join-predicate. The query compares each row of A with each row of B to findall pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, columnvalues for each matched pair of rows of A and B are combined into a result row. The result of the join can be defined as the outcome of first taking the Cartesian product(or cross-join) of all records in the tables (combining every record in table A with every record in table B) - thenreturn all records which satisfy the join predicate. Actual SQL implementations normally useother approaches where possible, since computing the Cartesian product is very inefficient.SQL specifies two different syntactical ways to express joins: "explicit join notation" and"implicit join notation".The "explicit join notation" uses the
JOIN
keyword to specify the table to join, and the
ON
keyword to specify the predicates for the join, as in the following example:
SELECT *FROM employeeINNER JOIN departmentON employee.DepartmentID = department.DepartmentID
The "implicit join notation" simply lists the tables for joining (in the
FROM
clause of the
SELECT
statement), using commas to separate them. Thus, it specifies a cross-join, and the
WHERE
clausemay apply additional filter-predicates (which function comparably to the join-predicates in theexplicit notation).The following example shows a query which is equivalent to the one from the previous example, but this time written using the implicit join notation:
SELECT *FROM employee, departmentWHERE employee.DepartmentID = department.DepartmentID
The queries given in the examples above will join the Employee and Department tables using theDepartmentID column of both tables. Where the DepartmentID of these tables match (i.e. the join-predicate is satisfied), the query will combine the
 LastName
,
 DepartmentID
and
 DepartmentName
columns from the two tables into a result row. Where the DepartmentID doesnot match, no result row is generated.Thus the result of the execution of any of the two queries above will be:
 
Employee.LastNameEmployee.DepartmentIDDepartment.DepartmentNameDepartment.DepartmentID
Robinson34Clerical34 Jones33Engineering33Smith34Clerical34Steinberg33Engineering33Rafferty31Sales31
Note:
Programmers should take special care when joining tables on columns that can contain NULLvalues, since NULL will never match any other value (or even NULL itself), unless the join condition explicitly uses the
IS NULL
or 
IS NOT NULL
predicates. Notice that the employee "John" and the department "Marketing" do not appear in the queryexecution results. Neither of these has any matching records in the respective other table: "John"has no associated department, and no employee has the department ID 35. Thus, no informationon John or on Marketing appears in the joined table. Depending on the desired results, this behavior may be a subtle bug.Outer joinsmay be used to avoid it.One can further classify inner joins as equi-joins, as natural joins, or as cross-joins (see below).
 [ edit 
 
 ] Equi-join
An
equi-join
, also known as an
equijoin
, is a specific type of comparator-based join, or 
theta join
, that uses onlyequality comparisons in the join-predicate. Using other comparison operators (such as
<
) disqualifies a join as an equi-join. The query shown above has already provided anexample of an equi-join:
SELECT *FROM employeeINNER JOIN departmentON employee.DepartmentID = department.DepartmentID
SQL provides an optional shorthand notation for expressing equi-joins, by way of the
USING
construct (Feature ID F402):
SELECT *FROM employeeINNER JOIN departmentUSING (DepartmentID)
The
USING
construct is more than meresyntactic sugar , however, since the result set differs fromthe result set of the version with the explicit predicate. Specifically, any columns mentioned inthe
USING
list will appear only once, with an unqualified name, rather than once for each table inthe join. In the above case, there will be a single
DepartmentID
column and no
employee.DepartmentID
or 
department.DepartmentID
.The
USING
clause is supported by MySQL, Oracle, PostgreSQL, SQLite, DB2/400 and Firebirdin version 2.1 or higher.

Activity (25)

You've already reviewed this. Edit your review.
1 hundred reads
ALi liked this
ALi liked this
gsubbu143 liked this
Rajveer Sa liked this
sunny_dba liked this
sunny_dba liked this
Himanshu liked this
vaishuprasad liked this

You're Reading a Free Preview

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