Analyzing subqueries and CTEs
SQL subqueries, also known as nested queries or inner queries, are queries that are embedded within the context of another SQL query. They are powerful tools for performing complex data manipulations that require one or more intermediary steps – that is, they are used for performing data manipulation operations that require multiple steps or depend on the result of an intermediary query.
That might sound complex and, indeed, subqueries can easily get very complicated. But once you know the rules of engagement, you’ll soon see that they’re very doable. Before implementing a subquery, ask yourself the following:
- Where am I starting?
- Where am I going?
If you can answer these two questions, you’ve won half the battle. The other half is determining what steps need to take place to get from point A (existing data) to point B (desired data). In this section, we will learn how to navigate multi-step queries with...