First-Touch vs Last-Touch Attribution

SQL practice question · Hard · Analytics · Google

Each user converts at most once. A touch is eligible for a conversion when touch_ts is at or before conv_ts and no more than 30 days earlier (touch_ts >= conv_ts minus 30 days). First-touch gives the full revenue to the earliest eligible touch, last-touch to the latest eligible touch; when timestamps tie, the smaller touch_id is earlier. Conversions with no eligible touch are unattributed. For every channel that has at least one eligible touch, return channel, first_touch_revenue and last_touch_revenue (ROUND to 2, 0 if none). Order by channel.

Table: ad_touches

Schema of ad_touches
ColumnType
touch_idINTEGER
user_idINTEGER
channelTEXT
touch_tsTIMESTAMP
Sample rows of ad_touches
touch_iduser_idchanneltouch_ts
11search2026-04-01 10:00:00
21display2026-04-03 12:00:00
31email2026-04-05 09:00:00
42social2026-04-02 08:00:00
52search2026-04-02 09:00:00
63email2026-04-04 10:00:00
74display2026-04-06 10:00:00
85social2026-04-07 09:00:00

Table: conversions

Schema of conversions
ColumnType
user_idINTEGER
conv_tsTIMESTAMP
revenueREAL
Sample rows of conversions
user_idconv_tsrevenue
12026-04-05 10:00:001200
22026-04-02 10:00:00300
32026-04-04 18:00:00500
52026-04-07 10:00:00150
62026-04-08 12:00:0080
Expected output on the sample data
channelfirst_touch_revenuelast_touch_revenue
display00
email5001700
search1350300
social300150

Solve it in the live editor on this page — run on sample data, then submit against hidden tests.

‹ Overlapping Licence Subscriptions · First Day Below Reorder Point ›

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Question Asked in Interview Blog
SQL Interview Practice

Company-wise SQL questions

{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.

🏆 Weekly SQL Contest
{{ totalCount }}
Questions
{{ companyCount }}
Companies
{{ prog.total.solved }}
Solved
Bookmark Question Companies Difficulty Category Status
{{ r.n }} - {{ r.title }} {{ c }} {{ r.difficulty }} {{ r.category }} {{ r.statusIcon }}
No questions match these filters.
Showing {{ rangeLabel }} of {{ filteredCount }}

Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.

← All questions

{{ cur.n }} - {{ cur.title }}

🏆 Contest {{ contestWeek }}
{{ cur.catLabel }} {{ cur.diffLabel }} {{ c }}
Embed this questionPaste on a blog, course page or LMS. Free to use — educator guide
{{ embedCode }}

{{ cur.statement }}

Table: {{ tb.name }}
ColumnType {{ c.name }}{{ c.type }}
{{ h }} {{ v.text }}
Expected output on the sample data:
{{ h }} {{ v.text }}
  • {{ h }}

No submissions yet. Press Evaluate to submit your query.

{{ s.verdict }} {{ s.meta }}
The solution unlocks after you solve this question or after 3 evaluations that didn't pass ({{ failCount }}/3 so far). Trying first is the fastest way to learn.
{{ solutionSql }}
  1. {{ e }}
⏱ {{ timerLabel }}
{{ fnHelpTitle }}
{{ fnHelpText }}
Writing {{ dialectName }} · Ctrl/Cmd + Enter runs the query · Tab indents · your code is saved automatically
Output {{ out.status }}
  • {{ w }}
{{ out.verdict }}
{{ out.detail }}
{{ out.error }}
Expected (sample data)
{{ h }}{{ v.text }}
Your output (sample data)
{{ h }}{{ v.text }}
{{ h }} {{ v.text }}
Run your query to see the result here.
🔒

To Track Your Progress, Log In / Sign Up

You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.

Sign Up Free Log In