Question №15
Remaining:
What are aggregate functions? Provide examples
Sample Answer
Show Answer by Default
Aggregate functions perform calculations on a set of values and return a single value. They are often used in combination with the GROUP BY clause.
Key Aggregate Functions
- COUNT() — counts the number of rows.
- SUM() — calculates the sum of values.
- AVG() — calculates the average value.
- MAX() — finds the maximum value.
- MIN() — finds the minimum value.
Examples of usage
- Counting the number of employees:
MySQL 8.1SELECT COUNT(*) FROM employees;
- Average salary by department:
MySQL 8.1SELECT department_id, AVG(salary) AS average_salary FROM employees GROUP BY department_id;
- Maximum salary in the company:
MySQL 8.1SELECT MAX(salary) FROM employees;
- Total sales for the month:
MySQL 8.1SELECT SUM(amount) FROM sales WHERE sale_date BETWEEN '2023-01-01' AND '2023-01-31';