When using "IF DOES NOT EXIST (SELECT ..." on the Sql server, it doesn’t matter which columns you select?

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?

+4
source share
3 answers

, 6.5 - 7 SQL Server, , , :

IF NOT EXISTS(SELECT * FROM Countries WHERE Name = 'France')

. SELECT 1 , , , .

, SQL - EXISTS FROM SELECT .


EXISTS:

, EXISTS, (*). , , , , .

+8

, sql- , .

+3

. , , - , SELECT 1 .

+3

All Articles