News for hackers. Stuff that matters.
Re:SQL queries don't start with SELECT by pjungwir on Thursday October 03, 2019 @04:30PM (#21151825)
Optimizer nuances aside, I agree with this and think it's helpful for anyone writing SQL. I've trained a lot of non-developers (and new-developers) to use SQL, and one of my favorite tips is, "Start with FROM."

Even sooner than "start with FROM" is "What does each row of the result mean?" Is it a sale? A month of sales? A customer? If you can answer what you want, then it's usually easy to start there. Especially if your answer corresponds to an existing table, put that in the FROM, and then add whatever joins you need. You can even think about the joins "imperatively".

Thinking imperatively about JOINs is also a helpful way of understanding a mistake I call "too many outer joins", which I wrote about here: https://illuminatedcomputing.com/posts/2015/02/too_many_oute... If you don't know about this mistake, it's easy to get the wrong answer from aggregate functions like AVG or COUNT. (EDIT: I should probably call this "too many joins" instead, since it's not really specific to outer joins. But in practice I find outer joins are more often used in these sort of situations.)

Re:SQL queries don't start with SELECT by yread on Friday October 04, 2019 @02:38AM (#21155358)
For updates, and deletes on the other hand I try to start with the where clause. Learnt the lesson after running a update query once before i typed the where to cause something like 40k worth of damage
Re:SQL queries don't start with SELECT by brigandish on Friday October 04, 2019 @02:43AM (#21155386)
Good advice. I also break up the statement so that an incomplete highlight and execute will fail, e.g.

    DELETE FROM   
      Customers WHERE    
      Name='Gude' AND   
      Area='Tama'   
      ;  
Much harder to leave off half the clause doing this kind of thing.
Re:SQL queries don't start with SELECT by fr1tkot on Friday October 04, 2019 @03:53AM (#21155673)
I've forced the habit upon myself to first compose a SELECT statement and verify that it will give me exactly the dataset that I want to run my operation on. Then I wrap that statement into a CTE and run the DELETE / UPDATE on the CTE.

It doesn't save me time, but a lot of nerves.

Re:SQL queries don't start with SELECT by CubicsRube on Friday October 04, 2019 @04:48AM (#21155924)
I'm used to writing the SELECT query and adding the commented-out DELETE just below the SELECT line.

  SELECT *
  -- DELETE
  FROM Sales
  WHERE Customer = 1
Next, selecting the whole query and executing it. If results are satisfactory - selecting only the DELETE part.
Re:SQL queries don't start with SELECT by piaste on Friday October 04, 2019 @03:55PM (#21161313)
This is the one I use too. Tiny difference - I write

   select * -- delete
   from sales
   where customer = 1
so it's even harder to accidentally highlight the 'delete' part.

Although I've switched to DBeaver for a year now, and it automatically pops up a warning when it detects a DELETE query without a WHERE, which is very nice.

Re:SQL queries don't start with SELECT by samus on Monday October 14, 2019 @05:29PM (#21253093)
DBeaver has something even better, albeit sometimes annoying: you can define a connection as "Production" (highlighted red) and "Test" (green). On Production systems, DBeaver will ask you for confirmation for every write statement.
Re:SQL queries don't start with SELECT by nimrody on Friday October 04, 2019 @07:25AM (#21156563)
Start all your sessions with "BEGIN TRANSACTION".

Then if you make an error you can always ROLLBACK. If it looks OK -- COMMIT.

Re:SQL queries don't start with SELECT by scarface74 on Friday October 04, 2019 @08:25AM (#21156898)
And then you use MySQL Workbench and forget to turn off autocommmit....
Re:SQL queries don't start with SELECT by cben on Sunday October 06, 2019 @06:16AM (#21171041)
Similar tip: don't start a command from `rm -rf ...`, type `rm /foo/bar/` first only when finished append ` -rf` at the end.

Similarly `git push ... -f` etc.

Re:SQL queries don't start with SELECT by samus on Monday October 14, 2019 @05:25PM (#21253068)
This saves you only if you delete directories. Better start with `ls`. The one with git force push is spot on though. This is one of the reasons to prefer tools that let you write options at the end of the argument list instead of after the command name.
Re:SQL queries don't start with SELECT by aleksei on Friday October 04, 2019 @05:25AM (#21156076)
Start every update and delete with BEGIN; and never fret again.
Re:SQL queries don't start with SELECT by gonzo41 on Friday October 04, 2019 @10:20AM (#21157965)
Yeah i've made a whoops like this before. I usually start a n update with a select statement to make sure my assumptions about the intent of the change is on point.
Re:SQL queries don't start with SELECT by C1sc0cat on Friday October 04, 2019 @07:00AM (#21156475)
For destructive statements I almost always do a select with the WHERE statement that I will use in the delete first so I can see I am not doing anything stupid.
Re:SQL queries don't start with SELECT by XJ6 on Friday October 04, 2019 @05:36AM (#21156113)
Auto-rollback transactions is helpful.

Everything is non-destructive without an explicit COMMIT.

Re:SQL queries don't start with SELECT by h1d on Sunday October 06, 2019 @09:03AM (#21171776)
For MySQL, run the command with

--safe-updates

to avoid queries running without WHERE clause.

Re:SQL queries don't start with SELECT by TehShrike on Thursday October 03, 2019 @06:24PM (#21152912)
I've called it the "GROUPing pitfall" since those extra JOINs only really come back to bite you when you try to aggregate the results: https://wikido.isoftdata.com/index.php?title=The_GROUPing_pi...
Re:SQL queries don't start with SELECT by pjungwir on Thursday October 03, 2019 @07:01PM (#21153173)
Nice writeup! The nasty part is that you don't get an error, you just get wrong answers. You might not even notice for a while. Such bad news!
Re:SQL queries don't start with SELECT by enobrev on Friday October 04, 2019 @07:18AM (#21156538)
Thanks to both of you for these articles. I'm very fond of outer joins as in most cases (in my day to day work) they're exactly what I need. While I don't use them often with aggregate queries, I didn't realize that multiple OUTER JOINS will break aggregates. I'll need to review my query logs to make sure I'm not doing this anywhere.
Re:SQL queries don't start with SELECT by ozlop111 on Friday October 04, 2019 @01:50PM (#21160132)
Was Emily Moore's child ever born?!?!
Re:SQL queries don't start with SELECT by lucb1e on Thursday October 03, 2019 @06:42PM (#21153024)
Unless I have the whole query in my head by the time I've typed "$db->query(", I will indeed start with the FROM by just typing the placeholder SELECT * and then revisiting the asterisk when I'm done with (most of) the query. Somehow that makes it easier, especially with complex queries.
Re:SQL queries don't start with SELECT by mritchie712 on Friday October 04, 2019 @02:16PM (#21160346)
I do this too, so the autocomplete in SeekWell automatically does this, check it out: https://www.loom.com/share/9c7979a163eb4513b3320e6e90c66079

If you type just a table name into an empty cell it will autocomplete with a full SELECT statement. e.g. if you typed "pubuser" and selected "public.users" it would autocomplete with:

  SELECT pu.*
  FROM public.users AS pu
  WHERE 1=1
Disclaimer: I built this.
Re:SQL queries don't start with SELECT by gigatexal on Thursday October 03, 2019 @08:15PM (#21153662)
I do this too! I thought I was the only one
Re:SQL queries don't start with SELECT by default-kramer on Friday October 04, 2019 @12:14AM (#21154706)
> Especially if your answer corresponds to an existing table, put that in the FROM, and then add whatever joins you need.

This is absolutely right. In fact, my ideal query language would require the programmer to be explicit if they want to increase the cardinality of the result set. Meaning, if I start a query with "from Employee ..." the query language should not let me accidentally add a join that will cause the result set to contain the same Employee more than once. (It should be possible, but only if I make it clear that it is intentional and not an accident.)

Re:SQL queries don't start with SELECT by dspillett on Friday October 04, 2019 @05:56AM (#21156198)
> If you can answer what you want, then it's usually easy to start there.

Yep. As developers/designers we need to understand this as much as we wished our clients did.

Re:SQL queries don't start with SELECT by scottc on Saturday October 05, 2019 @03:05PM (#21167297)
> "Start with FROM."

Interesting because LINQ starts the same way, or at least that's how I remember it.