|
|
|
|
|
by emaginniss
1400 days ago
|
|
Everyone else is mentioning it from an IDE perspective, but let's also think about logically from a language perspective. When you start a FROM clause and add some JOINs, a few WHERE conditions and maybe GROUP BY, you are building a virtual view of a series of tables, columns, and aggregations. You could even define this data set as an ephemeral table. What you do with that data set afterwards might vary depending on the need, but the data set might not change. Depending on the application, you might select different columns from the data set. We do this naturally using a WITH clause at the beginning of a query. WITH (combine a whole bunch of stuff) as dataset SELECT a, b, c FROM dataset This approach just says: FROM tables... WHERE ... SELECT a, b, c To me, it does make a lot of sense. This is also the paradigm that some of the graph databases use. |
|
Exactly! This is where SQL hurts me the most: not being able to store (partial) query expressions in variables for later reuse. The only way to do this is by creating explicit views (requires DDL permissions) or executing the partial query into a temporary table (which is woefully inefficient for obvious reasons).