Quantcast

how to do 'deep queries'?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to do 'deep queries'?

jeff sacksteder
Is there supported syntax to do 'deep' queries? That is where A relates to B relates to C, returning fields from each table?

This doesn't seem to work. Is there a google-able term for this sort of query?

select
   foo.aaa,
   bar.bbb,
   baz.ccc

from
   foo,bar,baz

where
   foo.bar_id = bar.id
and
   bar.baz_id = baz.id



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

Stewart Ben (RBAU/EQS4) *
> Is there supported syntax to do 'deep' queries? That is where
> A relates to B relates to C, returning fields from each table?
>
> This doesn't seem to work. Is there a google-able term for
> this sort of query?
>
> select
>    foo.aaa,
>    bar.bbb,
>    baz.ccc
>
> from
>    foo,bar,baz
>
> where
>    foo.bar_id = bar.id
> and
>    bar.baz_id = baz.id

This works for me..

SELECT table1.state, table2.coursename, table3.firstname
  FROM backend.enrolments table1, backend.courses table2, backend.users
table3
 WHERE table1.user = table3.employeeno
   AND table1.course = table2.courseid;

What errors are you getting?

Best regards,

Ben Stewart

--
Robert Bosch (Australia) Pty. Ltd.
Engineering Quality Services, Student Software Engineer (RBAU/EQS4)
Locked Bag 66 - Clayton South, VIC 3169 - AUSTRALIA
Tel: +61 3 9541-7002 Fax: +61 3 9541-7700
mailto:[hidden email]
http://www.bosch.com.au/ 

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

Anthony Molinaro
In reply to this post by jeff sacksteder

that query is 100% correct.

 

it’s just an equijoin (a type of inner join) between 3 tables.

 

the syntax you show is how queries should be written and is more

representative of what a joins between relations really are:

Cartesian products with filters applied

 

the ansi syntax, the explicit JOIN … ON  stuff is (imho) unnecessary,

useful only for outer joins since all the vendors did it differently.

 

what you have will work for postgreSQL, I used the syntax you show in my book

for every single join recipe except for outjoins.

 

are you seeing errors?

 

regards,

  Anthony

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of jeff sacksteder
Sent:
Monday, September 26, 2005 8:34 PM
To: [hidden email]
Subject: [SQL] how to do 'deep queries'?

 

Is there supported syntax to do 'deep' queries? That is where A relates to B relates to C, returning fields from each table?

This doesn't seem to work. Is there a google-able term for this sort of query?

select
   foo.aaa,
   bar.bbb,
   baz.ccc

from
   foo,bar,baz

where
   foo.bar_id = bar.id
and
   bar.baz_id = baz.id


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

jeff sacksteder
Nevermind. It's late here and I'm not thinking clearly. Problem solved.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

Daryl Richter
In reply to this post by Anthony Molinaro
Anthony Molinaro wrote:

> that query is 100% correct.
>  
> it's just an equijoin (a type of inner join) between 3 tables.
>  
> the syntax you show is how queries should be written and is more
> representative of what a joins between relations really are:
> Cartesian products with filters applied
>  
> the ansi syntax, the explicit JOIN ... ON  stuff is (imho) unnecessary,
> useful only for outer joins since all the vendors did it differently.
>

Whether you feel that is unnecessary or not, it *is* the ANSI Standard
and is thus, by definition, "how queries should be written."

In addition to cleaning up the outer join issue, it was added to make
the *intention* of the query clearer.

Because others are likely to read your query many more times than you
write it, clarity of intent *is* important.

> what you have will work for postgreSQL, I used the syntax you show in my
> book
> for every single join recipe except for outjoins.
>  
> are you seeing errors?
>  
> regards,
>   Anthony

[original snipped]

--
Daryl

"We want great men who, when fortune frowns, will not be discouraged."
     -- Colonel Henry Knox, 1776


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

Anthony Molinaro
In reply to this post by jeff sacksteder
Daryl,
 
> Whether you feel that is unnecessary or not, it *is* the ANSI Standard

> and is thus, by definition, "how queries should be written."  
 
I disagree 100%.  Oracle and db2 introduced window functions years
before
Ansi added them. Should we not have used them? It absurd to avoid using
a feature cuz it's not ansi.

Honestly, Don't be a slave to ansi, you miss out on all the great
vendor specific functionality *that you're already paying for*

> it was added to make the *intention* of the query clearer.

More clearer to whom?

Certainly not developers who have been working for many years
using the old syntax.

The intention of the old syntax is perfect. Realize that the problem is
not the old syntax, the problem is the watered down database field
today.
I see this more and more with each interview I conduct looking
for dba's and developers.

You know, it used to be that database developers
had a solid background in math and relational theory.  
Sadly, that's not the case anymore...

   select * from a,b where a.id=b.id  

Suggests a Cartesian product between two relations then a filter to keep
only matching rows.

That's a join. And that syntax is a *perfect* representation of it.

So to whom is ansi more clear? To the person who knows nothing about
databases and decided one day to get a certification and call themselves
an expert?

Or maybe the person who decided one day on a whim to get into databases
and not realize that tons of code from the prior decade use the old
style syntax?

> Because others are likely to read your query many more times than you
> write it, clarity of intent *is* important.

I've never worked in a place that used ANSI only syntax and I've never
had a problem with clarity nor any developers I've worked with.
So, I don't at all get what you're saying...

Old style is short and sweet and perfect.
Ansi dumbed it down, that's the bottom line.
And for people who've been developing for sometime,
It's wholly unnecessary.

Regards,
  Anthony

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Daryl Richter
Sent: Tuesday, September 27, 2005 9:24 AM
To: [hidden email]
Subject: Re: [SQL] how to do 'deep queries'?

Anthony Molinaro wrote:
> that query is 100% correct.
>  
> it's just an equijoin (a type of inner join) between 3 tables.
>  
> the syntax you show is how queries should be written and is more
> representative of what a joins between relations really are:
> Cartesian products with filters applied
>  
> the ansi syntax, the explicit JOIN ... ON  stuff is (imho)
unnecessary,
> useful only for outer joins since all the vendors did it differently.
>

Whether you feel that is unnecessary or not, it *is* the ANSI Standard
and is thus, by definition, "how queries should be written."

In addition to cleaning up the outer join issue, it was added to make
the *intention* of the query clearer.

Because others are likely to read your query many more times than you
write it, clarity of intent *is* important.

> what you have will work for postgreSQL, I used the syntax you show in
my
> book
> for every single join recipe except for outjoins.
>  
> are you seeing errors?
>  
> regards,
>   Anthony

[original snipped]

--
Daryl

"We want great men who, when fortune frowns, will not be discouraged."
     -- Colonel Henry Knox, 1776


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

Daryl Richter
Anthony Molinaro wrote:
 > Daryl,
 >
 >
 >>Whether you feel that is unnecessary or not, it *is* the ANSI Standard
 >
 >
 >>and is thus, by definition, "how queries should be written."
 >
 >
 > I disagree 100%.  Oracle and db2 introduced window functions years
 > before
 > Ansi added them. Should we not have used them? It absurd to avoid using
 > a feature cuz it's not ansi.
 >

Of course it would be absurd, I have not suggested otherwise.  Joins are
not a *new* feature.

 > Honestly, Don't be a slave to ansi, you miss out on all the great
 > vendor specific functionality *that you're already paying for*
 >
 >
 >>it was added to make the *intention* of the query clearer.
 >
 >
 > More clearer to whom?
 >
 > Certainly not developers who have been working for many years
 > using the old syntax.
 >
 > The intention of the old syntax is perfect. Realize that the problem is
 > not the old syntax, the problem is the watered down database field
 > today.
 > I see this more and more with each interview I conduct looking
 > for dba's and developers.
 >

I generally agree with your assessment of the state of database
knowledge (particularly re developers).  It is, however, the reality we
live in.

[snipped nostalgia and back-patting]

 > I've never worked in a place that used ANSI only syntax and I've never
 > had a problem with clarity nor any developers I've worked with.
 > So, I don't at all get what you're saying...

 > Old style is short and sweet and perfect.
 > Ansi dumbed it down, that's the bottom line.
 > And for people who've been developing for sometime,
 > It's wholly unnecessary.
 >

Well, perhaps you will one day and a developer will hose your server
with a "accidental" cross join and then you will understand.

But hopefully not.  ;)

 > Regards,
 >   Anthony
 >

[rest snipped]

--
Daryl
Director of Technology

((         Brandywine Asset Management          )
  ( "Expanding the Science of Global Investing"  )
  (          http://www.brandywine.com           ))



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to do 'deep queries'?

Anthony Molinaro
In reply to this post by jeff sacksteder
> Well, perhaps you will one day and a developer will hose your server
> with a "accidental" cross join and then you will understand.

Hehe :))

hey man, that's what testing and code review is all about
(dev teams still do that don't they?)

Accidental cartesians don't get to production ;)

Regards,
  Anthony

-----Original Message-----
From: Daryl Richter [mailto:[hidden email]]
Sent: Tuesday, September 27, 2005 11:35 AM
To: Anthony Molinaro
Cc: [hidden email]
Subject: Re: [SQL] how to do 'deep queries'?

Anthony Molinaro wrote:
 > Daryl,
 >
 >
 >>Whether you feel that is unnecessary or not, it *is* the ANSI
Standard
 >
 >
 >>and is thus, by definition, "how queries should be written."
 >
 >
 > I disagree 100%.  Oracle and db2 introduced window functions years
 > before
 > Ansi added them. Should we not have used them? It absurd to avoid
using
 > a feature cuz it's not ansi.
 >

Of course it would be absurd, I have not suggested otherwise.  Joins are

not a *new* feature.

 > Honestly, Don't be a slave to ansi, you miss out on all the great
 > vendor specific functionality *that you're already paying for*
 >
 >
 >>it was added to make the *intention* of the query clearer.
 >
 >
 > More clearer to whom?
 >
 > Certainly not developers who have been working for many years
 > using the old syntax.
 >
 > The intention of the old syntax is perfect. Realize that the problem
is
 > not the old syntax, the problem is the watered down database field
 > today.
 > I see this more and more with each interview I conduct looking
 > for dba's and developers.
 >

I generally agree with your assessment of the state of database
knowledge (particularly re developers).  It is, however, the reality we
live in.

[snipped nostalgia and back-patting]

 > I've never worked in a place that used ANSI only syntax and I've
never
 > had a problem with clarity nor any developers I've worked with.
 > So, I don't at all get what you're saying...

 > Old style is short and sweet and perfect.
 > Ansi dumbed it down, that's the bottom line.
 > And for people who've been developing for sometime,
 > It's wholly unnecessary.
 >

Well, perhaps you will one day and a developer will hose your server
with a "accidental" cross join and then you will understand.

But hopefully not.  ;)

 > Regards,
 >   Anthony
 >

[rest snipped]

--
Daryl
Director of Technology

((         Brandywine Asset Management          )
  ( "Expanding the Science of Global Investing"  )
  (          http://www.brandywine.com           ))



---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faq
Loading...