spacing
spacing

Reputation: 780

Does group by with having require aggregate function on the select instruction?

Let's say I have a table

InspectLog​(​userid, attr1, attr2)

I want to know the userid of the users who inspected more than 5 times.

Here's what I have figured out so far:

SELECT I.userid
 FROM InspectLog I
 GROUP BY I.userid
 HAVING Count(*) >5;

My question is, as I've seen a lot of examples out there when a having is used they also call Count() in the select instruction like so.

SELECT I.userid, Count(*) AS c
 FROM InspectLog I
 GROUP BY I.userid
 HAVING Count(*)>5;

Does having require to use the aggregate function at the start in the select instruction? Will the output be the same. From what I'm getting the output will differ in having an extra c column in this case. But will the Count() on HAVING work properly without the first one?

Upvotes: 0

Views: 69

Answers (1)

Speuline
Speuline

Reputation: 201

Short answer: No, you do not need to add "Count()" in the Select Clause.

It'll only add an extra column in the result.

Upvotes: 1

Related Questions