1

I know that the WHERE ... IN ... clause allows to select all values of a field that exists in a given array. How do I expand this idea to a group of fields? Something like:

SELECT * FROM table_name
WHERE (First, Last) IN ((Adam, Scott), (Betty, Johnson), (Cathy, Wyatt))

Or any other method that allows the same result.

3 Answers 3

2

The syntax in the question is valid in some other RDBMSs but not SQL Server. If on 2008+ you can use

SELECT *
FROM   table_name t
WHERE  EXISTS(SELECT *
              FROM   (VALUES ('Adam', 'Scott'),
                             ('Betty','Johnson'),
                             ('Cathy','Wyatt')
                              ) v(first, last)
              WHERE t.first = v.first AND t.last = f.last 
Sign up to request clarification or add additional context in comments.

Comments

1

It depends on what you are after really. If you just want to combine 2 fields then you can use something like this:

SELECT * FROM table_name
WHERE First+','+Last IN ('Adam, Scott', 'Betty, Johnson', 'Cathy, Wyatt')

4 Comments

@NickyThai it should do. Though relies on commas not being valid in the columns being concatenated.
What do you mean? If your table_name has string type columns named First and Last then it will work fine.
@MartinSmith, @Malk I got the error Invalid column name ',' I tried both single quote and double quote.
Double quotes it will be treated as an object identifier under default settings and give you that error. Single quotes should work though...
-3

SELECT * FROM Table_name WHERE First IN ('Adam', 'Betty', 'Cathy') AND Last IN ('Scott', 'Johnson', 'Wyatt')

2 Comments

This will return rows with names matching any of the 9 permutations of those names. Not just the three desired.
@MartinSmith That's true, but I don't see where the question says that wasn't a desirable outcome. He or she could have been more specific that they want to return only rows where the first name and the last name were matched.

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.