Q

Distinct values of more than one column

How do I see distinct values of more than one column in a single SQL command?

How do I see distinct values of more than one column in a single SQL command?

We've been getting a number of this type of question lately: short, and simple on the surface. Short questions are wonderful, because they leave more room for the answer. Simple questions are even better, as long as they aren't too simple.

In this case, there are two ways to do it.

select distinct 
       col1
     , col2 
  from daTable

This method uses the standard SQL keyword DISTINCT. All distinct combinations of values in two columns are shown. (It could be two, or 10, or even just one column.) The "distinctness" applies to the entire row produced by the query. Every result row is completely distinct.

select col1
     , col2 
  from daTable
group
    by col1
     , col2

This method uses the standard SQL keywords GROUP BY. All distinct combinations of values in two columns are shown. (It could be two, or 10, or even just one column.) The "distinctness" applies to the columns in the GROUP BY. Every result row will be completely distinct in those two columns.

If you wanted distinct rows by those two columns, but with additional columns as well, then the question was indeed not as simple as it first appeared. In that case, perhaps the answer DISTINCT is not a function (26 September 2005) may help.

This was first published in April 2007

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close