r/SQL • u/Bhanuprakash_1947 • Oct 03 '26
Discussion What’s the smallest SQL mistake that caused the biggest problem for you???
Not talking about some crazy DB failure or anything.
Could be something silly like a missing WHERE, wrong JOIN, duplicate rows, bad UPDATE....
Like a small mistake that looked harmless but ended up causing a big headache ....🤕
Curious what ppl have seen in real projects.
42
Upvotes
91
u/TraumaBondage Oct 03 '26
As a junior dev, I left an open transaction on a Friday that brought down the whole prod db over the weekend.
42
u/MsPandaLady Oct 03 '26
Honestly the biggest mistakes are where the code works but pulls the wrong data. I can't point to a specific incident but always review
42
u/Standgeblasen Oct 03 '26
I’m at my first job, supporting Saleslogix and the backend SQL Server database.
Learning as I go, trying to clean up some orphaned customer accounts. I identify the account to delete and confirm by running
SELECT * FROM Customers WHERE ID = 12345
It works, returns the row I want, and so I change the statement to
DELETE FROM Customers WHERE ID = 12345
Then I highlight the text “DELETE FROM Customers” and hit execute. 2 seconds go by, no result. Brain goes into overdrive, “what’s wrong?”, “why is it taking so long?” “Is it frozeOOOOOOHHH SHITSHITSHIT CANCEL CANCEL CANCEL!!!!!!!!!!”
I slam the cancel button and then spent the next 15 minutes making sure that nothing was committed and I didn’t accidentally delete every customer in the table.
In the end, it was all good, I never had to tell my boss and no one ever found out. But I learned a very valuable lesson that day, and I am extra diligent when deleting records, usually storing a table backup in a temp table first if I think there’s a risk of unintended consequences.
16
u/everyonemr Oct 03 '26
Making no mistakes and having performance fall off cliff once the dataset reaches a certain size.
10
u/Rohml Oct 03 '26
No WHERE on the UPDATE or DELETE statement... Still haunts me two decades later.
9
u/Rehcra Oct 03 '26
DELETE FROM t_check_detail WHERE check_id = 123 OR 124
8
u/NoEggs2025 Oct 03 '26
DELETE table without the WHERE
8
u/Infamous_Welder_4349 Oct 03 '26
Usually forgetting how null works between systems. Some systems treat null as an unknown, some as no value, or no. The difference is in "not in list" or list with a sub query that contains a null. The problem usually presents as o data or all data.
6
u/ghostlistener Oct 03 '26
My biggest mistake was forgetting the where, fortunately there was a backup of the table that I could undo the mistake.
6
u/lolcrunchy Oct 03 '26
Curious why you start your last sentence with "curious" just like all the other ai posts
5
u/Winterfrost15 Oct 03 '26
Truncate. It is dangerous.
4
u/Sir-Squashie Oct 03 '26
Using a non deterministic view which subtly changed my results every run of the coce
4
u/sam_cat Oct 03 '26
Ad a junior dev wrote a complex delete in ssms. All transaction wrapped with rollback to test it etc. Me and manager worked through all environments, all good. Get to prod and after testing with rollback highlight the query to run it without rollback. Didn't highlight the where clause. Didn't notice. "Why is it taking so much longer this time". "Oh. Oh fuck.... Bossss, I messed up!" Easy fix... But youch!
This was when he introduced commit transaction for run rather than highlighting code to run. Previously dangerous code was kept in a rollback and then we highlight everything except the transaction wrapper. Surprised it didn't happen sooner tbh.
4
u/mike-manley Oct 03 '26
Where my missing or commented out WHERE clause peeps at?
4
u/piercesdesigns Oct 03 '26
At one Fortune 100 were using Oracle and for some reason our devs had admin privileges on system tables. I can’t remember why we didn’t revoke that, it was a long time ago.
One of the devs had the GUI enterprise manager open and deleted the connect privileges from all users.
I had to repair that right quick.
3
u/supercoach Oct 03 '26
I made the mistake once of thinking a composite index would be the best way to go for a new application I was developing.
I kept wondering why the app would stall and then I looked at the query planner and saw the index was being skipped for half my queries. During testing with a few tens of thousands of rows it worked fine because everything was in memory, but once we got to production with hundreds of millions of rows it all slowed to a crawl.
I then actually looked at the index usage stats and made a plan that was based on evidence, not assumptions. Through a combination of improved indexing and better query structure I got the query time down from minutes to less than a second in most cases.
3
u/danmc853 Oct 03 '26
; commit
3
u/muteki_sephiroth Oct 03 '26
Company I worked for had a new application and I was brand new. Like, BRAND NEW. Our company had just lost our primary DBA so I got “promoted” to the job. I was all we had.
Didn’t know then that MSSQL is a memory hog. Kept watching the memory climb and thought we had a leak. To “fix” things I kept restarting the instance. As a result our app kept tipping over.
I still wake up in a cold sweat at night about it.
3
u/DexterHsu Oct 03 '26
IN operator result list has a NULL
2
u/AlCapwn18 Oct 03 '26
I ran a delete statement but forgot part of the where clause on a table containing patient data from randomized control trial studies. I didn't realize it for months but thankfully I had backups that I could restore and copy over the missing data.
2
u/Crab1551 Oct 03 '26
Extract data using a combination of OR - AND. You never know if output is correct o not
2
u/dudeman618 Oct 03 '26 edited Oct 03 '26
Drop table in production. Update column to null without a where clause. Delete rollback segment before I get the data. The worst was my offshore team had a series of jobs that did major updates across production runs, it fails half way through so they just restarted it from the beginning, cause nearly a year of data cleanup. New to automation tool, set up email notification to myself, it sent one email per row (around 5000 emails), when I was expecting one email at the end of the job.
2
u/masala-kiwi Oct 03 '26
I used CREATE OR REPLACE to add a column on an audit table in Snowflake. Should have used ALTER TABLE. Wiped 2+ years and millions of rows of unrecoverable data. I discovered my error 2 days after Snowflake's backup copy expired.
2
u/xenomachina Oct 03 '26
One of the first things I did at a new job was to find and fix discrepancies in our production database. At the time, they were using MySQL with no transactions. This was for billing data. The data was also denormalized in such a way that sometimes it'd become inconsistent. (Not my design. I was the new guy, remember.)
I wrote a script that found these discrepancies, and then generated SQL consisting of update statements that would repair them in whichever direction worked in the customer's favor. The script was reviewed by a senior developer, and then its output was reviewed by both me and that same senior developer.
We both somehow missed that the updates completely lacked a where clause until we ran the SQL and saw that every row was updated.
Luckily, the updates were all additions, not sets, so we were able to change them all to subtractions. We managed to do this quickly enough that there were no new rows.
(Eventually we completely rewrote this system, and changed the DB to be normalized and use transactions.)
1
u/grokbones Oct 03 '26
Changing an index on a 2 million row table. Resulted in a loss of server response and team restore from backup with data from 45 minutes old to say “all good” by helpdesk team.
1
u/WomenRepulsor Oct 03 '26
A colleague forgot WHERE clause in a update query. Ran it production
1
u/Ricnurt Oct 03 '26
In a query joining several tables, ran the where clause on tie wrong sub query. Froze the database..
1
u/DiscombobulatedSun54 Oct 03 '26
Making a left join an inner join by adding a where condition on a right table column without allowing for NULL.
1
u/captain_20000 Oct 03 '26
In my first two weeks on the job, I accidentally messed up a case statement. For example, instead of
case
when column = ‘value’ then ‘new value’
end
I put:
case
when column = ‘value’ then column = ‘new value’
end
It was on a Friday and the code was included in an automated job, so the job failed, as well as everything that was scheduled after. My boss came in Monday morning and was trying to pin point the problem, and traced it back to the change I had made. I was so embarrassed but thankfully he was understanding and it was an easy fix. Whew!
1
u/holmedog Oct 03 '26
Hardcoded use nested loops statement in a query. Not mine, but I found it while auditing long running jobs. Was running 13 hours. Was designed for a less than 200 row table that got repurposed. Removing the hint cut runtime to 10 minutes
1
u/Optimal_Law_4254 Oct 03 '26
Bobby Tables.
1
u/ToastieCPU Oct 03 '26
I accidentally used “new Guid()” instead of “Guid.NewGuid()” on a core table that handles invoicing, which resulted in around a thousand invoices being stored as a single large chain that had to be resolved manually.
1
u/TallDudeInSC Oct 04 '26
Missing join resulting in a Cartesian product is super common.
1
u/Ballbag94 Oct 04 '26
I made a mistake on an insert loop so it wouldn't end and then left it running overnight. Took down the dev/staging environment for a fair while until I could restore the DB
1
u/pinback77 Oct 04 '26
I think making sure to handle NULL values properly.
1
u/BarfingOnMyFace Oct 04 '26
From thing a
Inner join thing b on b.id = b.id
1
u/Sea_Fuel420 Oct 04 '26
Select
*
From dbo.xyz
In a schedules Pipeline
While table changed fields
1
u/Proclarian Oct 05 '26 edited Oct 05 '26
Something fairly insidious. I was tasked with keeping a SQL Server (source) and Postgres (target) server inventory table in sync with one another. This job ran every 30 minutes to keep freshness. My queries were actually fine, it was the surrounding code that was the issue. My development machine was a 4-core laptop so to speed up the process I grab inventory for {core count} locations at a time to load. In development this was fine. What was not fine is when I deployed this to production on a machine that has 32 cores. So every 30 minutes my transfer process opened 32 simultaneous connections and exhausted the threadpool in SQL Server.
I was using
with (nolock)and so stores using the application never experienced any deadlock or any actual exceptions, but they complained about general "slowness"(which was something they always complain about anyway because users are impatient when there's customers waiting) because the application was waiting on the threadpool for an available thread. This lasted for 4 months until we realized what the actual cause of it was.So yeah, don't set your degrees of parallelism to core count if your dev/testing environment is different from prod.
1
u/AlexAtOracleAIDB Oct 19 '26
Mine is an UPDATE with one condition missing from the WHERE. It only changes some extra rows, so it’s harder to spot than the missing WHERE you listed. In my experience the wrong values sit in those rows for days before someone asks for the old ones back.
That’s why I’d turn on Flashback Time Travel ahead of time for tables like that. Once a table is enabled, Oracle keeps its change history for a set retention period, so Flashback Query can read the table as of an earlier time, even after the undo data it normally relies on is gone.
-1
u/Agreeable_Ad4156 Oct 03 '26
Select * is always a bad move. Especially in a view, add a column and see what happens.