views:

72

answers:

2

If I have a table such as

1 bob
1 ray
1 bob
1 ray
2 joe
2 joe

And I want to select distinct based on the two columns so that I would get

1 bob
1 ray
2 joe

How can I word my query? Is the only way to concatenate the columns and wrap them around a distinct function operator?

+10  A: 
select distinct id, name from [table]

or

select id, name from [table] group by id, name
Denis Valeev
Leave some for us, eh?
OMG Ponies
Funny! It's been hours since my last accepted answer! Har-har...
Denis Valeev
@OMG Ponies, how come 47k is not enough?
Denis Valeev
The number is arbitrary - I could ask you why 100 is not enough. Me, I've learnt a fair bit while sharpening skills. The SO format works better than traditional forums.
OMG Ponies
@Denis and @OMG Ponies: "You can never get enough of what you don't need to make you happy." ~Eric Hoffer :-)
Joe Stefanelli
+1  A: 

You can just do:

select distinct col1, col2 from your_table;

That's exactly what the distinct operator is for: removing duplicate result rows.

Keep in mind that distinct is usually a pretty expensive operation, since, after processing the query, the DB server might perform a sort operation in order to remove the duplicates.

Pablo Santa Cruz