mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-22 19:32:25 +01:00
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWXLvi9aq4MxL4XHKJ4
811 B
811 B
Skeleton: SQL queries (sql)
Signal: a table schema and a question about rows; joins, group-by, a filter before or after grouping. Trap: clauses run in this order: FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT; WHERE cannot see an aggregate or a select alias, HAVING can see the aggregate; a filter on the right table of a LEFT JOIN belongs in ON, not WHERE.
SELECT /* HOLE: columns, or an aggregate over each group */
FROM /* HOLE: main table */ AS a
LEFT JOIN /* HOLE: other table */ AS b
ON /* HOLE: join condition */
WHERE /* HOLE: row filter: runs before grouping, no aggregates */
GROUP BY /* HOLE: every selected column that is not aggregated */
HAVING /* HOLE: group filter: aggregates allowed */
ORDER BY /* HOLE: sort keys, ASC or DESC */
LIMIT /* HOLE: how many rows */;