TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2716 1.51K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 5

Aggregate Functions: COUNT, SUM, AVG, MIN & MAX ๐Ÿ“Š

So far, you've learned how to retrieve, filter, and sort individual rows.

Now we're moving to one of the most important skills for a Data Analyst:



Turning thousands of rows into meaningful business metrics.



For example:

How many customers do we have?

What is our total revenue?

What is the average order value?

What is the highest salary?

What is the lowest product price?

That's exactly what aggregate functions are designed for.

1๏ธโƒฃ What Are Aggregate Functions?

Aggregate functions perform a calculation across multiple rows and return a summarized result.

The five essential functions are:

โ€ข COUNT() Counts rows/values

โ€ข SUM() Calculates total

โ€ข AVG() Calculates average

โ€ข MIN() Finds minimum

โ€ข MAX() Finds maximum

2๏ธโƒฃ COUNT()

COUNT() is used to count records or non-NULL values.

Count all rows:

SELECT COUNT(*) AS total_customers
FROM customers;


If there are 5,000 customers: total_customers = 5000

3๏ธโƒฃ COUNT(*) vs COUNT(column)

This distinction is extremely important.

COUNT(*) Counts rows.

SELECT COUNT(*)
FROM employees;


COUNT(column) Counts non-NULL values in that column.

SELECT COUNT(manager_id)
FROM employees;


Suppose: employee A | 101, B | 102, C | NULL, D | 103

Then: COUNT(*) = 4, COUNT(manager_id) = 3

Because one manager_id is NULL.

Interview Tip:



COUNT(*) counts rows; COUNT(column) counts non-NULL values in that column.



4๏ธโƒฃ COUNT(DISTINCT)

Use COUNT(DISTINCT ...) when you want to count unique values.

Example:

SELECT
COUNT(DISTINCT customer_id) AS unique_customers
FROM orders;


Suppose: customer_id 101, 101, 102, 103, 103, 103

Then: COUNT(*) = 6, COUNT(DISTINCT customer_id) = 3

This is extremely common in analytics.

5๏ธโƒฃ Real-World Example: Active Customers

Suppose your orders table contains thousands of orders.

The business asks:



How many unique customers placed an order?



SELECT
COUNT(DISTINCT customer_id) AS active_customers
FROM orders;


Notice that we're counting customers, not orders. One customer may have placed 20 orders, but should still count as one unique customer.

6๏ธโƒฃ SUM()

SUM() calculates the total of a numeric column.

Example:

SELECT
SUM(amount) AS total_revenue
FROM orders;


If the amounts are: 1000, 2000, 1500, 3000 then: SUM = 7500

7๏ธโƒฃ SUM With a Condition

You can combine SUM() with WHERE.

Example:



Calculate revenue from completed orders only.



SELECT
SUM(amount) AS completed_revenue
FROM orders
WHERE order_status = 'Completed';


This is a very common business query.

8๏ธโƒฃ AVG()

AVG() calculates the average of non-NULL numeric values.

Example:

SELECT
AVG(salary) AS average_salary
FROM employees;


If salaries are: 50000, 60000, 70000 then: Average = 60000

9๏ธโƒฃ AVG and NULL Values

AVG() generally ignores NULL values.

Suppose: salary 50000, 60000, NULL, 70000

The average is: (50000 + 60000 + 70000) / 3 = 60000

It doesn't divide by 4. This is important when working with incomplete real-world data.

๐Ÿ”Ÿ MIN()

MIN() finds the smallest value.

Example:

SELECT
MIN(salary) AS lowest_salary
FROM employees;


For products:
  • โค 4
  • ๐Ÿ‘ 1
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’