🚀
8. Aggregate Functions
++++
Data Engineering
May 2026×8 min read

Use COUNT, SUM, AVG, MIN, and MAX to summarize rows and turn raw tables into useful metrics.

8. Aggregate Functions

Driptanil Datta
Driptanil DattaSoftware Developer

Aggregate Functions

Aggregate functions summarize many rows into one value. They are the foundation of reporting queries.

Sample table

CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  order_date DATE,
  status VARCHAR(20),
  total_amount DECIMAL(10, 2)
);
 
INSERT INTO orders
VALUES
  (101, 1, "2026-01-05", "paid", 250.00),
  (102, 2, "2026-01-06", "paid", 1200.50),
  (103, 1, "2026-01-10", "cancelled", 80.00),
  (104, 3, "2026-01-11", "paid", 740.25),
  (105, 4, "2026-01-12", "pending", 980.00);

COUNT

COUNT(*) counts rows.

SELECT COUNT(*) AS total_orders
FROM orders;

COUNT(column_name) counts non-null values in that column.

SELECT COUNT(total_amount) AS orders_with_amount
FROM orders;

SUM

SUM adds numeric values.

SELECT SUM(total_amount) AS gross_order_value
FROM orders;

Filter first when the metric should exclude rows.

SELECT SUM(total_amount) AS paid_revenue
FROM orders
WHERE status = "paid";

AVG

AVG calculates the average of non-null values.

SELECT AVG(total_amount) AS average_order_value
FROM orders
WHERE status = "paid";

MIN and MAX

MIN and MAX find the lowest and highest values.

SELECT
  MIN(total_amount) AS smallest_order,
  MAX(total_amount) AS largest_order
FROM orders;

They also work with dates.

SELECT
  MIN(order_date) AS first_order_date,
  MAX(order_date) AS latest_order_date
FROM orders;

Multiple metrics in one query

You can calculate several metrics at once.

SELECT
  COUNT(*) AS total_orders,
  SUM(total_amount) AS total_value,
  AVG(total_amount) AS average_value,
  MIN(total_amount) AS min_value,
  MAX(total_amount) AS max_value
FROM orders;

Aggregates ignore NULL values except COUNT(*), which counts rows regardless of null columns.

Drip

Driptanil Datta

Software Developer

Building full-stack systems, one commit at a time. This blog is a centralized learning archive for developers.

Legal Notes
Disclaimer

The content provided on this blog is for educational and informational purposes only. While I strive for accuracy, all information is provided "as is" without any warranties of completeness, reliability, or accuracy. Any action you take upon the information found on this website is strictly at your own risk.

Copyright & IP

Certain technical content, interview questions, and datasets are curated from external educational sources to provide a centralized learning resource. Respect for original authorship is maintained; no copyright infringement is intended. All trademarks, logos, and brand names are the property of their respective owners.

System Operational

© 2026 Driptanil Datta. All rights reserved.