Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
How to find distinct values of multiple columns in PySpark ?
Next article icon

How to SELECT DISTINCT on Multiple Columns in SQL?

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In the world of databases, data duplication can lead to confusion and inefficiency. SQL provides a powerful tool, SELECT DISTINCT, to retrieve unique values from columns. However, when dealing with multiple columns, the approach becomes more detailed.

In this article, we will explain how to use SELECT DISTINCT on multiple columns in SQL by understanding various methods along with practical implementations. By the end, we will have a clear understanding of how to effectively retrieve unique combinations of values from multiple columns.

SELECT DISTINCT on Multiple Columns in SQL

When working with SQL databases, it's common to encounter scenarios where we need to retrieve unique combinations of values from multiple columns. This is where the SELECT DISTINCT statement becomes invaluable. Querying distinct values helps in:

  • Avoiding data duplication in results.
  • Improving report accuracy by presenting unique data.
  • Simplifying analysis by eliminating redundant information.

Below, we explore the syntax and multiple methods for using SELECT DISTINCT on multiple columns in SQL.

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

table--gfg
Table - geeksforgeeks

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

without-where-clause
SELECT DISTINCT without WHERE Clause

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

with-where-clause
SELECT DISTINCT with WHERE clause

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

order-by-desc
SELECT DISTINCT with ORDER BY Clause

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

groupby
SELECT DISTINCT - GROUP BY & COUNT()

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.


Next Article
How to find distinct values of multiple columns in PySpark ?

V

vishuvaishnav3001
Improve
Article Tags :
  • SQL
  • Databases
  • SQL-Query

Similar Reads

  • How to SELECT DISTINCT on Multiple Columns in SQLite?
    SQLite is a lightweight and server-less relational database management system (R.D.B.M.S). It is a self-contained database and requires very minimal configuration. It is a server-less architecture that is good for mobile applications and simple desktop applications. In this article, we are going to
    4 min read
  • How to SELECT DISTINCT on Multiple Columns in SQL Server?
    When working with SQL Server, there are scenarios where we might need to retrieve unique combinations of values from multiple columns. This is where the SELECT DISTINCT statement comes in handy. It allows us to eliminate duplicate rows from the result set. However, using SELECT DISTINCT it on multip
    4 min read
  • Selecting Multiple Columns Based On Condition in SQL
    SQL (Structured Query Language) is used to manage and query databases. One common requirement when querying data is selecting multiple columns based on specific conditions. Understanding how to use SQL for this purpose can enhance your ability to retrieve relevant data efficiently. In this article,
    4 min read
  • How to Find the Maximum of Multiple Columns in SQL Server?
    When working with SQL Server databases, there are times when we need to find the maximum value among multiple columns. This task can be accomplished using various techniques within SQL queries. By using functions like CASE and GREATEST, SQL Server provides efficient ways to determine the maximum val
    4 min read
  • How to find distinct values of multiple columns in PySpark ?
    In this article, we will discuss how to find distinct values of multiple columns in PySpark dataframe. Let's create a sample dataframe for demonstration: [GFGTABS] Python3 # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creati
    2 min read
  • How to Find the Maximum of Multiple Columns in PL/SQL?
    In PL/SQL finding the maximum value of multiple columns is a common requirement for maintaining the database. This operation is important for various applications, from financial analysis to data reporting. In this article, we will learn How to find the maximum of multiple columns in PL/SQL with the
    5 min read
  • How to Find the Maximum of Multiple Columns in SQL
    Finding the maximum value of multiple columns is one of the most common analytical tasks essential for making decisions and analyzing data. Using the MAX() function of SQL, users can find the maximum value in a single column. But to find the maximum value in multiple columns, users need to use other
    2 min read
  • How to Find the Maximum of Multiple Columns in SQLite?
    SQLite is a serverless architecture that does not require any server to perform operations and queries. It is widely used in embedded systems, mobile applications, and small-scale web applications because of its simplicity, efficiency, and portability. SQLite supports most of the standard SQL featur
    4 min read
  • How to Select Individual Columns in SQL?
    In SQL, sometimes we require to select individual columns from a table. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Select is the most commonly used statement in SQL. The S
    2 min read
  • How to Find the Maximum of Multiple Columns in PostgreSQL?
    PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under PostgreSQL license, a liberal open-source license. Anyone with the right skills is free to use, modify, and distr
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences