03
Subqueries
&
CTEs
Query
composition
Scalar
and
derived
subqueries
Subqueries
in
the
SELECT
,
FROM
,
and
WHERE
clauses
,
including
inline
views
as
derived
tables
.
Correlated
subqueries
Subqueries
that
reference
the
outer
row
—
EXISTS
and
NOT EXISTS
for
anti
-
join
semantics
.
Common
table
expressions
WITH
blocks
that
break
a
query
into
readable
stages
,
plus
multiple
CTEs
chained
in
one
statement
.
Recursive
CTEs
WITH RECURSIVE
for
org
charts
,
bill
-
of
-
materials
,
and
graph
traversal
,
with
a
termination
condition
.
04
Aggregation
&
Window
Functions
Analytics
Aggregate
functions
COUNT(*)
versus
COUNT(column)
,
SUM
,
AVG
,
MIN
,
MAX
,
and
conditional
aggregation
with
CASE
.
Window
functions
OVER (PARTITION BY … ORDER BY …)
and
the
distinction
between
a
window
frame
and
a
GROUP BY
.
Ranking
functions
ROW_NUMBER()
,
RANK()
,
DENSE_RANK()
,
NTILE()
—
and
choosing
correctly
when
there
are
ties
.
Offset
and
running
calculations
LAG()
,
LEAD()
,
and
cumulative
sums
using
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
.
Multi
-
level
aggregation
GROUPING SETS
,
ROLLUP
,
and
CUBE
for
subtotaled
reporting
output
in
a
single
pass
.
Pivot
and
unpivot
Reshaping
rows
into
columns
and
back
—
natively
in
some
engines
,
via
conditional
aggregation
in
others
.
3 / 8