Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I'm trying to find out if a row exists in a table. Using MySQL, is it better to do a query like this:

SELECT COUNT(*) AS total FROM table1 WHERE ...

and check to see if the total is non-zero or is it better to do a query like this:

SELECT * FROM table1 WHERE ... LIMIT 1

and check to see if any rows were returned?

In both queries, the WHERE clause uses an index.

1 Answer

+1 vote
by (40.7k points)

Use this code:

SELECT EXISTS(SELECT * FROM table1 WHERE Condition)

you can refer to the documentation.

As Per the comment use this code:

SELECT EXISTS(SELECT 1 FROM table1 WHERE Condition)

Related questions

0 votes
1 answer
+2 votes
1 answer
0 votes
1 answer
asked Jul 13, 2019 in SQL by Tech4ever (20.3k points)
0 votes
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...