Aggregations

Aggregators only aggregate vertically - the values of a column.

SQL evaluates the aggregations before the LIMIT clause.

COUNT

SELECT COUNT (*) AS account_count
FROM accounts
SELECT COUNT(primary_poc) AS account_primary_poc_count
FROM accounts

COUNT does not consider rows that have NULL values. Therefore, this can be useful for quickly identifying which rows have missing data.

SUM

Unlike COUNT, you can only use SUM on numeric columns. However, SUM will ignore NULL values.

MIN, MAX & AVG

MIN and MAX are aggregators that again ignore NULL values.

GROUP BY

  • used to aggregate data within subsets of the data.

  • Any column in the SELECT statement that is not within an aggregator must be in the GROUP BY clause.

  • You can GROUP BY multiple columns at once, The order of column names in your GROUP BY clause doesn’t matter—the results will be the same regardless.

DISTINCT

using DISTINCT, particularly in aggregations, can slow your queries down quite a bit.

HAVING

DATE Functions

Functions

CASE Statements

  • The CASE statement always goes in the SELECT clause.

  • CASE must include the following components: WHEN, THEN, and END. ELSE is an optional component to catch cases that didn’t meet any of the other previous CASE conditions.

Last updated