We've been getting a number of this type of question lately: short, and simple on the surface. Short questions...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
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.
Related Q&A from Rudy Limeback
Read SQL expert Rudy Limeback's advice for counting combinations in a table with SQL's GROUP BY clausecontinue reading
Read an example of an SQL case expression from our SQL expert Rudy Limeback.continue reading
Read about the Mimer Validator, a tool used to verify your SQL code, in this tip from SQL expert Rudy Limeback.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.