Menu

Post image 1
Post image 2
1 / 2
0

Part 10: Subqueries and CTEs

DEV Community: sql·Mohamed Idris·3 days ago
#jbEED8Pl
#dev#query#subquery#select#orders#total
Reading 0:00
15s threshold

Part of the "SQL: Zero to Ninja" series, written for junior web developers. Your boss asks: "Show me every user who spent more than the average order." You freeze. To filter by the average, you first need to calculate the average. But that is a whole query on its own. Can you put a query inside another query? Yes. That little trick is called a subquery, and it is about to make you dangerous. The idea in one line A subquery is a query inside another query (the inner one runs first and hands its answer to the outer one), and a CTE is just a way to give that inner query a clear name so the whole thing reads like plain English. The metaphor: a recipe with sub-steps Think of cooking pasta with sauce. You do not make it all in one motion. You have a sub-step: "first, make the sauce." Once the sauce is ready, you use it in the main dish. A subquery is that sub-step happening inside the recipe, sometimes a bit buried in the instructions.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More