News for hackers. Stuff that matters.
Re:SQL Tips and Tricks by AtNightWeCode on Wednesday September 25, 2024 @01:43AM (#41644042)
Never use WHERE 1=1. It is both a security risk and a performance risk to run dynamic ad-hoc queries.
Re:SQL Tips and Tricks by DH61AG on Wednesday September 25, 2024 @04:17AM (#41645020)
This doesn't make any sense at all.
Re:SQL Tips and Tricks by adamzochowski on Wednesday September 25, 2024 @01:57AM (#41644122)
Can you expand on this? How is having

    WHERE 1=1
    AND ...[usual-where-clause]...
A performance and security compared to doing

    WHERE ...[usual-where-clause]...
Re:SQL Tips and Tricks by AtNightWeCode on Wednesday September 25, 2024 @11:27AM (#41648535)
If you use it in the same way you use trailing commas. Fair. But the site says to make it easier to add dynamic conditions. Which is a terrible idea in maybe not all but many SQL engines.
Re:SQL Tips and Tricks by ricardo81 on Wednesday September 25, 2024 @02:23AM (#41644248)
Presumably you are thinking about queries in code that add WHERE clauses dynamically that aren't escaped correctly- which doesn't have to be the case.

1 = 1 is at least handy for simply joining a variadic amount of other clauses with ' AND ' rather than counting if there's any to add at all.

Re:SQL Tips and Tricks by AtNightWeCode on Wednesday September 25, 2024 @09:15AM (#41647154)
Yes. "Use a dummy value in the WHERE clause so you can dynamically add and remove conditions with ease:" I don't know how to read this in another way.
Re:SQL Tips and Tricks by regexman1 on Thursday September 26, 2024 @07:26PM (#41664669)
I've amended the README.md to explain what I meant. My error was in using the word dynamic!
Re:SQL Tips and Tricks by jcz_nz on Wednesday September 25, 2024 @02:01AM (#41644137)
Can you elaborate on security issues here?
Re:SQL Tips and Tricks by thestepafter on Wednesday September 25, 2024 @02:26AM (#41644271)
I think that it means the reason for doing where 1 = 1 is sometimes to allow for easy insertion of dynamic queries which can be a security and performance issue. The actual usage of where 1 = 1 doesn't cause the security or performance issue.
Re:SQL Tips and Tricks by AtNightWeCode on Wednesday September 25, 2024 @11:21AM (#41648484)
Which is exactly what the site says. To insert dynamic conditions. I know that you can use 1=1 for the same reasons as trailing commas. But kinda obvious that this is not the case here.
Re:SQL Tips and Tricks by regexman1 on Thursday September 26, 2024 @07:20PM (#41664623)
Just to be clear I'm using it for the same reason as trailing commas.

If I'm inspecting a dataset I use WHERE 1=1 so I can add and remove conditions more easily.

I realise the confusion is in my wording of dynamic - I might amend the README.md to clarify. Thanks for the feedback!

Re:SQL Tips and Tricks by regexman1 on Wednesday September 25, 2024 @02:14AM (#41644190)
I'll add this as a caveat. I'm an analyst so my SQL isn't really exposed to anyone other than myself and so I wasn't aware of this, thanks for flagging.
Re:SQL Tips and Tricks by halayli on Wednesday September 25, 2024 @02:42AM (#41644363)
A random person claims adding 1=1 is a security risk and you are going to add it as caveat without verifying if the claim is true nor knowing why? That's how misinformation spreads around.

OP doesn't know what they are talking about because adding 1=1 is not a security risk. 1=1 is related to sql injections where a malicious attacker injects 'OR 1=1' into the end of the where clause to disable the where clause completely. OP probably saw '1=1' and threw that into the comment.

Re:SQL Tips and Tricks by regexman1 on Wednesday September 25, 2024 @02:50AM (#41644417)
Fair point!
Re:SQL Tips and Tricks by AtNightWeCode on Wednesday September 25, 2024 @12:34PM (#41649190)
Read my other comments. I worked with SQL on and off since the last century. It has nothing to do with your poor assumptions.
Re:SQL Tips and Tricks by the_gorilla on Wednesday September 25, 2024 @06:30PM (#41652561)
Duration of working with SQL doesn't matter. The better SQL programmers don't do it specifically, and have experience in real languages that they bring over to database queries.
Re:SQL Tips and Tricks by AtNightWeCode on Thursday September 26, 2024 @02:00PM (#41661290)
Not sure I get this. But I think it does matter since you understand why people do it to begin with. I worked on two enterprise solutions over the last couple of years that have this exact problem. That people are using WHERE 1=1 and then add random "AND something=something" that completely trashes the performance of the db. Also, it does not matter as much on-prem. But in cloud envs it does. Since you can't really spike CPU and mem the same way as on-prem.

The reason I pointed out this specific issue is just that I thought it was the worsed of many poor tips. ChatGPT can give better tips.

Re:SQL Tips and Tricks by the_gorilla on Thursday September 26, 2024 @04:11PM (#41662732)
If the query planner can't optimize out "IF TRUE" I don't know what to say. Is there something deeper happening or is this just gross incompetence?
Re:SQL Tips and Tricks by AtNightWeCode on Tuesday October 01, 2024 @05:18PM (#41714287)
Jesus. The problem is not WHERE 1=1. It is WHY people do it. People do it because they then in Python, JS or whatever can easily add conditions. Like QUERY + "AND x='blabla'". There is the problem. Every time you create a unique SQL statement the query will need a new query plan in most SQL engines. Some will cache parts of the query. And you could use parameters along with this paradigm but if you are this bad at SQL I doubt it.

It is kinda funny that op backpedal on this cause to me the whole site is amateurish. I just pointed out what I thought was the worst part. It is likely that it is generated by AI. Either way the post is terrible.

Re:SQL Tips and Tricks by regexman1 on Wednesday October 02, 2024 @12:13AM (#41717093)
I'm an analyst so I literally only query for analytical purposes. I don't have any SQL code embedded in an application so the 1=1 is purely for tweaking/testing a query more quickly.

I certainly didn't use AI, all these tips/tricks are from work experience and reading Snowflake documentation and the like, but I guess I can't convince you either way. Regardless I appreciate the feedback, even if I did find the tone a little aggressive.

Re:SQL Tips and Tricks by paperplatter on Wednesday September 25, 2024 @12:20PM (#41649060)
1=1 is not a security risk
Re:SQL Tips and Tricks by Gunax on Wednesday September 25, 2024 @01:51AM (#41644085)
What is a dynamic, adhoc query? Why does adding 1=1 support that?
Re:SQL Tips and Tricks by egormakarov on Wednesday September 25, 2024 @04:13AM (#41644995)
Lets say its 2001 and you are writing some hot e-commerce stuff in plain php. You want to filter data depending on multiple fields in the submitted form. If some field is there, you add one more "AND" clause to the "WHERE", like this: if (isset($_POST['product'])) { $query .= "AND product = " . $_POST['product']; }. So in order not to check every time if the added clause is the first one you start with "WHERE 1=1 ", as "WHERE AND ..." would not work.
Re:SQL Tips and Tricks by freilanzer on Wednesday September 25, 2024 @08:47AM (#41646815)
Php has nothing like this?

In [1]: "... WHERE " + " AND ".join(str(i) for i in range(4))

Out[1]: '... WHERE 0 AND 1 AND 2 AND 3'

Very strange.

Re:SQL Tips and Tricks by egormakarov on Wednesday September 25, 2024 @10:00AM (#41647637)
This will produce broken SQL on empty clauses list. Very strange.
Re:SQL Tips and Tricks by freilanzer on Thursday September 26, 2024 @02:45AM (#41655233)
You're quite right, but this is easily fixed. That doesn't change my question, since something like this is much easier that the other logic.
Re:SQL Tips and Tricks by paperplatter on Thursday September 26, 2024 @12:42PM (#41660521)
The easiest fix for this is the "WHERE 1=1" or "WHERE true"
Re:SQL Tips and Tricks by paperplatter on Wednesday September 25, 2024 @12:19PM (#41649051)
I get how this isn't good. But how else would you handle multi-field filtering, keep all the ANDs and use (product_id = $1 OR $1 IS NULL) so the unset filters are no-op? That's ok as long as the query planner is smart enough.
Re:SQL Tips and Tricks by abrookewood on Wednesday September 25, 2024 @04:09AM (#41644969)
I'm wondering that as well. I don't get that suggestion at all.
Re:SQL Tips and Tricks by AtNightWeCode on Wednesday September 25, 2024 @06:51AM (#41645914)
In this case. A query that you build by adding different strings. 1=1 is for adding AND statements to the WHERE clause dynamically. In your code. I never seen it used for anything else. Adhoc is just the practice of running raw SQL queries.

So you end up with things like this.

"SELECT * FROM Music WHERE 1=1" + "AND category='rock'"

The risk is now that you by mistake allow for SQL-injections but also every genre will generate a different query plan. Depending on what SQL engine you use this may hurt performance.

And one would think that this is a thing of the past. But it is not.