
- 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
Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause. Consider the following example in which we have used DISTINCT clause in first query and GROUP BY clause in the second query, on ‘fname’ and ‘Lname’ columns of the table named ‘testing’.
mysql> Select * from testing; +------+---------+---------+ | id | fname | Lname | +------+---------+---------+ | 200 | Raman | Kumar | | 201 | Sahil | Bhalla | | 202 | Gaurav | NULL | | 203 | Aarav | NULL | | 204 | Harshit | Khurana | | 205 | Rahul | NULL | | 206 | Piyush | Kohli | | 207 | Lovkesh | NULL | | 208 | Gaurav | Kumar | | 209 | Raman | Kumar | +------+---------+---------+ 10 rows in set (0.00 sec) mysql> Select DISTINCT FNAME,LNAME from testing; +---------+---------+ | FNAME | LNAME | +---------+---------+ | Raman | Kumar | | Sahil | Bhalla | | Gaurav | NULL | | Aarav | NULL | | Harshit | Khurana | | Rahul | NULL | | Piyush | Kohli | | Lovkesh | NULL | | Gaurav | Kumar | +---------+---------+ 9 rows in set (0.00 sec) mysql> Select Fname, LNAME from testing GROUP BY Fname,Lname; +---------+---------+ | Fname | LNAME | +---------+---------+ | Aarav | NULL | | Gaurav | NULL | | Gaurav | Kumar | | Harshit | Khurana | | Lovkesh | NULL | | Piyush | Kohli | | Rahul | NULL | | Raman | Kumar | | Sahil | Bhalla | +---------+---------+ 9 rows in set (0.00 sec)
The only difference is that the result set returns by MySQL query using GROUP BY clause is sorted and in contrast, the result set return by MySQL query using DISTICT clause is not sorted.
- Related Articles
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- How to use MySQL DISTINCT clause on multiple columns?
- How can we use two columns with MySQL WHERE clause?
- How can we create a MySQL view with GROUP BY clause?
- What is the significance of using multiple columns in MySQL GROUP BY clause?
- Can we fetch multiple values with MySQL WHERE Clause?
- What MySQL returns when we use DISTINCT clause with the column having multiple NULL values?
- How can we use ASCII() function with MySQL WHERE clause?
- How can we use CHAR_LENGTH() function with MySQL WHERE clause?
- How can we use BIN() function with MySQL WHERE clause?
- How can we use FIND_IN_SET() function with MySQL WHERE clause?
- How can we use MySQL INSTR() function with WHERE clause?
- How can we use MySQL SUM() function with HAVING clause?
- How can we use a MySQL subquery with FROM clause?

Advertisements