Taking a second look, and now I'm sure thats what the query does. You won't need the grouping of course when the ID is unique, else it will eliminate duplicates. Using distinct would work as well. The current condition in the having clause should work the same in a where clause so.