T-SQL returning something from nothing

Occasionally you may need to return a value even if nothing exists.
 
In this very basic example, suppose you have an empty table i.e.

SELECT id FROM table1;

image1
Now say, you need to return the value 1 to the application if there is no data.
 
There is a multitude of ways of achieving this, and here is one:

SELECT COALESCE((SELECT id FROM table1),1) AS id;

image2

Leave a Reply