HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajor

Why would someone put `where 1=1` in a query?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
whysomeonewhereputquerywould

Problem

I came across a view in our database today where the first statement in the where clause was where 1 = 1. Shouldn't this return true for every record? Why would someone write this if it isn't filtering any records?

Solution

Some dynamic query builders include this condition so that any "real" conditions can be added with an AND without doing a check like if (first condition) 'WHERE' else 'AND'.

Context

StackExchange Database Administrators Q#763, answer score: 41

Revisions (0)

No revisions yet.