course_sql

SQL 4 - Aggregations

Outcomes

Reduce data rows by pivoting the data and filter the results.

Aggregation Functions

Note that we sometimes use COUNT(*) to count the number of rows, and othertimes we used COUNT(field_name) to count a specific field. This is particularly important when we start using joins.

GROUP BY

Group by returns a single row for each unique value (or set of values). It’s very useful in combination with an aggregation function. SELECT year, sum(sales) as year_sales FROM sales GROUP BY year

We also may need to limit the results. This can be tricky, as WHERE and HAVING appear similar.

The key is the WHERE runs first! Then the group by runs, and finally the HAVING. The syntax of HAVING is identical to WHERE. SELECT year, sum(sales) as year_sales FROM sales WHERE year < 2000 GROUP BY year HAVING year_sales > 100

Further practice

See SQL Zoo 5 SUM and COUNT (all questions)

Problems