Aggregation Functions

 Aggregation functions in MySQL are used to perform calculations on multiple values and return a single value. These functions are commonly used with the GROUP BY clause. Here are some of the most commonly used aggregation functions in MySQL:

1. COUNT()
Counts the number of rows that match a specified condition.

SELECT COUNT(*)
FROM students;

2. SUM()
Calculates the sum of a set of values.

SELECT SUM(score)
FROM scores;

3. AVG()
Calculates the average value of a set of values.

SELECT AVG(score)
FROM scores;

4. MIN()
Finds the minimum value in a set of values.

SELECT MIN(score)
FROM scores;

5. MAX()
Finds the maximum value in a set of values.

SELECT MAX(score)
FROM scores;

6. GROUP_CONCAT()
Concatenates values from multiple rows into a single string.

SELECT GROUP_CONCAT(course_name)
FROM courses;


7.VARIANCE()
Returns the variance of a numeric column.
SELECT VARIANCE(salary) FROM employees;


8.STDDEV()
Returns the standard deviation of a numeric column.
SELECT STDDEV(salary) FROM employees;


9.BIT_AND()
Returns the bitwise AND of all bits in a column.
SELECT BIT_AND(some_bitwise_column) FROM employees;

10.BIT_OR()
Returns the bitwise OR of all bits in a column.
SELECT BIT_OR(some_bitwise_column) FROM employees;

11.JSON_ARRAYAGG()
Aggregates values as a JSON array.
SELECT JSON_ARRAYAGG(employee_name) FROM employees;

12.JSON_OBJECTAGG()
Aggregates key-value pairs as a JSON object.
SELECT JSON_OBJECTAGG(employee_id, employee_name) FROM employees;

These functions are typically used in combination with the GROUP BY clause to aggregate data across different groups within a table. 

Example:
We'll have two tables: departments and employees.

Departments Table

CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);
Employees Table

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(100),
    salary DECIMAL(10, 2),
    department_id INT,
    join_date DATE,
    FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

Sample Data
Inserting Data into Departments Table

INSERT INTO departments (department_id, department_name) VALUES
(1, 'HR'),
(2, 'Engineering'),
(3, 'Marketing');

Inserting Data into Employees Table
INSERT INTO employees (employee_id, employee_name, salary, department_id, join_date) VALUES
(1, 'Alice', 60000, 2, '2020-01-15'),
(2, 'Bob', 50000, 1, '2019-03-20'),
(3, 'Charlie', 70000, 2, '2021-07-30'),
(4, 'David', 55000, 3, '2018-11-12'),
(5, 'Eve', 65000, 2, '2020-05-01');

Aggregation Queries

Total number of employees:
SELECT COUNT(*) AS total_employees FROM employees;

Total salary paid to employees:
SELECT SUM(salary) AS total_salary FROM employees;

Average salary per department:
SELECT department_id, AVG(salary) AS average_salary 
FROM employees 
GROUP BY department_id;

Minimum and maximum salary in the company:
SELECT MIN(salary) AS min_salary, MAX(salary) AS max_salary 
FROM employees;

Concatenated list of employee names per department:
SELECT department_id, GROUP_CONCAT(employee_name) AS employees 
FROM employees 
GROUP BY department_id;

Variance and standard deviation of salaries:SELECT VARIANCE(salary) AS salary_variance, STDDEV(salary) AS salary_stddev 
FROM employees;

Bitwise AND and OR of employee IDs:SELECT BIT_AND(employee_id) AS bit_and_result, BIT_OR(employee_id) AS bit_or_result 
FROM employees;

Aggregated employee names as JSON array per department:
SELECT department_id, JSON_ARRAYAGG(employee_name) AS employee_names 
FROM employees 
GROUP BY department_id;

Aggregated key-value pairs of employee IDs and names as JSON object:
SELECT JSON_OBJECTAGG(employee_id, employee_name) AS employee_id_name_map 
FROM employees;

Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

Procedures in PL/SQL

Cursors in PL/SQL