When using GROUP BY, do we always have to group by a selected column?

Question

When using the GROUP BY clause, do we always have to group by one of the selected columns listed after SELECT?

Answer

No, you can GROUP BY a column that was not included in the SELECT statement.

For example, this query does not list the price column in the SELECT, but it does group the data by that column.

SELECT name, downloads
FROM fake_apps
GROUP BY price;

However, usually we do include the grouped by column in the SELECT for the sake of clarity, so that it’s easier to see what rows belong to which group.

17 Likes

I tire it and it did not work out… in the example project on CAmy it gave me only the SUM.

In your example, what name and download will be then displayed in each line?

The display will start with price = 0.0, but what name and downloads will it display then?

In my experience, it will display the name, downloads of the last item having a price = 0.0

This question is confusing.

2 Likes