How can I return a unique value based on a set of non unique values being searched?

For example:

If I wanted to return one phone number for a list of 4 people who can have more than one phone number - but I can only use one phone number for each person. It doesn't matter which phone number I use to reach them because any number that belongs to them will get me to them.

I don't think something like this exists - but if I could use something like the DISTINCT modifier except it would be called FIRST - it would solve my problem:

FROM Sample_Table
WHERE ID in ("Bob", "Sam", "Kyle", "Jordan")

In picture - from this


I'd like that (or any) query to return

something like this.

I'm using this type of query in a db where for 200 "ID"s there are millions of "Unique Values", so it is hard to get crafty.

New contributor
PJT is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Please edit your tags with the database you are using. – Gordon Linoff 18 mins ago

There is no such thing as a "first id" in a SQL table. Tables represent unordered sets. You can accomplish what you want (if I understand correctly) using aggregation:

SELECT ID, MIN(UniqueValue)
FROM Sample_Table
WHERE ID in ('Bob', 'Sam', 'Kyle', 'Jordan')

using group by and max method can help you:

 select id
    ,max (typeofvalue)
    from Sample_Table
    group by

Your Answer

PJT is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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