Menu

1 / 2

What every `?` in your SQL is hiding

DEV Community·Eitamos Ring·6 months ago
#2uunebW6
Reading 0:00
15s threshold

Take a query that comes out of pg_stat_statements :

SELECT date_trunc ( ? , o . created_at ) AS week , count ( * ) AS total FROM orders o INNER JOIN customers c ON c . id = o . customer_id WHERE o . created_at >= ? AND o . amount > ? AND c . plan = ? GROUP BY ? ORDER BY 2 DESC LIMIT ?

Enter fullscreen mode

Exit fullscreen mode

Six question marks. Each one means something completely different.

The first, inside date_trunc , expects a string like 'week' — it's telling the function which time bucket to use. The second is a timestamp comparing against created_at . The third is a number comparing against amount . The fourth is a string joined through to the customers table — it has to match a plan value over there. The fifth, sitting bare inside GROUP BY , is a positional integer like 1 , pointing back at the first column in the SELECT list. It's not a value, it's an index .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.