18

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?

2 Answers 2

35
select distinct id, name from [table]

or

select id, name from [table] group by id, name
Sign up to request clarification or add additional context in comments.

3 Comments

@OMG Ponies, how come 47k is not enough?
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.
and @OMG Ponies: "You can never get enough of what you don't need to make you happy." ~Eric Hoffer :-)
6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.