Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.33 KB

6mxs.md

File metadata and controls

50 lines (33 loc) · 1.33 KB

lang.psql.aggregation

You can aggregate repeated records of a field

Synopsis

  SELECT <column-name> FROM <table-names>
  GROUP BY <column-name[s]> HAVING <condition>;

Overview

You can use the GROUP BY command to aggregate repeated columns and display them as a resuming.

It is most commonly used with other aggregate functions like COUNT()

When using multiple selected columns and only one agreggator the group by must reference the other columns

Commands

  • HAVING: Perform additional filtering after the aggregation and summary
    • Unlke WHERE you can use aggregate functions after HAVING

Cookbook

Add additional filtering after the aggregation

You can perform some extra filtering after the aggregation with the HAVING command, this command is different from WHERE because it does it's filtering after the aggregation

It also must come before the sortin ORDER BY command

  SELECT country_of_birth, COUNT(*) FROM person
  GROUP BY country_of_birth HAVING COUNT(*) > 5
  ORDER BY country_of_birth;

Find the minimum value for each record group

If you want to know what are the minimum values for each aggregation field

  SELECT brand, model, MIN(prince) FROM car
  GROUP BY brand, model;