SQL ROLLBACK

SQL ROLLBACK helps analysts discard every uncommitted change in the current transaction.

What & Why

The ROLLBACK pattern is used to discard every uncommitted change in the current transaction. This keeps the SQL aligned with one concrete business question and makes the result grain explicit before the query is reused.

See How It Works

BUSINESS QUESTION

Growth finds churned user rows that fail validation and should cause the whole transaction to be discarded.

idcreated_atcountrychannelplanactivated_atchurned_at
1012024-01-15 09:10:00+00USorganicpro2024-01-16 14:25:00+00NULL
1022024-02-10 11:05:00+00CApaidfreeNULL2024-03-20 10:00:00+00
1032024-03-12 16:35:00+00GBreferralpro2024-03-13 08:15:00+00NULL
1042024-04-01 13:20:00+00USorganicfree2024-04-03 12:00:00+00NULL
EXAMPLE QUERY
-- ROLLBACK; discards every uncommitted statement since BEGIN.
SELECT
  id AS user_id,
  plan,
  churned_at
FROM growth.users
WHERE churned_at IS NOT NULL
ORDER BY user_id;
RESULT — users with recorded churn
user_idplanchurned_at
102free2024-03-20 10:00:00+00

Only user 102 has a persisted churn timestamp in the representative data.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario