Uses of the GROUP BY Function
Learn how to group rows and use aggregate functions on groups.
When retrieving data from the database, we can group rows using the GROUP BY
statement.
Basic use of GROUP BY
The GROUP BY
function is used as part of a SELECT
statement:
SELECT ColumnName FROM TableSchema.TableName
GROUP BY ColumnName;
In this basic form, GROUP BY
allows us to see the distinct values in a given column. In some cases, this approach is much more effective in terms of performance compared to the DISTINCT
keyword. The reason is that DISTINCT
is applied to the whole query while GROUP BY
eliminates the duplicates first and then performs the rest of the work. In the code below, we use GROUP BY
to print out the names of teams that are participating in the competition:
Get hands-on with 1400+ tech skills courses.