EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL SELECT DISTINCT Multiple Columns
 

SQL SELECT DISTINCT Multiple Columns

Updated March 16, 2023

SQL SELECT DISTINCT Multiple Columns

 

 

Introduction to SQL SELECT DISTINCT Multiple Columns

Sql select distinct multiple columns are used to retrieve specific records from multiple columns on which we have used distinct clauses. We can also add multiple table columns with sql select distinct clause, as we know that sql select distinct eliminates rows where all the fields are identical, which we have selected. By using it, we can filter the data from multiple columns.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

SQL select distinct on multiple columns is more useful in an RDBMS system to fetch unique records from various columns in a single table. We can use SQL to select distinct keywords on multiple columns from the specified table defined in the query. It will remove duplicate records from the column. It will work on various columns to find unique records. It retrieves the count of all unique records from the multiple columns. Therefore, it will eliminate all duplicate records.

How to Use SQL SELECT DISTINCT Multiple Columns?

SQL select distinct will work on multiple columns; we can use the same on single and multiple columns on the table from which we are retrieving the unique records.

Below is the syntax of sql select distinct multiple column statements as follows:

Syntax:

Select DISTINCT name_of_column1, name_of_column2, …., name_of_columnN
From name_of_table;
Select DISTINCT name_of_column1, name_of_column2, …., name_of_columnN
From name_of_table where condition;
Select DISTINCT name_of_column1, name_of_column2, …., name_of_columnN from name_of_table where condition order by name_of_column;

Below is the description syntax of SQL select distinct multiple columns statement:

  • Select: Using select, we can select the data as per the condition given in the query. For example, we can choose a distinct statement to retrieve unique records from the table.
  • Name of the table: This is nothing but the table’s name from which we have retrieved the unique records from multiple columns.
  • Name of a column: This is the column name used with a distinct keyword to retrieve data from multiple columns.
  • Order by: This condition is used with sql select distinct multiple columns statement to fetch the records as per column, which we have used with an order by condition.
  • Where condition: The where condition in any statement of SQL will be used to select or retrieve a specified row we defined in the where condition.

For defining how to use SQL select distinct multiple columns, we are using the orders table. In addition, we are using the Postgres database to execute queries that define how we are using it.

Code:

 Select * from orders;

Output:

SQL SELECT DISTINCT Multiple Columns 1

In the below query, we use two columns with sql select distinct clause. After using two columns, we can see the output retrieving the unique values from both columns.

SQL SELECT DISTINCT Multiple Columns 2

SQL SELECT DISTINCT Multiple Columns 3

Below are the relational algebra expressions of the above query.

SQL SELECT DISTINCT Multiple Columns 4

Below is the relational algebra tree of the above query.

SQL SELECT DISTINCT Multiple Columns 5

Below is the pictorial representation of the above output.

SQL SELECT DISTINCT Multiple Columns 6

In the below query, we are retrieving data from three columns. After using a distinct clause on three columns, it will retrieve the unique values from both the rows.

SQL SELECT DISTINCT Multiple Columns 7

we are retrieving data from three columns

Below are the relational algebra expressions of the above query.

SQL SELECT DISTINCT Multiple Columns 9

Below is the relational algebra tree of the above query.

relational algebra tree

Below is the pictorial representation of the above output.

pictorial representation

In the below query, we are retrieving data from all columns. After using a distinct clause on all columns will retrieve the unique values from all the columns.

SQL SELECT DISTINCT Multiple Columns 12

SQL SELECT DISTINCT Multiple Columns 13

Below are the relational algebra expressions of the above query.

SQL SELECT DISTINCT Multiple Columns 14

Below is the relational algebra tree of the above query.

relational algebra tree

Below is the pictorial representation of the above output.

SQL SELECT DISTINCT Multiple Columns 16JPG

In the below query, we retrieve data from two columns in order by clause.

SQL SELECT DISTINCT Multiple Columns 17JPG

SQL SELECT DISTINCT Multiple Columns 18JPG

Below are the relational algebra expressions of the above query.

SQL SELECT DISTINCT Multiple Columns 19JPG

Below is the relational algebra tree of the above query.

SQL SELECT DISTINCT Multiple Columns 20JPG.

Below is the pictorial representation of the above output.

SQL SELECT DISTINCT Multiple Columns 21JPG.

Examples of SQL SELECT DISTINCT Multiple Columns

Different examples are mentioned below:

We are using distinct_multiple tables to define examples.

Code:

Select * from distinct_multiple;

Output:

distinct_multiple table

Example #1

In the below example, we retrieve the count of unique records from multiple columns by using distinct clauses.

Code:

Select count(distinct id), count(distinct name), count(distinct city), count(distinct phone) from distinct_multiple;

Output:

retrieving count of unique records

Example #2

The below example shows the distinct count of all columns by using it with where conditions are as follows.

Code:

Select count(distinct id), count(distinct name), count(distinct city), count(distinct phone) from distinct_multiple where id = 101;

Output:

SQL SELECT DISTINCT Multiple Columns 24JPG.

Example #3

In the below example, we retrieve unique records from all the table columns by using order by condition as follows. We are using order by condition on the id column as follows.

Code:

Select distinct id, name, city, phone from distinct_multiple order by id;

Output:

retrieving unique records from all the table columns

Example #4

In the example below, we use where condition and order by clause in the same query. In addition, we use where condition on the name column and order by condition on the id column. Also, we are using all the columns with distinct clauses.

Code:

Select distinct id, name, city, phone from distinct_multiple where name = ‘ABC’ order by id;

Output:

where condition and order by clause in same query

Example #5

In the below example, we retrieve data from all columns with where condition. We use the id, name, city, and phone column to retrieve the data. After using a distinct clause on all columns with the where condition, it will retrieve the unique values from the rows we defined in the where condition.

Code:

Select distinct id, name, city, phone from distinct_multiple where id = 102;

Output:

we are retrieving data from all columns with where condition

Conclusion

Selecting distinct counts on multiple columns retrieves all unique records from the multiple columns. It will eliminate all duplicate records. It retrieves distinct records from multiple columns on which we have used distinct clauses.

Recommended Articles

This is a guide to SQL SELECT DISTINCT Multiple Columns. Here we discuss the introduction, how to use and examples, respectively. You may also have a look at the following articles to learn more –

  1. SQL ORDER BY DESC
  2. SQL EXECUTE
  3. SQL EXCLUDE
  4. MySQL InnoDB Cluster

 

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW