Live data from Hacker News

SQL Tips and Tricks

github.com

21–30 of 168 posts

Re: SQL Tips and Tricks

#21

Leading comma is nice in SELECT statements because you can comment toggle individual lines. Indenting your code to make it more readable is basically what anyone with room temperature IQ does automatically. A lot of these other tips look like they're designed to deal with SQL design flaws, like how handling nulls isn't well defined in the spec so it's up to each implementation to do whatever it wants.

A lot of databases support trailing commas in select clauses. Which is just as well. I want to scratch my eyes out every time I see someone formatting with comma starting the lines. It's the kind of foolish consistency that is a big part of performative engineering.

> I want to scratch my eyes out every time I see someone formatting with comma starting the lines

Right!? I _physically_ recoil every time I see that. I think that's the clearest example of normalisation of deviance [1] I know. Seems like anyone that enters the industry straight from data instead of moving from a more (software) engineering background gets used to this.

And the arguments in favour are always so weak! - It's easier to comment out lines - Easier to not miss a comma

Those are picked up in seconds by the compiler. And are a tiny help in writing code vs violating a core writing convention from basically every other language.

[1]- https://danluu.com/wat/

Re: SQL Tips and Tricks

#23

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.

I'm not the biggest fan of how the first two conventions look, but they are real conventions used by real SQL people. And I can understand why they exist.

I've seen them enough to not be bothered by them any more.

Re: SQL Tips and Tricks

#24

Never use WHERE 1=1. It is both a security risk and a performance risk to run dynamic ad-hoc queries.

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

#28

Leading comma is nice in SELECT statements because you can comment toggle individual lines. Indenting your code to make it more readable is basically what anyone with room temperature IQ does automatically. A lot of these other tips look like they're designed to deal with SQL design flaws, like how handling nulls isn't well defined in the spec so it's up to each implementation to do whatever it wants.

A lot of databases support trailing commas in select clauses. Which is just as well. I want to scratch my eyes out every time I see someone formatting with comma starting the lines. It's the kind of foolish consistency that is a big part of performative engineering.

Postgres and postgres-likes (e.g. Redshift) notably don't support trailing commas in select clauses.

Re: SQL Tips and Tricks

#29
post #7

Not shown: stop using SELECT *. You almost certainly do not need the entire width of the table, and by doing so, you add more data to filter and transmit, and also prevent semijoins, which are awesome.

There are broadly two kinds of people who write SQL: analysts, and developers

For developers, yeah. SELECT * has pitfalls, and you should almost always specify your columns or use a query builder that does that for you.

For analysts though, life is short and sometimes you really don't want to type all the columns out. SELECT * is fine.

Re: SQL Tips and Tricks

#30

Never use WHERE 1=1. It is both a security risk and a performance risk to run dynamic ad-hoc queries.

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.
Post reply on HN