Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table of some columns named as Students.

I am using following three queries to select rows.

1.
SQL
select * from Students

2.
SQL
select * from Students where 1=1

3.
SQL
select * from Students where null=null

When I execute first then this returns all rows.

When I execute second then this is also returns all rows.

But when I execute third then this does not return any row.

I have two issues

What is 1=1 and null=null and why are we using these?
What is difference between first query and second query?
Posted

SQL
SELECT ... WHERE 1=1
is a pointless waste: 1=1 evaluates to true, so all records will be returned. It's functionally identical to the same select without the where clause (but harder to read, and probably slightly less efficient as the command has to be parsed)

SQL
SELECT ... WHERE null=null
is different: nulls propagate in SQL because they aren't "real" values - just a marker for a non-existent value - so any expression with a null in it, returns null. Since null is not true, the WHERE clause always fails, and no rows are returned.

See here: http://en.wikipedia.org/wiki/Null_(SQL)[^]
 
Share this answer
 
Comments
RDBurmon 6-Jun-15 9:57am    
Good answer!! My five
Afzaal Ahmad Zeeshan 6-Jun-15 10:30am    
You forgot to mention the usage of this comparison in SQL Injection.
OriginalGriff 6-Jun-15 11:03am    
His code as shown isn't susceptible to SQL Injection, so we have no idea what he is doing with it. :laugh:
Matt T Heffron 10-Jul-15 13:53pm    
+5!
This would work though. When comparing NULL you should always use "IS"
SQL
where null is null
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900