SQL database system
 
Manual page for join(TDH)

JOIN

The JOIN keyword, which is used as part of a SELECT command, allows a relational join to be performed on two or three tables. The JOIN .. ON syntax must be used; alternatives aren't supported. Usage:

SELECT ..
FROM
left_table [AS|= alias1] [jointype] JOIN right_table [AS|= alias2]
[ [jointype2] JOIN table3 [AS|= alias3]
ON binding
WHERE .. (etc)


Examples

SELECT c.name, t.description
FROM customers (AS c) JOIN transactions (AS t) 
ON c.cust_id = t.cust_id

select p.*, ps.currentstatus
from patients = p left join patientstatus = ps
on p.patientid = ps.patientid
where ps.class = "B"
order by p.patientid

SELECT * FROM people JOIN pi_ref JOIN inst (as i)
ON people.id = pi_ref.people_id AND pi_ref.inst_id = i.id
ORDER BY people.lastname

Usage notes

When using JOIN, fieldnames must be specified using table.fieldname notation everywhere in the SQL command. Table name aliases may be defined using AS or = (as in the above examples); this helps to maintain brevity.

ON binding specifies the key field(s) that will be matched up when performing the join. The only supported comparison operation is equality (=), and matching is case-insensitive. Multiple bindings may be connected using AND.

Join type Method Multiple rows having same key*
INNER This is the default. Content must be present on both sides to produce an output record. Right side only
LEFT All left side rows kept; right side rows joined when available, filled in with NULLs otherwise. Right side only
RIGHT All right side rows kept; left side rows joined when available, filled in with NULLs otherwise. Right side only
OUTER All left side and right side rows kept; companion content joined when available, filled in with NULLs otherwise. Right side only
INNERDL Same as INNER. Left side only
LEFTDL Same as LEFT. Left side only
RIGHTDL Same as RIGHT. Left side only
OUTERDL Same as OUTER. Left side only

* Multiple rows having same key may be present on the left side or the right side. Multiple rows will be preserved in the join result if the appropriate jointype is used. Multiple rows having same key are never permitted on both left and right sides. The jointypes ending in DL are non-standard; DL stands for "duplicates in left side".

Three-table joins are supported (see the 3rd example above). The 2nd and 3rd tables are joined first, then that result is joined with the first table. Each of these join operations has a left side and a right side, and its own jointype.

Naming of the resultant fields is described in the SELECT manual page.

shsql performs joins by executing a separate program called shsql_join. shsql_join must be in the current command PATH, or in the dbbin directory as defined in your project config file.


Copyright Steve Grubb  


Markup created by unroff 1.0,    March 18, 2004.