1

Basic question. In SQL, is there a quicker way to list conditions in a SELECT statement using the OR operator?

At the moment I'm writing multiple conditions in my statements like this:

SELECT * FROM customers WHERE City = 'Berlin' OR City = 'London' OR 
City = 'Dublin';

Is there a shorter way of doing this, without having to write 'City =' for each condition, perhaps?

1 Answer 1

1

Use IN:

SELECT c.*
FROM customers c
WHERE c.City IN ('Berlin', 'London', 'Dublin');

In addition to being more concise, this is more efficient for long lists as well. MySQL sorts the list of values and uses a binary search for long lists -- much more efficient than a sequential search under most circumstances.

Sign up to request clarification or add additional context in comments.

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.