Readit News logoReadit News
regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
AtNightWeCode · a year ago
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.

regexman1 · a year ago
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!

regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
AtNightWeCode · a year ago
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.
regexman1 · a year ago
I've amended the README.md to explain what I meant. My error was in using the word dynamic!
regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
AtNightWeCode · a year ago
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.
regexman1 · a year ago
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!

regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
Izkata · a year ago
For their one here it's just the ability to rapidly comment/uncomment conditions in a query editor while exploring the data or debugging the query, and not having to worry about the leading AND or OR.

I've also seen it in code with iterative adds, for example:

  for crit in criteria:
      sql += " AND " + crit
No needed to add a sentinel or other logic to skip the first AND. I saw it a lot before people got used to " AND ".join(criteria).

regexman1 · a year ago
That's right - it's just a quicker way of being able to comment/uncomment conditions when doing EDA or debugging.
regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
l5870uoo9y · a year ago
And I take it CTEs are implicitly being discouraged.
regexman1 · a year ago
Not at all actually, I just hadn't really planned to add this as a tip. Additionally I thought an in-line view was fine for the examples included. But maybe I will!
regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
halayli · a year ago
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.

regexman1 · a year ago
Fair point!
regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
silveraxe93 · a year ago
I'll try to give some constructive criticism instead of a drive by pot shot. I'm sorry, it's just that the leading commas make my eyes bleed and I really hope the industry moves away from it.

On point 3: What I do is use CTEs to create intermediate columns (with good names) and then a final one creating the final column. It's way more readable.

```sql

with intermediate as (

select

  DATEDIFF(DAY, timeslot_date, CURRENT_DATE()) > 7 as days_7_difference,

  DATEDIFF(DAY, timeslot_date, CURRENT_DATE()) >= 29 as days_29_difference,

  LAG(overnight_fta_share, 1) OVER (PARTITION BY timeslot_date, timeslot_channel ORDER BY timeslot_activity) as overnight_fta_share_1_lag,

  LAG(overnight_fta_share, 2) OVER (PARTITION BY timeslot_date, timeslot_channel ORDER BY timeslot_activity)as overnight_fta_share_2_lag
from timeslot_data)

select

  iff(days_7_difference, overnight_fta_share_1_lag, null) as C7_fta_share,

  iff(days_29_difference, overnight_fta_share_2_lag, null) as C28_fta_share
from intermediate ```

regexman1 · a year ago
I appreciate the feedback, no offence taken. I'm an analyst so I often find the leading comma useful when I'm testing something and want to quickly comment a column out but I take your point.

And I agree, I should have used CTEs for this query, I was just trying to save lines of code which had the unintended consequence of quite an ugly query. However I did want to use it as an example of indentation being useful to make it slightly easier to read. Although perhaps I'm the only one who thinks so.

I greatly appreciate the constructive criticism.

regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
silveraxe93 · a year ago
The "readability" section has 3 examples. The first 2 are literally sacrificing readability so it's easier to write, and the last has an unreadable abomination that indenting is really not doing much.
regexman1 · a year ago
That's a totally valid point haha.
regexman1 commented on SQL Tips and Tricks   github.com/ben-n93/SQL-ti... · Posted by u/regexman1
Semaphor · a year ago
Regarding "Comment your code!": At least for MSSQL, it’s often recommended not to use -- for comments but instead /**/, because many features like the query store save queries without line breaks, so if you get the query from there, you need to manually fix everything instead of simply using your IDEs formatter.
regexman1 · a year ago
I didn't realise that, great to know. Thanks!

u/regexman1

KarmaCake day107September 25, 2024View Original