๐ Aggregate Functions (COUNT, SUM, AVG, MIN, MAX)
Aggregate functions are used to perform calculations on multiple rows of a table and return a single value. They're mostly used with GROUP BY, but also work standalone.
1. COUNT()
Returns the number of rows.
Example:
SELECT COUNT(*) FROM employees;
Counts all employees in the table.
You can also count only non-null values in a column:
SELECT COUNT(email) FROM customers;
2. SUM()
Adds up all the values in a numeric column.
Example:
SELECT SUM(salary) FROM employees;
Gives you the total salary payout.
3. AVG()
Calculates the average value of a numeric column.
Example:
SELECT AVG(price) FROM products;
Finds the average product price.
4. MIN()
Returns the lowest value.
Example:
SELECT MIN(salary) FROM employees;
Finds the smallest salary.
5. MAX()
Returns the highest value.
Example:
SELECT MAX(salary) FROM employees;
Finds the highest salary in the table.
Bonus Example:
SELECT
COUNT(*) AS total_orders,
SUM(amount) AS total_revenue,
AVG(amount) AS avg_order_value
FROM orders;
This gives you a quick business summary: number of orders, total revenue, and average order value.
React with โค๏ธ for more.
Share with credits: https://t.me/sqlspecialist
Hope it helps :)
Post #2136
3.94K
- โค 10
- ๐ 1