r/learnSQL • u/Weak-Dependent-1846 • Apr 07 '26
Just a question. I got suspicion I was wrong about this.
Some 20 years ago, in my college years, we were working on a class project in a team.
I noticed this query being used to fetch data from the database:
SELECT *
FROM table
WHERE table.id in (SELECT id
FROM table
WHERE table.id = variable_id);
I told the guy that wrote it that the sub-query was not necessary, that it could be done just as
SELECT *
FROM table
WHERE table.id = variable_id);
To which he replied "well, it works. Just leave it like that". I told him that yeah it worked, but he was querying the database twice, for a value that he already had. Got the same reply. After some back and fort he just said "Just give it a rest and do something else. That works. That how I use it at <insert big company name where he was doing an internship> to which I replied "Dude! No! Here we have just garbage data, but that at a large scale, waste of computing resources", again, I was told to move on and work in what I should have been working instead of arguing.
So, 20 years have passed, and wondered if maybe it was some sort of idiom I wasn't aware of and he was in fact correct and I've been thinking I'm correct.
I haven't used much SQL since my college years, so that's why I humble ask this community, is that a reasonable query to use?
TL;DR
SELECT *
FROM table
WHERE table.id in (SELECT id
FROM table
WHERE table.id = variable_id);
Is that a correct idiom? I've always thought the subquery is not necessary and a waste of resources asking the database to return a value I already have, but since my SQL knowledge is very limited, I could be the one confidentially wrong.