Querydrill

Level 6: Work mode Exercise 139 of 161 Hard Clean messy data

Days from signup to first payment

An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.

Try it yourself

From Leo: "For each signup that has a paid payment, how many days passed between the signup and the first paid payment? Both dates come in mixed formats: a plain date, a date with a time, or a date with a T and a time." Return the signup id and the number of days as a whole number.

signups_raw (first 12 of 14 rows)

idemailcountryplansigned_up
1anna@mail.comUSPro2025-01-04
2 Anna@Mail.comus pro2025-01-09 08:30:00
3bruno@mail.comBRFREE2025-01-15T10:02:00
4chloe@mail.com UsPro2025-02-02
5dev@mail.comINNULL2025-02-11 17:45:00
6elif@mail.comtrfree2025-02-20
7farah@mail.comUSTeam2025-03-01T09:00:00
8CHLOE@mail.com USpro2025-03-05
9gus@mail.comnlFree2025-03-14
10hana@mail.comNULLPro2025-03-22 12:00:00
11ivo@mail.comNL team2025-04-02
12jun@mail.comNULL2025-04-10T16:20:00

payments_raw (first 12 of 16 rows)

idsignup_idamountstatuspaid_at
11$12.00paid2025-01-04
21$12.00Paid2025-02-04 08:00:00
34$1,200.00paid2025-02-02T11:00:00
43 45failed2025-01-16
5345PAID 2025-01-17
65$99refunded2025-02-12
77$1,200.00paid2025-03-01 09:05:00
8712.5 paid2025-03-15
98$12.00Failed2025-03-05
109 30.50paid2025-03-14T18:00:00
1110$12.00paid2025-03-22
1211$600Paid2025-04-02 10:10:00

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

What this exercise teaches

Cut the date part out first, then cast it to a real date. In PostgreSQL a plain minus between two dates gives the days between them (DATEDIFF in MySQL and SQL Server).

A hint if you get stuck

substr(x, 1, 10) gives the YYYY-MM-DD part of every format. Cast it to a date with ::date; one date minus another is a number of days. Take MIN of the paid date per signup first.

Background you need

Common mistakes on this exercise

  • Time of day not removed: part of a day is cut off.
  • No paid filter: failed and refunded payments counted.

Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.

Keep going on the practice page

Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.

Open this exercise

More practice on the same topic

More exercises in Work mode