0

say you want to check if any rows match a query (just a true/false if the table contains any matches). which is preferred (and optionally, why)? or is there a better way?

SELECT COUNT(*) > 0 FROM someTable WHERE someField = someValue 

or

SELECT someField = someValue FROM someTable WHERE someField = someValue LIMIT 1 

or

SELECT COUNT(*) > 0 FROM (SELECT someValue FROM someTable WHERE someField = someValue LIMIT 1) someAlias; 
1

1 Answer 1

0

I would use EXISTS:

SELECT EXISTS (SELECT * FROM someTable); 

This returns 1 (True) if the subquery returns any rows at all, otherwise it returns 0 (False).

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.