Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Wow! Thank you so much for the explanation!

> I have seen folks add pointless "1 = 1"-type clauses to their text

I have seen something like that, too, and I have always wondered if that was just job protection or if there was some arcane reason to it. The code in question was part of a view, though, so it ran unaltered countless of times. That view also contained numerous "1 <> 1" clauses, apparently to disable branches of the - rather intimidating WHERE clause. It was the one time I took over someone else's code and felt like swearing.



SQL with 1=1 makes sense to me.

Let's assume you start with "SELECT * FROM ITEMS" like query. Now you're adding the WHERE clause. Do you keep track if you have the WHERE already present? Do you then keep track of if you need the "AND" or if it's the first entry that cannot take the "AND"?

With 1=1 you're guaranteed to have the WHERE clause and you're guaranteed to be able to use AND in your statement.

Do you have a better way?


I agree that as a starting point for experimenting with an ad-hoc query, a "1 = 1" clause can make sense.

There were two reasons I was mad:

1. Using "1 <> 1" to disable a branch of a WHERE clause is not something I would have done. I would have commented it out. If you are used to this idiom, it might feel like the natural thing to do, but I think commenting it out makes the intention clearer.

2. I understand that queries can get a little convoluted when you start experimenting with them until you get the results you need. It is perfectly natural, and it is how the views I have written started out, as well. But before I put my views into production, I cleaned up the code, removed the parts I did not need or added a little comment to explain why the code was there.

Actually there were three: 3. The entire view was written in such a convoluted way that I was pretty sure the person who had written it was drunk at the time (he was an alcoholic, so this is probably true in the literal sense). When I inherited it, the view had about 700 lines and was mostly unreadable. When I was finished cleaning it up, I had reduced that to about 300 lines. I checked very carefully, but it still gave the exact same results. It still was not pretty, but if I could throw out more than 50% of the original code without affecting the result, you can probably imagine what a mess it was.


I think you hit the most likely reason for the "1 <> 1" clauses: it was basically "commenting out" branches of the query (or something programmatically generated the query in a way that needed to switch branches on and off without changing the query text so much that the query builder would have to understand more SQL syntax).

> It was the one time I took over someone else's code and felt like swearing.

The one time? Y'all hiring?


Well, to be honest, I have taken over about four pieces of software over the years, and one of them was a one-and-a-half time maintenance job (adding one tiny little feature, later finding a bug that turned out to be not in the application after all).

I was exceedingly lucky, I guess, because except for that hairy SQL view, they were all gems of clarity.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: