17.6 Derived Tables: Subqueries in the FROM Clause
Alright, let’s talk about putting a subquery in the one place you probably didn’t expect it: the FROM clause. It feels a bit wrong, like putting a sofa in the kitchen. But sometimes, the kitchen needs a sofa. This is what we call a derived table, and it’s one of the most powerful tools for wrangling complex logic without losing your mind. Think of it this way: a regular SELECT works on a table. A derived table lets your SELECT work on the result of another SELECT. You’re effectively creating a temporary, in-memory table on the fly, just for the duration of your main query. It’s a fantastic way to break a monstrous, multi-part problem into a sane, two-step process: 1) build a smaller, focused result set, and 2) query against that.