Home > Software design >  SQL - Difference between .* and * in aggregate function query
SQL - Difference between .* and * in aggregate function query

Time:09-22

SELECT reviews.*, COUNT(comments.review_id)
AS comment_count
FROM reviews
LEFT JOIN comments ON comments.review_id = reviews.review_id
GROUP BY reviews.review_id
ORDER BY reviews.review_id ASC;

When I run this code I get exactly what I want from my SQL query, however if I run the following

SELECT *, COUNT(comments.review_id)
AS comment_count
FROM reviews
LEFT JOIN comments ON comments.review_id = reviews.review_id
GROUP BY reviews.review_id
ORDER BY reviews.review_id ASC;

then I get an error "column must appear in GROUP BY clause or be used in an aggregate function

Just wondered what the difference was and why the behaviour is different.

Thanks

CodePudding user response:

In the first example, the column are taken only from the reviews table. Although not databases allow the use of SELECT * with GROUP BY, it is allowed by Standard SQL, assuming that review_id is the primary key.

The issue is that that you are including columns in the SELECT that are not included in the GROUP BY. This is only allowed -- in certain databases -- under very special circumstances, where the columns in the GROUP BY are declared to uniquely identify each row (which a primary key does).

The second example has columns from comments that do not meet this condition. Hence it is not allowed.

CodePudding user response:

In the select part of the query with group by, you can chose only those columns which you used in group by.

Since you did group by reviews.review_id, you can get the output for the first case. In the second query you are try to get all the records and that is not possible with group by.

You can use window function if you need to select columns which are not present in your group by clause. Hope it makes sense.

https://www.windowfunctions.com/

  • Related