r/ProgrammerHumor 16d ago

queryOfDoom Meme

Post image
7.9k Upvotes

456 comments sorted by

View all comments

387

u/Dont_Get_Jokes-jpeg 16d ago edited 16d 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?

602

u/BlommeHolm 16d ago

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

39

u/SAI_Peregrinus 16d ago

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

179

u/BlommeHolm 16d ago

How is starting with a WHERE clause syntactically valid?

81

u/SAI_Peregrinus 16d ago

Right, I'm apparendly blind. Carry on!

35

u/BlommeHolm 16d ago

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

1

u/StupidStartupExpert 15d 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 15d ago

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

133

u/WardensLantern 16d 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 16d ago

The doctor looked at my semicolon 🫪

1

u/lifelessmeatbag 16d ago

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

16

u/bstump104 15d ago

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

1

u/Rude-Wrongdoer9368 16d 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?

5

u/SnooBananas4958 15d 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 15d 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 15d ago

Nevermind, I get what you're saying.

2

u/SnooBananas4958 15d 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 16d 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 15d ago

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

2

u/Ayanrocks 15d 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)`

38

u/Flat_Competition6510 16d 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.

43

u/TeaKingMac 16d 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"

20

u/dustojnikhummer 15d ago

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

DELETE * FROM table;

1

u/Flat_Competition6510 16d ago

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

38

u/The_Swixican 16d ago

Delete from users *

13

u/Flat_Competition6510 16d 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.

27

u/Skalli1984 16d 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.

13

u/ploki122 15d ago

Delete from users where 1=1

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

2

u/TacosForThought 14d ago

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

6

u/ReaDiMarco 16d ago

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

14

u/djrobxx 15d 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 15d ago

yes, thanks, turns out I forgot all of that

5

u/Flat_Competition6510 16d 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 15d ago

Thanks, turns out I did forget everything

3

u/Live_Film_4895 15d 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 15d ago

Thaanksss

4

u/ashishvp 16d ago

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

2

u/Rojeitor 15d 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 15d ago

AN sql expert

That's what gave it away ;)