Comment

Comment by crazygringo

> Now the mysterious query: SELECT (SELECT sum(a) FROM xx LIMIT 1) FROM aa

To be clear, that's just an absolutely pathological query, since column "a" doesn't exist in table "xx" -- it only exists in table "aa".

You can use values from outer queries inside of inner queries, which are called "correlated subqueries". Which is not recommended unless you're already a real SQL expert, because they're so confusing (and their performance is also pretty absymal).

But this isn't even that -- this is an aggregate function across an outer variable. I consider myself a strong SQL expert, but looking at this query, it just feels nonsensical. Not only could I not tell you what it would produce -- I couldn't even guess at what its author intended it to mean in the first place.

> Later in the article we will see that SQL implementations don’t even agree about the correct result.

I'm not surprised at all. I'm actually more surprised they produce results at all, rather than just errors about it being an invalid query.

parent crazygringo 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton