Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Up to this point, our queries have operated on individual rows (scalar operations). However, business intelligence and executive reporting require summarizing data across thousands or millions of rows: calculating total quarterly sales, finding the maximum salary, or determining average product ratings. These calculations are performed using Aggregate Functions.
| Function | Purpose | Input Types | Treats NULL as... |
|---|---|---|---|
COUNT() | Counts the number of rows or non-null values. | Any | COUNT(*) counts all rows; COUNT(col) ignores NULL. |
SUM() | Calculates the total sum of values. | Numeric | Ignored (Skipped). |
AVG() | Calculates the arithmetic mean. | Numeric | Ignored (Skipped). |
MIN() | Finds the minimum value. | Numeric, Date, String | Ignored (Skipped). |
MAX() | Finds the maximum value. | Numeric, Date, String | Ignored (Skipped). |
COUNT(*) vs COUNT(column) DistinctionOne of the most critical concepts in SQL is the difference between COUNT(*) and COUNT(col):
AVG() Handles NULL ValuesBecause aggregate functions skip NULL rows, AVG() computes the mean divided only by the count of non-null rows:
If your business requirement mandates treating missing commissions as ₹0.00:
DISTINCTYou can combine DISTINCT inside an aggregate function:
ONLY_FULL_GROUP_BY), running SELECT department, AVG(salary) FROM employees; triggers an error! You cannot ask for a single average salary alongside multiple department names without a GROUP BY clause.MIN and MAX on Dates and Strings: MIN() and MAX() work seamlessly on dates (finding oldest/newest) and alphabetical strings ('Aarav' to 'Zoya').COUNT(*) count in a relational database table?SUM() and AVG() when they encounter NULL values in a numeric column?rating = NULL, what does SELECT COUNT(rating) FROM table; return?customers table?MIN(order_date) return when executed against an orders table?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Control Flow Functions (IF, IFNULL, COALESCE) | Grouping Data with GROUP BY |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.