What is the fastest way to check if a record exists in SQL Table?


Usually, I use the below code to check if a record exists in the table

SELECT user.id FROM users WHERE user.id = 2;

But I want to know if there is any other efficient way of doing it?

1 Answer

4 years ago by

You can use distinct while querying which stops searching after finding the first record and hence it yields fast results.

SELECT distinct 1 user.id FROM products WHERE user.id = ?;

4 years ago by Divya