When a programmer wants to check only the existence of records that meet certain criteria, he/she will probably use select count(*) from
I do it more quickly:
For example, in a table called EMP I want to see if there are employees with salaries greater than $10,000:
SELECT COUNT('X') FROM DUAL WHERE EXISTS (SELECT 'X' FROM EMP WHERE SALARY > 10000)However, the select statement in the form of...
SELECT COUNT('X') FROM DUAL WHERE EXISTS (SELECT 'X' FROM .... WHERE .....)
...is more effiecient because in the average case it is 2 times faster and will return 1 for existence and 0 for none.
For More Information
- What do you think about this tip? E-mail us at editor@searchDatabase.com with your feedback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBA's and developers? The best tips submitted will receive a cool prize--submit your tip today!
- Ask your technical SQL questions--or help out your peers by answering them--in our live discussion forums.
- Ask the Experts yourself: Our SQL gurus are waiting to answer your technical questions.