How to SELECT DISTINCT on Multiple Columns in SQL?
Last Updated :
20 Jun, 2025
Data duplication can lead to confusion, inefficiency and errors in reporting or analysis. One of the most useful SQL commands to avoid this issue is SELECT DISTINCT. It is used to
retrieve unique values from a column or combination of columns. It ensures that your query results are clean with no repeating entries.
While SELECT DISTINCT
is commonly used with a single column, its application on multiple columns requires a slightly more detailed understanding. When working with SQL databases, it is common to face situation where you need to retrieve unique combinations of values across multiple columns. The SELECT DISTINCT is especially valuable in such cases. It helps in
- Avoiding data duplication in results.
- Improving report accuracy by presenting unique data.
- Simplifying analysis by eliminating redundant information.
Syntax:
SELECT DISTINCT column01, column02, ............
FROM table_name
WHERE (specify the condition if required ) ;
Creating a Demo Table in our Database
To understand How to SELECT DISTINCT on multiple columns in SQL we need a table on which we will perform various operations and queries. Here we will consider a table called geeksforgeeks which contains id, name, score, and course as Columns. Here is the SQL query to create the table:
CREATE TABLE geeksforgeeks (
id INT,
name VARCHAR(50),
score INT,
course VARCHAR(50)
);
We can populate the table with sample data as follows:
INSERT INTO geeksforgeeks (id, name, score, course) VALUES
(1, 'Vishu', 150, 'Python'),
(2, 'Sumit', 100, 'Java'),
(3, 'Neeraj', 150, 'Python'),
(4, 'Aayush', 100, 'Java'),
(5, 'Vivek', 50, 'Javascript');
Output
1. SELECT DISTINCT without WHERE Clause
In this example, we are going to implement SELECT DISTINCT statement for multiple values but without using WHERE clause. We will explore each and every data of the table.
Query:
SELECT DISTINCT score, course
from geeksforgeeks ;
Output
Explanation:
The query eliminates duplicate rows based on the selected columns (score
and course
). For example, the combination (150, Python)
appears twice in the original data but only once in the result.
2. SELECT DISTINCT with WHERE Clause
In this method, we are going to perform similar kind of operation as we have done in 'method 1' but this time we will work with some specified data. We will use WHERE clause along with the SELECT DISTINCT statement.
Query:
SELECT DISTINCT score, course
from geeksforgeeks
WHERE course IN ('Java','JavaScript');
Output
Explanation:
In the above image, we can clearly notice that all values are unique. This is similar kind of operation we have performed in 'method 1'. This query retrieves distinct combinations of score
and course
but only for rows where course
is either 'Java' or 'JavaScript'.
3. SELECT DISTINCT with ORDER BY Clause
In this example, we are going to display all the distinct data from multiple columns of our table in descending order. We will use ORDER BY Clause along with DESC keyword to achieve this task.
Query:
SELECT DISTINCT score, course
FROM geeksforgeeks
ORDER BY score DESC;
Output
Explanation:
The query retrieves unique combinations and sorts them in descending order based on score
. The result maintains uniqueness while ensuring an organized presentation of data.
4. SELECT DISTINCT with COUNT() and GROUP BY Clause
In the above example, we will count distinct values considering two of the columns of the table. We will use GROUP BY clause and COUNT() function.
Query:
SELECT course,count(DISTINCT CONCAT(score, course)) as count_score_course
from geeksforgeeks
GROUP by course ;
Output
Explanation:
This query calculates the number of unique combinations of score
and course
for each course
. The CONCAT()
function is used to create a combined string for counting unique entries.
Conclusion
The SELECT DISTINCT
statement in SQL is an essential tool for retrieving unique combinations of values from multiple columns. It simplifies data queries, removes redundancy, and makes the results cleaner and more meaningful. By understanding the various approaches and strategies outlined in this article, we can effectively use SELECT DISTINCT on multiple columns in SQL to streamline our data querying processes and eliminate duplicate data.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read