Today i found a query like
SELECT 1 [Col_name] FROM MyTable
and
SELECT [Col_name] FROM MyTable
Both seems to return the same result. I am confused.
This is the actual query:
SELECT 1 Col1 FROM [Table1] WHERE Col1 = 1
UNION
SELECT 2 Col1 FROM [Table1]
Any help is appreciated
Your query:
will return the literal value
1with an aliascol_name. Even ifcol_nameis an identifier in your table.However:
will select
col_namefrom your table.The same with:
Will give you only two rows:
regardless the values in the table. Because
SELECT 1 Col1 FROM [Table1] WHERE Col1 = 1returns the literal value1with an aliascol1,SELECT 2 Col1 FROM [Table1]returns the literal value2with the same aliascol1withUNION(Distinct select) set operator, will give you only two values (1, 2) since they are the only distinct values.Live Demo