Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Structure Rules (ST)


ST03

CTEs that are defined but never referenced.

An unused CTE is dead code — it adds query planning overhead and confuses readers.

-- bad: 'unused_cte' is never referenced
with
    used_cte as (select id from orders),
    unused_cte as (select id from customers)
select id from used_cte

-- good
with used_cte as (
    select id from orders
)
select id from used_cte

ST08

COUNT(DISTINCT *) is not valid SQL.

DISTINCT * inside COUNT is not standard SQL and is rejected by most databases. Use COUNT(DISTINCT col) with an explicit column name instead.

-- bad
select count(distinct *) from orders

-- good
select count(distinct id) from orders