Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: Parentheses and joins

Re: Parentheses and joins

From: <Jared.Still_at_radisys.com>
Date: Wed, 10 Dec 2003 12:19:24 -0800
Message-ID: <F001.005D9746.20031210121924@fatcity.com>


Content-Type: text/plain; charset="us-ascii"

Jonathan,

I didn't realize that parentheses would affect to optimizer to change the join order.

A little experimentation shows that it can indeed be done.

For anyone that wants to see it, the joins are below.

I learn something every day around here.

Jared

select

   e.ename

   , e.empno
   , d.dname
   , m.ename

from

   scott.emp e
   join scott.dept d on d.deptno=e.deptno    join scott.emp m on m.empno=e.mgr
/

select

   e.ename

   , e.empno
   , d.dname
   , m.ename

from

   scott.dept d join
   (scott.emp e join scott.emp m on m.empno=e.mgr)    on d.deptno = e.deptno
/

select

   e.ename

   , e.empno
   , d.dname
   , m.ename

from

   scott.emp m join
   (scott.emp e join scott.dept d on e.deptno=d.deptno)    on m.mgr = e.empno
/

Jonathan Gennick <jonathan_at_gennick.com>
Sent by: ml-errors_at_fatcity.com
 12/10/2003 10:54 AM
 Please respond to ORACLE-L  

        To:     Multiple recipients of list ORACLE-L <ORACLE-L_at_fatcity.com>
        cc: 
        Subject:        Parentheses and joins


I looking for a good, simple example of when parentheses matter when writing a join query using the new, SQL92 join syntax. For example, I could write:

SELECT *
FROM county y JOIN city c

     ON y.county_id = c.county_id
     JOIN attraction a
     ON c.city_id = a.city_id;

and I could use parens to clarify the join order:

SELECT *
FROM (county y JOIN city c

     ON y.county_id = c.county_id)
     JOIN attraction a
     ON c.city_id = a.city_id;

I could even force a different join order:

SELECT *
FROM county y JOIN (city c

     JOIN attraction a
     ON c.city_id = a.city_id)
     ON y.county_id = c.county_id;

I'm struggling to come up with a good example of when you might *need* to use parens. I'm not having a really good day today. In fact, I'm having a really, really bad day, so maybe I'm not thinking too clearly, but so far I'm unable to come up with a good example to demonstrate the necessity of parentheses. Can anyone help me out with this?

Best regards,

Jonathan Gennick --- Brighten the corner where you are http://Gennick.com * 906.387.1698 * mailto:jonathan@gennick.com

Join the Oracle-article list and receive one article on Oracle technologies per month by email. To join, visit http://four.pairlist.net/mailman/listinfo/oracle-article, or send email to Oracle-article-request_at_gennick.com and include the word "subscribe" in either the subject or body.

-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.net
-- 
Author: Jonathan Gennick
  INET: jonathan_at_gennick.com

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).



--=_alternative 006F6EF188256DF8_=
Content-Type: text/html; charset="us-ascii"



<br><font size=2 face="sans-serif">Jonathan,</font>
<br>
<br><font size=2 face="sans-serif">I didn't realize that parentheses would affect to optimizer to change the join order.</font>
<br>
<br><font size=2 face="sans-serif">A little experimentation shows that it can indeed be done.</font>
<br>
<br><font size=2 face="sans-serif">For anyone that wants to see it, the joins are below.</font>
<br>
<br><font size=2 face="sans-serif">I learn something every day around here.</font>
<br>
<br><font size=2 face="sans-serif">Jared</font>
<br>
<br><font size=2 face="Courier">select</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;e.ename</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, e.empno</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, d.dname</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, m.ename</font>
<br><font size=2 face="Courier">from</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;scott.emp e</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;join scott.dept d on d.deptno=e.deptno</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;join scott.emp m on m.empno=e.mgr</font>
<br><font size=2 face="Courier">/</font>
<br>
<br>
<br><font size=2 face="Courier">select</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;e.ename</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, e.empno</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, d.dname</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, m.ename</font>
<br><font size=2 face="Courier">from</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;scott.dept d join</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;(scott.emp e join scott.emp m on m.empno=e.mgr)</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;on d.deptno = e.deptno</font>
<br><font size=2 face="Courier">/</font>
<br>
<br>
<br><font size=2 face="Courier">select</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;e.ename</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, e.empno</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, d.dname</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;, m.ename</font>
<br><font size=2 face="Courier">from</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;scott.emp m join</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;(scott.emp e join scott.dept d on e.deptno=d.deptno)</font>
<br><font size=2 face="Courier">&nbsp; &nbsp;on m.mgr = e.empno</font>
<br><font size=2 face="Courier">/<br>
</font>
<br>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1 face="sans-serif"><b>Jonathan Gennick &lt;jonathan_at_gennick.com&gt;</b></font>
<br><font size=1 face="sans-serif">Sent by: ml-errors_at_fatcity.com</font>
<p><font size=1 face="sans-serif">&nbsp;12/10/2003 10:54 AM</font>
<br><font size=2 face="sans-serif">&nbsp;</font><font size=1 face="sans-serif">Please respond to ORACLE-L</font>
<br>
<td><font size=1 face="Arial">&nbsp; &nbsp; &nbsp; &nbsp; </font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; To: &nbsp; &nbsp; &nbsp; &nbsp;Multiple recipients of list ORACLE-L &lt;ORACLE-L_at_fatcity.com&gt;</font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; cc: &nbsp; &nbsp; &nbsp; &nbsp;</font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; Subject: &nbsp; &nbsp; &nbsp; &nbsp;Parentheses and joins</font></table>
<br>
<br>
<br><font size=2 face="Courier New">I looking for a good, simple example of when parentheses<br>
matter when writing a join query using the new, SQL92 join<br> syntax. For example, I could write:<br>
<br>
SELECT *<br> FROM county y JOIN city c<br> &nbsp; &nbsp; ON y.county_id = c.county_id<br> &nbsp; &nbsp; JOIN attraction a<br> &nbsp; &nbsp; ON c.city_id = a.city_id;<br>
<br>
and I could use parens to clarify the join order:<br>
<br>
SELECT *<br> FROM (county y JOIN city c<br> &nbsp; &nbsp; ON y.county_id = c.county_id)<br> &nbsp; &nbsp; JOIN attraction a<br> &nbsp; &nbsp; ON c.city_id = a.city_id;<br>
<br>
I could even force a different join order:<br>
<br>
SELECT *<br> FROM county y JOIN (city c<br> &nbsp; &nbsp; JOIN attraction a<br> &nbsp; &nbsp; ON c.city_id = a.city_id)<br> &nbsp; &nbsp; ON y.county_id = c.county_id;<br>
<br>
I'm struggling to come up with a good example of when you<br> might *need* to use parens. I'm not having a really good day<br> today. In fact, I'm having a really, really bad day, so<br> maybe I'm not thinking too clearly, but so far I'm unable to<br> come up with a good example to demonstrate the necessity of<br> parentheses. Can anyone help me out with this?<br>
<br>
Best regards,<br>
<br>
Jonathan Gennick --- Brighten the corner where you are<br> http://Gennick.com * 906.387.1698 * mailto:jonathan@gennick.com<br>
<br>
Join the Oracle-article list and receive one<br> article on Oracle technologies per month by <br> email. To join, visit http://four.pairlist.net/mailman/listinfo/oracle-article, <br> or send email to Oracle-article-request_at_gennick.com and <br> include the word &quot;subscribe&quot; in either the subject or body.<br>
<br>
-- <br> Please see the official ORACLE-L FAQ: http://www.orafaq.net<br> -- <br> Author: Jonathan Gennick<br> &nbsp;INET: jonathan_at_gennick.com<br>
<br>
Fat City Network Services &nbsp; &nbsp;-- 858-538-5051 http://www.fatcity.com<br> San Diego, California &nbsp; &nbsp; &nbsp; &nbsp;-- Mailing list and web hosting services<br> ---------------------------------------------------------------------<br> To REMOVE yourself from this mailing list, send an E-Mail message<br> to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in<br> the message BODY, include a line containing: UNSUB ORACLE-L<br> (or the name of mailing list you want to be removed from). &nbsp;You may<br> also send the HELP command for other information (like subscribing).<br>
</font>
<br>
<br>
--=_alternative 006F6EF188256DF8_=-- -- Please see the official ORACLE-L FAQ: http://www.orafaq.net -- Author: INET: Jared.Still_at_radisys.com Fat City Network Services -- 858-538-5051 http://www.fatcity.com San Diego, California -- Mailing list and web hosting services --------------------------------------------------------------------- To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
Received on Wed Dec 10 2003 - 14:19:24 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US