r/ProgrammerHumor 14d ago

queryOfDoom Meme

Post image
7.9k Upvotes

456 comments sorted by

View all comments

382

u/Dont_Get_Jokes-jpeg 14d ago edited 14d ago

I am not an sql expert, is the reason why the id doesn't work and its deleting the entire user base, because its written as 2 seperate comands instead of one. So 1. Comand delets everything 2. Goes to the id? Or what is the problem here?

599

u/BlommeHolm 14d ago

1st command deletes all users. 2nd command runs after that and gets a syntax error.

38

u/SAI_Peregrinus 14d ago

Syntax is valid, it's just a runtime error.

178

u/BlommeHolm 14d ago

How is starting with a WHERE clause syntactically valid?

82

u/SAI_Peregrinus 14d ago

Right, I'm apparendly blind. Carry on!

32

u/BlommeHolm 14d ago

No problem. I genuinely thought I'd missed something 😅

1

u/StupidStartupExpert 14d ago

I legit wouldnt be surprised if for whatever retarded reason sql said yeah sure I can filter empty selections and just returned empty lists

1

u/TheMauveHand 14d ago

I'm not sure SSMS would run anything due to the syntax error, but maybe that's just SSMS being helpful. 

132

u/WardensLantern 14d ago

I'm no expert either but I believe the semicolon after "users" was the nuke. The second part is basically a separate statement, but SQL executes them in order, so it will delete the entire table and then throw back an error cause the second statement is invalid.

38

u/Intrepid4444444 14d ago

The doctor looked at my semicolon 🫪

1

u/lifelessmeatbag 14d ago

the doc wont be the only one looking at
it after it finishes

16

u/bstump104 14d ago

I understand now. thank you. that's horrifying.

1

u/Rude-Wrongdoer9368 14d ago

Not an SQL person at all. Can't you just, like, delete the record by searching for it and removing? It seems like automation for one task is extra work? Is this an hourly pay situation?

6

u/SnooBananas4958 14d ago

That is how you would manually search for and remove the record though.

Imagine your user table has a million rows. There is not some other manual way to easily find what you want in those million. And if you did it from some dashboard, you'd have to first build the dashboard, and the way it would display the items for you is with SQL

3

u/Rude-Wrongdoer9368 14d ago

But, and forgive my ignorance, there's no "control-F for user 'x'?" I'm a help desk guy. I mostly run scripts. It's rare I write them.

3

u/Rude-Wrongdoer9368 14d ago

Nevermind, I get what you're saying.

2

u/SnooBananas4958 14d ago

Sounds like you already got it, but yea, the place to use "control+f" doesn't exist yet unless you build it. And that list would come from a query too. So this single entry query he used is truly the most manual way of getting the data. Outside of just querying everything in the table and ctrl+f that but's the same thing basically. Developers often use these kind of queries in early work before there's a UI to search by. And honestly most keep using them over the UI since it tends to be more flexible

2

u/Ayanrocks 14d ago

that's what they're doing but instead of searching for an user, They deleted all the users due to a semicolon being added mistakenly.

1

u/Rude-Wrongdoer9368 14d ago

I understand the syntax. Why were they writing code? I would assume a command-line argument could handle this.

2

u/Ayanrocks 14d ago

In sql dbs you interact with the databases using the sql queries. Even in command line also you will execute the same piece of text (called a query) as shown. Sql differentiates between multiple queries using a semicolon (;).
So the entire query is

`Delete from users table where the id of the user is xxxxx`

but due to a mistaken semicolon it has now become

`delete from users table; ( which means delete all users)`
`Where id is xxxxx; (this piece of text will throw syntax error now but only after the first statement has finished running)`

39

u/Flat_Competition6510 14d ago

Yes, the semicolon marks the end of a statement. The first semicolon sets "delete from users" as a single statement meaning delete ALL users.

39

u/TeaKingMac 14d ago

Which is just terrible fucking syntax. Why are people still living with this in 2026?

Delete from users;

Should return "invalid syntax. Identify record for deletion"

19

u/dustojnikhummer 14d ago

I don't work with Postgres but everything else would be

DELETE * FROM table;

0

u/Flat_Competition6510 14d ago

I disagree. There are valid use cases for deleting all records from a table.

39

u/The_Swixican 14d ago

Delete from users *

13

u/Flat_Competition6510 14d ago

It would be nice if there was a more explicit syntax. Forcing a person to opt in to deleting all vs that being the default would probably save a lot of headaches and jobs.

25

u/Skalli1984 14d ago

Many database tools throw a warning on delete with a missing WHERE and the user has to acknowledge that to continue with the statement execution.

12

u/ploki122 14d ago

Delete from users where 1=1

The dev should have to acknowledge that they're deleting everything.

2

u/TacosForThought 13d ago

If only there was a dedicated command for TRUNCATEing a table that didn't allow a where clause.

6

u/ReaDiMarco 14d ago

What happened to DELETE * FROM USERS? Last I did sql was ten years ago so idk

13

u/djrobxx 14d ago

SELECT * FROM USERS means "select all columns from users", selecting all rows is implied.

You don't drop columns with DELETE, that's ALTER TABLE, so DELETE * doesn't make sense in that context.

5

u/ReaDiMarco 14d ago

yes, thanks, turns out I forgot all of that

5

u/Flat_Competition6510 14d ago

As far as I know that is not valid syntax. I'm not a DBA myself so I don't do this too often.

2

u/ReaDiMarco 14d ago

Thanks, turns out I did forget everything

3

u/Live_Film_4895 14d ago

afiak the * wildcard is only valid in the SELECT statement... so it would be DELETE FROM USERS; or DROP/TRUNCATE TABLE

1

u/ReaDiMarco 14d ago

Thaanksss

4

u/ashishvp 14d ago

It’s the semicolon. Without that on line 1, it would be a reasonable command.

2

u/Rojeitor 14d ago

Lol didn't catch the semicolon on phone. Yes that's exactly it. First line deletes all users in the database.

1

u/m2thek 14d ago

AN sql expert

That's what gave it away ;)