Quite a few database scripts take the form:
IF NOT EXISTS(SELECT * FROM Countries WHERE Name = 'France')
INSERT INTO(Countries)
However, I also saw people:
IF NOT EXISTS(SELECT CountryID FROM Countries WHERE Name = 'France')
INSERT INTO(Countries)
And even:
IF NOT EXISTS(SELECT 1 FROM Countries WHERE Name = 'France')
INSERT INTO(Countries)
The advantage of the latter is that its efficiency is more efficient: the query does not actually use any of the columns in the subquery, so it would be faster not to return any of them. But it looks weird, so it seems to me that this may confuse some people. And does it really matter to the actual runtime?
source
share