Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I attended some interviews in that they asked these questions.

what is result of execute this command "delete from table where 1=1"

my ans is

delete from table where 1=1

and

delete from table

both are doing same functionality. again they asked then why we include where 1=1? is there any special reason?

and another one is instead of 1=1 can we use other numbers like 2=2 / 3=3 /1=2/2=1 etc..

please verify i have lot of confusion in that. i could not provide perfect ans on this to interviewer..

Thanking you,
Posted
Updated 12-Aug-13 23:44pm
v2
Comments
[no name] 13-Aug-13 5:46am    
http://stackoverflow.com/questions/242822/why-would-someone-use-where-1-1-and-conditions-in-a-sql-clause

1 solution

ThePhantomUpvoter has provided you with a good link and it answers your original question, but I'd like to add a couple of things: Don't do it.

The WHERE 1=1 acts as placeholder for adding AND userName='FRED' and so forth conditions where you aren't sure exactly what will be added. As such you could replace it with any other condition which evaluates to true without affecting the outcome. But...it's a dangerous practice, and not one I would recommend. What happens if the additional clauses are not added, because the user doesn't enter any? Particularly with a DELETE command, this is spectacularly poor design because the default action is to delete everything in the database table...ouch!

A better approach is to construct the whole WHERE clause intelligently, specifically checking if there is anything to add at all, and not performing the command at all if not. Only if there is more than one item, do you need to faff with AND operators.

You could replace it with WHERE 1=2 instead, but only if you them replace the AND clause with OR - which may badly affect the delete list that is generated so care should be taken to ensure the user realises exactly what he is deleting!
 
Share this answer
 
Comments
OriginalGriff 13-Aug-13 6:08am    
You're welcome!

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