Group & Aggregate Records in Sequelize

Sequelize·2 min read·Jan 1, 2025

In SQL, grouping and aggregation are used to summarize and analyze data.

Grouping allows us to group records that share a common column, while aggregation functions such as COUNT, SUM, AVG, MAX, and MIN allow us to perform calculations on groups of data.

Aggregation functions

To execute an aggregation function like COUNT or SUM on an column, you can use the Sequelize.fn() static method:

{  attributes: [    [Sequelize.fn(<function>, Sequelize.col(<attribute>)), <alias>?]  ]}

Where:

  • function is a string containing the name of the aggregation function.
  • attribute is a string containing the name of the column to perform an aggregation on.
  • alias is an optional string containing the alias of the column.

Note: The Sequelize.col() static method is used to ensure that column names are properly interpreted as columns and not strings.

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.