More aggregate statistics!
Can we calculate more than one aggregate statistic while grouping? Surely we can!
For example, from the audi_cars
table, we can calculate the maximum price
and tax
for each year
.
123SELECT AVG(price) AS "avg_price", AVG(tax) AS "avg_tax", year FROM audi_cars GROUP BY year
Please note, while grouping you can put non-aggregated function within
SELECT
statement only if it figures withinGROUP BY
, otherwise - it has to be aggregated.
Also note, that in the example above without aliases (
AS
) there will be two columns with identical names, which is unacceptable in SQL.
Another note: you can use aggregate functions for ordering after
GROUP BY
statement. It will filter based on calculations within groups.
Swipe to start coding
From the audi_cars
table find out the minimum and the maximum price
s for each model
. Sort in descending order by minimum price.
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 2.63Awesome!
Completion rate improved to 2.63
More aggregate statistics!
Can we calculate more than one aggregate statistic while grouping? Surely we can!
For example, from the audi_cars
table, we can calculate the maximum price
and tax
for each year
.
123SELECT AVG(price) AS "avg_price", AVG(tax) AS "avg_tax", year FROM audi_cars GROUP BY year
Please note, while grouping you can put non-aggregated function within
SELECT
statement only if it figures withinGROUP BY
, otherwise - it has to be aggregated.
Also note, that in the example above without aliases (
AS
) there will be two columns with identical names, which is unacceptable in SQL.
Another note: you can use aggregate functions for ordering after
GROUP BY
statement. It will filter based on calculations within groups.
Swipe to start coding
From the audi_cars
table find out the minimum and the maximum price
s for each model
. Sort in descending order by minimum price.
Løsning
Tak for dine kommentarer!
single
Awesome!
Completion rate improved to 2.63
More aggregate statistics!
Stryg for at vise menuen
Can we calculate more than one aggregate statistic while grouping? Surely we can!
For example, from the audi_cars
table, we can calculate the maximum price
and tax
for each year
.
123SELECT AVG(price) AS "avg_price", AVG(tax) AS "avg_tax", year FROM audi_cars GROUP BY year
Please note, while grouping you can put non-aggregated function within
SELECT
statement only if it figures withinGROUP BY
, otherwise - it has to be aggregated.
Also note, that in the example above without aliases (
AS
) there will be two columns with identical names, which is unacceptable in SQL.
Another note: you can use aggregate functions for ordering after
GROUP BY
statement. It will filter based on calculations within groups.
Swipe to start coding
From the audi_cars
table find out the minimum and the maximum price
s for each model
. Sort in descending order by minimum price.
Løsning
Tak for dine kommentarer!