MariaDB Aggregation Query
MariaDB is an open source relational Database management system, a branch of MySQL. It supports multiple aggregation queries, including but not limited to the following:
1. COUNT: Used to return the number of non NULL values in the specified column.
For example, given a students table containing three fields: id, name, and age, the following query statement can be used to calculate the total number of students:
sql
SELECT COUNT(*) AS total_students FROM students;
2. SUM: Used to calculate the sum of specified columns.
For example, given a sales table containing three fields: id, product, and amount, the following query statement can be used to calculate the total sales amount:
sql
SELECT SUM(amount) AS total_sales FROM sales;
3. AVG: Used to calculate the average value of a specified column.
For example, given a scores table containing two fields: id and score, the average score can be calculated using the following query statement:
sql
SELECT AVG(score) AS average_score FROM scores;
4. MAX: Used to find the maximum value of a specified column.
For example, given a products table containing three fields: id, name, and price, the following query statement can be used to find the product with the highest price:
sql
SELECT MAX(price) AS highest_price FROM products;
5. MIN: Used to find the minimum value of a specified column.
For example, given an employees table containing three fields: id, name, and salary, the following query statement can be used to find the lowest paying employee:
sql
SELECT MIN(salary) AS lowest_salary FROM employees;
6. GROUP BY: Used to group results by specified columns.
For example, given an orders table containing three fields: id, customer, and amount, the following query statement can be used to group customers and calculate the total order amount for each customer:
sql
SELECT customer, SUM(amount) AS total_amount FROM orders GROUP BY customer;
7. HAVING: Used to further filter the grouped results in the GROUP BY clause.
For example, given an orders table containing three fields: id, customer, and amount, the following query statement can be used to identify customers with a total order amount exceeding 100:
sql
SELECT customer, SUM(amount) AS total_amount FROM orders GROUP BY customer HAVING total_amount > 100;
The above are only some common examples of aggregation queries, which can be adjusted according to specific business needs during actual use. The table structure and sample data can be defined and inserted according to the actual situation.