Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL statement with an impossible constant comparison

Tags:

sql

sql-server

I have this SQL query:

SELECT * FROM [table] WHERE 2=3;

Obviously, the query will return 0 rows, but my question is as follows. Does the SQL Server engine evaluate the where condition before executing the selection?

In other words, does SQL Server detect the where condition is impossible and return 0 rows automatically?

like image 699
davidfercas Avatar asked Jun 24 '26 16:06

davidfercas


1 Answers

If you examine the execution plan for a simple query using such a predicate you will see that SQL Server uses a Constant Scan to determine that zero results will return. No indexes or tables are even touched.

SELECT * FROM [Production].[Product];
SELECT * FROM [Production].[Product] WHERE 1=0;

enter image description here

like image 85
Chris Pickford Avatar answered Jun 26 '26 10:06

Chris Pickford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!