File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Count on a subquery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Count on a subquery" Watch "Count on a subquery" New topic

Count on a subquery

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Hi all,
Is it possible in SQL to do a count on a subquery? For example..

select parent_name , count (select child_id from child a where = from parent p group by parent_name

I know that SQL is wrong. It is meant only to be psuedo-code, but I hope you get the idea.

Thanks for any help.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46332
Not sure, but I think yes.
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

Can you try

select p.parent_name , (select count (a.child_id) from child a where = from parent p

attention: untested.

OCUP UML fundamental and ITIL foundation
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

This works in postgresql. Not sure if it would work in all databases, but it is fast to try in yours.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Count on a subquery
It's not a secret anymore!