Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Subselect in SELECT
Matthias Matker wrote:
> John K. Hinsdale schrieb:
> > The preferred way in SQL to express your desired result is something
> > like:
> >
> > SELECT P.name, count(*) AS no_of_cds
> > FROM persons P, person_cds PC
> > WHERE P.name = PC.pname
> > GROUP BY P.name
> > HAVING count(*) < 280
> > ORDER BY P.name
> >
>
> Thanks, but what is wrong in my "solution"?
Hi Matthias,
Nothing "wrong" with it; its just a lot harder to write and understand than it needs to be. That is, unless you're designing, grading, or doing a homework assignment, with the purpose of exercising the subquery functionality of SQL.
> My only problem is to get the value of
> ( SELECT COUNT(*) amount FROM person_cds WHERE
> ( person_cds.pname= persons.name))
> to be stored as "amount"
OK, this should work if you insist on using subqueries:
SELECT name, amount
FROM ( SELECT persons.name,
( SELECT count(*) FROM person_cds WHERE person_cds.name = persons.name ) AS amount FROM persons )
But now you've got three SELECTs where a simple join will do! Please tell me this is some sort of exercise and not real work. ;)
HTH,
John Hinsdale
Received on Sat Jan 20 2007 - 18:31:28 CST
![]() |
![]() |