SQL Aggregate Functions: COUNT, SUM, AVG, MIN, MAX

๐Ÿ’ก Concept Name

SQL Aggregate Functions perform calculations on multiple rows and return a single summary value.

๐Ÿ“˜ Quick Intro

Aggregate functions are used to compute values such as totals, averages, minimums, maximums, and counts in SQL queries. They are especially useful with GROUP BY to analyze grouped data.

๐Ÿง  Analogy / Short Story

Imagine a class of students. The teacher might count how many students are present (COUNT), calculate the total marks (SUM), the average grade (AVG), the highest score (MAX), and the lowest score (MIN). Aggregate functions are like the teacherโ€™s summary reportโ€”they donโ€™t look at individual details but rather the big picture.

๐Ÿ”ง Technical Explanation

  • ๐Ÿ”ข COUNT() returns the number of rows.
  • โž• SUM() adds all numeric values.
  • โš–๏ธ AVG() returns the arithmetic mean.
  • ๐Ÿ“‰ MIN() gets the smallest value.
  • ๐Ÿ“ˆ MAX() gets the largest value.
  • ๐Ÿงฎ Often used with GROUP BY to calculate per-group aggregates.

๐ŸŽฏ Purpose & Use Case

  • โœ… Count total orders, users, or rows.
  • โœ… Calculate average salary, product price, or scores.
  • โœ… Find minimum or maximum values in a dataset.
  • โœ… Summarize grouped data (e.g., sales per region).

๐Ÿ’ป Real Code Example

-- Example Table: Sales
-- Columns: Id, Product, Quantity, Price

-- Total number of sales
SELECT COUNT(*) FROM Sales;

-- Total quantity sold
SELECT SUM(Quantity) FROM Sales;

-- Average price per product
SELECT AVG(Price) FROM Sales;

-- Minimum and maximum price
SELECT MIN(Price), MAX(Price) FROM Sales;

-- Aggregates per product
SELECT Product, SUM(Quantity) AS TotalQty, AVG(Price) AS AvgPrice
FROM Sales
GROUP BY Product;

โ“ Interview Q&A

Q1: What does COUNT(*) return?
A: It returns the number of rows in the result set, including NULLs.

Q2: What's the difference between COUNT(*) and COUNT(column)?
A: COUNT(*) counts all rows, while COUNT(column) ignores NULL values in that column.

Q3: Can you use aggregate functions in WHERE clause?
A: No, use HAVING for filtering aggregated data.

Q4: Whatโ€™s the purpose of GROUP BY with aggregates?
A: It groups rows based on column values and applies aggregate functions on each group.

Q5: What does SUM(Price) do?
A: It adds all the values in the Price column.

Q6: What is AVG used for?
A: It calculates the average value from a column.

Q7: When would you use MAX vs MIN?
A: MAX finds the highest value; MIN finds the lowest.

Q8: Can you use multiple aggregates in one SELECT?
A: Yes, such as SUM and AVG in the same query.

Q9: Are NULL values considered in AVG?
A: No, NULLs are ignored in AVG calculations.

Q10: Can aggregate functions be used in subqueries?
A: Yes, they are commonly used in subqueries for filters or joins.

๐Ÿ“ MCQs

Q1. What does COUNT(*) return?

  • Number of columns
  • Sum of values
  • Number of all rows
  • NULL count

Q2. Which function gives the average?

  • SUM()
  • MAX()
  • COUNT()
  • AVG()

Q3. Which aggregate returns the highest value?

  • MIN()
  • SUM()
  • MAX()
  • TOP()

Q4. What is the result of SUM(Quantity)?

  • Average quantity
  • Total of all quantity values
  • Number of products
  • Maximum quantity

Q5. Where do you filter aggregated results?

  • WHERE clause
  • HAVING clause
  • GROUP BY clause
  • ORDER BY clause

Q6. What is returned by COUNT(column) when NULLs are present?

  • All rows
  • NULL only
  • Count excluding NULLs
  • Zero

Q7. What does AVG ignore?

  • Decimals
  • Negative values
  • NULL values
  • Zero

Q8. Which clause groups data for aggregates?

  • WHERE
  • ORDER BY
  • GROUP BY
  • HAVING

Q9. Can you use COUNT and AVG together?

  • No
  • Only in joins
  • Yes
  • Only in subqueries

Q10. Which function returns the lowest number?

  • MAX()
  • MIN()
  • AVG()
  • ROUND()

๐Ÿ’ก Bonus Insight

Aggregate functions can be used in subqueries to rank, filter, or join datasets dynamically. When using GROUP BY, always ensure the SELECTed columns are either part of the grouping or wrapped in an aggregate function.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More on SQL Topics

SQL Joins Explained: INNER, LEFT, RIGHT, FULL ๐Ÿ‘‰ Explained
SQL Subqueries and Nested Queries Explained ๐Ÿ‘‰ Explained
SQL Aggregate Functions: COUNT, SUM, AVG, MIN, MAX ๐Ÿ‘‰ Explained
Understanding SQL GROUP BY and ORDER BY ๐Ÿ‘‰ Explained
Difference Between DELETE, TRUNCATE and DROP ๐Ÿ‘‰ Explained
SQL Views: Use Cases and Performance Tips ๐Ÿ‘‰ Explained
Common Table Expressions (CTE) in SQL ๐Ÿ‘‰ Explained
SQL Window Functions and OVER Clause ๐Ÿ‘‰ Explained
SQL Indexes: Clustered vs Non-Clustered ๐Ÿ‘‰ Explained
SQL Normalization and Denormalization ๐Ÿ‘‰ Explained
SQL Constraints: NOT NULL, UNIQUE, CHECK, DEFAULT ๐Ÿ‘‰ Explained
SQL Injection and How to Prevent It ๐Ÿ‘‰ Explained
Primary Key vs Foreign Key in SQL ๐Ÿ‘‰ Explained
Data Types in SQL: INT, VARCHAR, DATE and more ๐Ÿ‘‰ Explained
How to Use CASE Statement in SQL ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup