From my understanding, primary keys are unique by definition. If that's true, would one ever want to SELECT DISTINCT on the primary key? Is this ever practical/useful?

  • Yes it is unique for the record so without joining to other tables, DISTINCT wouldn't matter. However, in a one to many relationship, or when joining to a table with unnormalized data, it may be beneficial. – scsimon Jun 22 '18 at 13:45
  • You don't have to use DISTINCT in a PRIMARY KEY columns since its already UNIQUE. There is also a performance impact when using DISTINCT. – Edgar Allan Bayron Jun 22 '18 at 14:06

If you join with another m:n table that duplicates the PK value in your result set, then it would be useful.

| improve this answer | |
  • (Or 1:n for that matter) – Koen D Jun 22 '18 at 13:46
  • Note that multi-column distinct groups are more efficiently queried using the GROUP BY clause. – Elaskanator Aug 15 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.