Question15
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

  1. Counting the number of employees:
MySQL 8.1
SELECT COUNT(*) FROM employees;
  1. Average salary by department:
MySQL 8.1
SELECT department_id, AVG(salary) AS average_salary
FROM employees
GROUP BY department_id;
  1. Maximum salary in the company:
MySQL 8.1
SELECT MAX(salary) FROM employees;
  1. Total sales for the month:
MySQL 8.1
SELECT SUM(amount) FROM sales WHERE sale_date BETWEEN '2023-01-01' AND '2023-01-31';