SQL BEGIN

SQL BEGIN helps analysts open an explicit transaction before related statements run.

What & Why

The BEGIN pattern is used to open an explicit transaction before related statements run. 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

Marketing wants to identify the exact active campaign rows before beginning one atomic change.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
-- BEGIN; opens the transaction before the write statements.
SELECT
  id AS campaign_id,
  name,
  status
FROM marketing.campaigns
WHERE status = 'active'
ORDER BY campaign_id;
RESULT — active campaigns before a transaction
campaign_idnamestatus
1Spring Launchactive
2Retention Webinaractive
3Finance Retargetingactive
4Enterprise Searchactive

The read-only example establishes the state a transaction could begin from.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario