
- 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 to use MySQL DISTINCT clause on multiple columns?
We can use the DISTINCT clause on more than columns in MySQL. In this case, the uniqueness of rows in the result set would depend on the combination of all columns.
Example
Consider the following table ‘testing’ having 10 rows −
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)
Now if we apply the DISTINCT clause on two columns named Fname and Lname then we would get the unique rows based on the combinations of both the columns. It can be observed from the following query −
mysql> Select DISTINCT FNAME,LNAME from testing2; +---------+---------+ | 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 returns 9 rows as a result set because they are having unique combinations of values from ‘Fname’ and ‘Lname’ columns.
- Related Articles
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- MySQL Select Statement DISTINCT for Multiple Columns?
- What MySQL returns when we use DISTINCT clause with the column having multiple NULL values?
- How can we use two columns with MySQL WHERE clause?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”
- What is the significance of using multiple columns in MySQL GROUP BY clause?
- How to search multiple columns in MySQL?
- How to use MySQL VIEW with WHERE clause?
- How to use the ‘except’ clause with multiple exceptions in Python?
- How to order MySQL rows by multiple columns?
- How to use MySQL Date functions with WHERE clause?
- How to use user variables in MySQL LIKE clause?
- How to update multiple rows using single WHERE clause in MySQL?

Advertisements