
- 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 use group functions with non-group fields in MySQL SELECT query?
We have to use GROUP BY clause if we want to use group functions with non-group fields in the SELECT query. The general syntax can be as follows
Syntax
SELECT group_function1,…, non-group-column1,… from table_name GROUP BY column_name;
Example
mysql> Select COUNT(*), id from Student GROUP BY id; +----------+------+ | COUNT(*) | id | +----------+------+ | 1 | 1 | | 1 | 2 | | 1 | 15 | | 1 | 17 | | 1 | 20 | +----------+------+ 5 rows in set (0.00 sec) mysql> Select COUNT(*), address from Student GROUP BY id; +----------+---------+ | COUNT(*) | address | +----------+---------+ | 1 | Delhi | | 1 | Mumbai | | 1 | Delhi | | 1 | Shimla | | 1 | Jaipur | +----------+---------+ 5 rows in set (0.00 sec)
The field after GROUP BY clause can be different from non-group field given in SELECT query.
- Related Articles
- Why should we not use group functions with non-group fields without GROUP BY clause in MySQL SELECT query?
- Can we GROUP BY one column and select all data in MySQL?
- Can we use SELECT NULL statement in a MySQL query?
- Why do we use Group, Ring and Fields in Information Security?
- How can we create a MySQL view with GROUP BY clause?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- What are MySQL group functions?
- MySQL syntax error (in SELECT query) while using ‘group’ as table name
- MongoDB query to group several fields using aggregation framework?
- SELECT DISTINCT vs GROUP BY in MySQL?
- How can I order in group but randomly with MySQL?
- Group all documents with common fields in MongoDB?
- How can we use MySQL SELECT without FROM clause?
- How we can group data in HTML forms?
- How can group functions be used in ORDER BY clause?

Advertisements