MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The GROUP BY
statement in MySQL is used in collaboration with the SELECT
statement to arrange identical data into groups. The GROUP BY
statement follows the WHERE
clause in a SELECT
statement and precedes the ORDER BY
clause.
Here's a basic usage of the GROUP BY
statement:
SELECT column1, column2, ... FROM table_name WHERE condition GROUP BY column1, column2, ... ORDER BY column1, column2, ... ;
For example, suppose you have a table called orders
with the columns order_id
, customer_id
, and order_amount
. If you want to find the total order amount per customer, you could use the GROUP BY
statement along with the SUM()
function like this:
SELECT customer_id, SUM(order_amount) FROM orders GROUP BY customer_id;
This query will return the total order_amount
for each customer_id
.
You can also use GROUP BY
with multiple columns. For example, if you want to find the total order amount for each combination of customer_id
and order_id
, you can modify the query like this:
SELECT customer_id, order_id, SUM(order_amount) FROM orders GROUP BY customer_id, order_id;
This query will return the total order_amount
for each combination of customer_id
and order_id
.
Note: When you use GROUP BY
, the SELECT
statement can only include columns that you're grouping by or aggregate functions like COUNT()
, SUM()
, AVG()
, MIN()
, or MAX()
. Trying to select columns that are not part of the GROUP BY
clause without applying an aggregate function will return an error.
It's also important to note that NULL values will be grouped together as one group in the result set of a GROUP BY
query.
MySQL GROUP BY statement example:
GROUP BY
statement is used to group rows that have the same values in specified columns into summary rows.SELECT column1, column2, COUNT(*) FROM your_table GROUP BY column1, column2;
How to use GROUP BY in MySQL:
GROUP BY
clause to group rows based on one or more columns in a MySQL table.SELECT department, AVG(salary) FROM employees GROUP BY department;
Grouping data with GROUP BY in MySQL:
GROUP BY
groups rows with identical values in specified columns, facilitating aggregate analysis.SELECT product_category, MAX(product_price) FROM products GROUP BY product_category;
Aggregate functions and GROUP BY in MySQL:
GROUP BY
for summarized results.SELECT order_date, COUNT(*) AS OrderCount FROM orders GROUP BY order_date;
Grouping by multiple columns in MySQL:
SELECT department, job_title, AVG(salary) FROM employees GROUP BY department, job_title;
HAVING clause with GROUP BY in MySQL:
HAVING
clause with GROUP BY
to filter results based on aggregated conditions.SELECT product_category, AVG(product_price) AS AvgPrice FROM products GROUP BY product_category HAVING AvgPrice > 50;
Grouping and counting with GROUP BY in MySQL:
GROUP BY
to group rows and COUNT
to count the number of rows in each group.SELECT department, COUNT(*) AS EmployeeCount FROM employees GROUP BY department;
Examples of using GROUP BY in MySQL queries:
GROUP BY
statement in MySQL queries.SELECT department, AVG(salary) AS AvgSalary FROM employees GROUP BY department; SELECT country, city, COUNT(*) AS CustomerCount FROM customers GROUP BY country, city;