Skip to main content
made example more clear
Source Link
Martin Schapendonk
  • 13.6k
  • 3
  • 22
  • 26

It's better to use either of the following:

-- Method 1. SELECT 1 FROM table_name WHERE keyunique_key = value; -- Method 2. SELECT COUNT(1) FROM table_name WHERE keyunique_key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize SELECT COUNT(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.

It's better to use either of the following:

-- Method 1. SELECT 1 FROM table_name WHERE key = value; -- Method 2. SELECT COUNT(1) FROM table_name WHERE key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize SELECT COUNT(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.

It's better to use either of the following:

-- Method 1. SELECT 1 FROM table_name WHERE unique_key = value; -- Method 2. SELECT COUNT(1) FROM table_name WHERE unique_key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize SELECT COUNT(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.

Expanded and organized code, capitalized SQL functions.
Source Link

It isIt's better to use aeither of the following:

select-- Method 1. fromSELECT table1 whereFROM table_name WHERE key = value; select-- countMethod 2. SELECT COUNT(1) fromFROM tabletable_name whereWHERE key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize selectSELECT countCOUNT(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.

It is better to use a

select 1 from table where key = value; select count(1) from table where key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize select count(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.

It's better to use either of the following:

-- Method 1. SELECT 1 FROM table_name WHERE key = value; -- Method 2. SELECT COUNT(1) FROM table_name WHERE key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize SELECT COUNT(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.

Source Link
Martin Schapendonk
  • 13.6k
  • 3
  • 22
  • 26

It is better to use a

select 1 from table where key = value; select count(1) from table where key = value; 

The first alternative should give you no result or one result, the second count should be zero or one.

How old is the documentation you're using? Although you've read good advice, most query optimizers in recent RDBMS's optimize select count(*) anyway, so while there is a difference in theory (and older databases), you shouldn't notice any difference in practice.