sqlite3 data.db 'WITH s AS (SELECT dept, AVG(salary) AS a FROM employees GROUP BY dept) SELECT * FROM s WHERE a > 70000;'
Filter grouped results with a common table expression
A common table expression (WITH s AS ...) names a subquery so the outer query can filter its result, here keeping only departments whose average salary exceeds 70000. This two-step pattern is how you put a WHERE clause on an aggregate, since WHERE cannot directly follow GROUP BY. CTEs also make complex queries readable. The alias a refers to the computed average inside the outer query.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.