A worked example, rendered from real sample data. Sign in to run the tool on your own input.
SELECT u.id, u.name, u.email, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.active = true AND u.created_at > '2024-01-01' GROUP BY u.id, u.name, u.email HAVING COUNT(o.id) > 5 ORDER BY order_count DESC LIMIT 10;SELECT
u.id,
u.name,
u.email,
COUNT(o.id) AS order_count
FROM
users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE
u.active = TRUE
AND u.created_at > '2024-01-01'
GROUP BY
u.id,
u.name,
u.email
HAVING
COUNT(o.id) > 5
ORDER BY
order_count DESC
LIMIT
10;
Format and prettify SQL queries. Part of the DevTools Surf developer suite. Browse more tools in the Generators collection.