syntax of outer join in 7.1devel

Started by Poul L. Christiansenover 25 years ago5 messagesgeneral
Jump to latest
#1Poul L. Christiansen
poulc@cs.auc.dk

<html><head></head><body>Hi<br>
<br>
I just grabbed the latest development version from the CVS, because i need the outer join functionality.<br>
<br>
But how do I make an outer join? <br>
<br>
What's the syntax?<br>
<br>
I will RTFM if someone points me to the docs :-)<br>
<br>
Poul L. Christiansen<br>
<br>
<br>
</body></html>

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Poul L. Christiansen (#1)
Re: syntax of outer join in 7.1devel

"Poul L. Christiansen" <poulc@cs.auc.dk> writes:

But how do I make an outer join?
What's the syntax?
I will RTFM if someone points me to the docs :-)

I'm afraid I haven't updated the FM yet :-( ... but you could look at
the examples in the JOIN regress test, src/test/regress/sql/join.sql.

Or, from the SQL92 spec, here's the grammar:

<from clause> ::= FROM <table reference> [ { <comma> <table reference> }... ]

<table reference> ::=
<table name> [ [ AS ] <correlation name>
[ <left paren> <derived column list> <right paren> ] ]
| <derived table> [ AS ] <correlation name>
[ <left paren> <derived column list> <right paren> ]
| <joined table>

<derived table> ::= <table subquery>

<derived column list> ::= <column name list>

<column name list> ::=
<column name> [ { <comma> <column name> }... ]

<joined table> ::=
<cross join>
| <qualified join>
| <left paren> <joined table> <right paren>

<cross join> ::=
<table reference> CROSS JOIN <table reference>

<qualified join> ::=
<table reference> [ NATURAL ] [ <join type> ] JOIN
<table reference> [ <join specification> ]

<join specification> ::=
<join condition>
| <named columns join>

<join condition> ::= ON <search condition>

<named columns join> ::=
USING <left paren> <join column list> <right paren>

<join type> ::=
INNER
| <outer join type> [ OUTER ]
| UNION

<outer join type> ::=
LEFT
| RIGHT
| FULL

<join column list> ::= <column name list>

We don't do UNION JOIN yet, but I think everything else shown in this
snippet is implemented ...

regards, tom lane

#3Poul L. Christiansen
poulc@cs.auc.dk
In reply to: Poul L. Christiansen (#1)
Re: syntax of outer join in 7.1devel

<html><head></head><body>Using the example:<br>
SELECT '' AS "xxx", *<br>
FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i);<br>
<br>
..doesn't work because the columns which I am using to join the tables don't
have the same name. How do i specify the columns if they don't have the same
name?<br>
<br>
Poul L. Christiansen<br>
<br>
Tom Lane wrote:<br>
<blockquote type="cite" cite="mid:6858.976116402@sss.pgh.pa.us"><pre wrap="">"Poul L. Christiansen" <a class="moz-txt-link-rfc2396E" href="mailto:poulc@cs.auc.dk">&lt;poulc@cs.auc.dk&gt;</a> writes:<br></pre>
<blockquote type="cite"><pre wrap="">But how do I make an outer join? <br>What's the syntax?<br>I will RTFM if someone points me to the docs :-)<br></pre></blockquote>
<pre wrap=""><!----><br>I'm afraid I haven't updated the FM yet :-( ... but you could look at<br>the examples in the JOIN regress test, src/test/regress/sql/join.sql.<br><br>Or, from the SQL92 spec, here's the grammar:<br><br> &lt;from clause&gt; ::= FROM &lt;table reference&gt; [ { &lt;comma&gt; &lt;table reference&gt; }... ]<br><br> &lt;table reference&gt; ::=<br> &lt;table name&gt; [ [ AS ] &lt;correlation name&gt;<br> [ &lt;left paren&gt; &lt;derived column list&gt; &lt;right paren&gt; ] ]<br> | &lt;derived table&gt; [ AS ] &lt;correlation name&gt;<br> [ &lt;left paren&gt; &lt;derived column list&gt; &lt;right paren&gt; ]<br> | &lt;joined table&gt;<br><br> &lt;derived table&gt; ::= &lt;table subquery&gt;<br><br> &lt;derived column list&gt; ::= &lt;column name list&gt;<br><br> &lt;column name list&gt; ::=<br> &lt;column name&gt; [ { &lt;comma&gt; &lt;column name&gt; }...
</blockquote>
<br>
</body></html>

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Poul L. Christiansen (#3)
Re: syntax of outer join in 7.1devel

"Poul L. Christiansen" <poulc@cs.auc.dk> writes:

<html><head></head><body>Using the example:<br>
SELECT '' AS "xxx", *<br>
FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i);<br>
<br>
..doesn't work because the columns which I am using to join the tables don't
have the same name. How do i specify the columns if they don't have the same
name?<br>

USING is just for the simple case where you want to join on identically-
named columns. (In the even simpler case where you want to join on all
identically-named columns, just say NATURAL JOIN.)

Whenever you need something more complicated, you say ON some-condition,
eg

SELECT '' AS "xxx", *
FROM J1_TBL LEFT OUTER JOIN J2_TBL ON j1_tbl.x = j2_tbl.y

Note that unlike USING and NATURAL, an ON-condition doesn't cause
columns to be omitted from the join result. So in the above example
you'd see both x and y as output columns.

Poul L. Christiansen<br>
<br>
Tom Lane wrote:<br>
<blockquote type=3D"cite" cite=3D"mid:6858.976116402@sss.pgh.pa.us"><pre wr=
ap=3D"">"Poul L. Christiansen" <a class=3D"moz-txt-link-rfc2396E" href=3D"m=
ailto:poulc@cs.auc.dk">&lt;poulc@cs.auc.dk&gt;</a> writes:<br></pre>
<blockquote type=3D"cite"><pre wrap=3D"">But how do I make an outer join?=
<br>What's the syntax?<br>I will RTFM if someone points me to the docs :-)=
<br></pre></blockquote>
<pre wrap=3D""><!----><br>I'm afraid I haven't updated the FM yet :-( .=
.. but you could look at<br>the examples in the JOIN regress test, src/test=
/regress/sql/join.sql.<br><br>Or, from the SQL92 spec, here's the grammar:<=

br> <br> &lt;from clause&gt; ::=3D FROM &lt;table reference&gt; [ { =

&lt;comma&gt; &lt;table reference&gt; }... ]<br><br> &lt;table refe=
rence&gt; ::=3D<br> &lt;table name&gt; [ [ AS ] &lt;correlat=
ion name&gt;<br> [ &lt;left paren&gt; &lt;derived column=
list&gt; &lt;right paren&gt; ] ]<br> | &lt;derived table&gt; =
[ AS ] &lt;correlation name&gt;<br> [ &lt;left paren&gt;=
&lt;derived column list&gt; &lt;right paren&gt; ]<br> | &lt;j=
oined table&gt;<br><br> &lt;derived table&gt; ::=3D &lt;table subqu=
ery&gt;<br><br> &lt;derived column list&gt; ::=3D &lt;column name l=
ist&gt;<br><br> &lt;column name list&gt; ::=3D<br> &lt=
;column name&gt; [ { &lt;comma&gt; &lt;column name&gt; }...
</blockquote>
<br>
</body></html>

BTW, would you PLEASE turn off the HTML option in your mail program?

regards, tom lane

#5Bruce Momjian
bruce@momjian.us
In reply to: Poul L. Christiansen (#3)
Re: syntax of outer join in 7.1devel

See the updated FAQ on our web site for syntax.

[ text/html is unsupported, treating like TEXT/PLAIN ]

<html><head></head><body>Using the example:<br>
SELECT '' AS "xxx", *<br>
FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i);<br>
<br>
..doesn't work because the columns which I am using to join the tables don't
have the same name. How do i specify the columns if they don't have the same
name?<br>
<br>
Poul L. Christiansen<br>
<br>
Tom Lane wrote:<br>
<blockquote type="cite" cite="mid:6858.976116402@sss.pgh.pa.us"><pre wrap="">"Poul L. Christiansen" <a class="moz-txt-link-rfc2396E" href="mailto:poulc@cs.auc.dk">&lt;poulc@cs.auc.dk&gt;</a> writes:<br></pre>
<blockquote type="cite"><pre wrap="">But how do I make an outer join? <br>What's the syntax?<br>I will RTFM if someone points me to the docs :-)<br></pre></blockquote>
<pre wrap=""><!----><br>I'm afraid I haven't updated the FM yet :-( ... but you could look at<br>the examples in the JOIN regress test, src/test/regress/sql/join.sql.<br><br>Or, from the SQL92 spec, here's the grammar:<br><br> &lt;from clause&gt; ::= FROM &lt;table reference&gt; [ { &lt;comma&gt; &lt;table reference&gt; }... ]<br><br> &lt;table reference&gt; ::=<br> &lt;table name&gt; [ [ AS ] &lt;correlation name&gt;<br> [ &lt;left paren&gt; &lt;derived column list&gt; &lt;right paren&gt; ] ]<br> | &lt;derived table&gt; [ AS ] &lt;correlation name&gt;<br> [ &lt;left paren&gt; &lt;derived column list&gt; &lt;right paren&gt; ]<br> | &lt;joined table&gt;<br><br> &lt;derived table&gt; ::= &lt;table subquery&gt;<br><br> &lt;derived column list&gt; ::= &lt;column name list&gt;<br><br> &lt;column name list&gt; ::=<br> &lt;column name&gt; [ { &lt;comma&gt; &lt;column name&gt; }...
</blockquote>
<br>
</body></html>

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026