r/SQL • u/No_Ambition8323 • Sep 24 '26
Discussion What SQL query looks harmless but can cause serious problems?
Some SQL queries look simple and safe but can cause major issues when used on production data.
For example, a missing WHERE condition in an UPDATE or DELETE, or an incorrect JOIN that unexpectedly multiplies rows.
What SQL query or mistake have you seen cause a serious problem even though the query looked harmless?
1
Upvotes
41
u/juu073 Sep 24 '26
Without checking an actual value of a field. It deletes pretty much everything but nulls, empty strings, and zeros.
It looks harmless because it looks like you weren't dumb enough to leave the WHERE off, but there is no condition other than checking if field is true.
28
u/Idata10 Sep 24 '26
Select * From x Inner join y On x.id = x.id
17
u/Dboy3sixty Sep 24 '26
Datatype mismatch on a join predicate causing an implicit conversion. This can make indexes moot, force scans and if the query runs enough, cause you have a very bad day.
10
u/Healthy_Coconut1636 Sep 24 '26
If your day is ever feeling a little slow, just add CASCADE at the end of any statement if you really want to feel alive.
8
u/ugly_lemon Sep 24 '26
The thing the devs at my old company loved to do is to throw TRIM() and UPPER() on indexed columns, preventing the DB from using the index
7
u/jensimonso Sep 24 '26
- DELETE FROM someTable WHERE colX not in (select…) and the subquery returns nulls
- Accidental inner join:
select from table1 t1
Left join table2 t2 on t1.col=t2.col
Where t2.col2 = something
3
u/Profvarg Sep 24 '26
An UPDATE accidentally outside an IF (or basically anything accidentally outside an IF)
3
u/Joelle_bb Sep 24 '26
Uncapped recursion
2
u/Rare_Ad_3519 Sep 24 '26
Using between on a varchar that looks like a numeric data type
2
u/IAmADev_NoReallyIAm Sep 24 '26
Mass DELETEs or UPDATEs without a TRANSACTION ... hell, any delete or update without a TRANSACTION. That;s a mistake you make once. ONCE...
2
u/ChaosEngine-6502 Sep 28 '26
If you think about it, pretty much any SQL script can become a lethal payload, or at least severely service-affecting if the person writing it isn't intimately familiar with the data in the database they're querying.
I guess one not covered so far that I come across frequently in production code is:
Sure, the NOLOCK hint might be OK for one-off reports or ad-hoc queries where you just want to sample the data to see what's there, but there's always that risk it produces duplicates due to a page split occurring. That can foul things up royally.
2
u/Ok_Relative_2291 Sep 29 '26
Left joins with an on and then the where filter after it
1
u/tripy75 Sep 24 '26
begin transaction in a script, with the commit in a branch never reached
1
u/44Nj Sep 24 '26
Open transactions. Using unindexed columns in where statements or joins.
1
u/Upper-Raspberry4153 Sep 24 '26
Not deleting the current record at the end of your loop
1
u/Hour-Measurement-835 Sep 24 '26
UPDATE ... FROM where the join finds two source rows for one target. SQL Server and Postgres just apply one of them and don't warn you. Same data through MERGE errors out.
1
u/Einar_Son_of_Bjorn Sep 25 '26
highlighting just the
UPDATE users SET status = 'deleted'part of your query in dbeaver, slipping on the mouse, and accidentally hitting F5 before it selects theWHEREclause - congratulations, your database is now perfectly clean and you need to update your resume.1
u/DiscombobulatedSun54 Sep 28 '26
Misplaced semi-colons.
DELETE from <table_name>;
where <field_name> = <value>;
1
u/da_chicken Sep 28 '26
Now go to the remote view and change the column order. Would you like datetimes in your int column?
1
u/TacticalBunchies Oct 01 '26
Any query that starts with DELETE.