
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we divide the result set returned by MySQL into groups?
It can be done by using GROUP BY clause in the SELECT statement. We can specify a column as grouping criteria with the help of GROUP BY clause. Due to the specified grouping criteria, rows with the same value in a particular column are considered as a single group. In this way, the result set returned by MySQL SELECT statement will be divided into groups.
Example
Following is a good example to understand it −
We have a table named ‘employees’ as follows −
mysql> Select * from employees; +------+-------------+--------+------------+ | id | designation | Salary | DoJ | +------+-------------+--------+------------+ | 100 | Asst.Prof | 50000 | 2016-06-15 | | 300 | Prof | 85000 | 2010-05-18 | | 250 | Asso.Prof | 74000 | 2013-02-12 | | 400 | Prof | 90000 | 2009-05-19 | | 200 | Asst.Prof | 60000 | 2015-05-11 | +------+-------------+--------+------------+ 5 rows in set (0.00 sec)
Now with the help of following script we will divide the output into groups;
mysql> select designation, count(*), AVG(salary) from employees group by designation; +-------------+----------+-------------+ | designation | count(*) | AVG(salary) | +-------------+----------+-------------+ | Asso.Prof | 1 | 74000.0000 | | Asst.Prof | 2 | 55000.0000 | | Prof | 2 | 87500.0000 | +-------------+----------+-------------+ 3 rows in set (0.00 sec)
Above query returns the result set into groups of Asso.Prof, total 1 in number and having average salary 74000, Asst.Prof, total 2 in number and having an average salary of 55000 and Prof, total 2 in number and having average salary 87500.
- Related Articles
- How can we apply filtering criteria at group levels of the result set returned by MySQL?
- How can we get all the unique rows in MySQL result set?
- How can we handle a result set inside MySQL stored procedure?
- In MySQL, how can we randomize set of rows or values in the result set?
- How can we set up a MySQL User account by using INSERT INTO statement?
- How can we get the summary output of a column in MySQL result set itself?
- How Can MySQL operator precedence affect result set?
- How can we get only unique values of a column in MySQL result set?
- How can we specify the number of records to be returned in MySQL output?
- How can we upload data into MySQL tables by using mysqlimport?
- Can we replace a number with a String in a MySQL result set?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How can we change MySQL user password by using the SET PASSWORD statement?
- How can we upload data into multiple MySQL tables by using mysqlimport?
- How can we import CSV files into MySQL tables by using mysqlimport?
