Find Users Whose Subscriptions Expire Within the Next 7 Days

Quick Overview

Write a SQL query that lists users whose subscription expires between a fixed date and seven days later, with both ends included, sorted by expiry date and user id. It tests inclusive date ranges, NULL handling and an index-friendly filter on a table with tens of millions of rows.

Find Users Whose Subscriptions Expire Within the Next 7 Days

Company: Wix

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Return all users whose subscription expires within the next 7 days, treating today as the fixed date `2024-06-01`. An expiry date of today or exactly 7 days later is included. Return `user_id`, `name` and `subscription_expiry_date`, ordered by `subscription_expiry_date` ascending, then `user_id` ascending. ```hint Check both ends Write down the first and the last expiry date that should appear, then confirm your filter keeps both of them. ``` ### Input Table(s) `users` | Column | Type | Meaning | |---|---|---| | `user_id` | INT | User identifier | | `name` | VARCHAR(100) | User's name, never NULL | | `subscription_expiry_date` | DATE | Day the subscription expires, may be NULL | Primary key: `user_id`. ### Output Contract - Columns, in this order: `user_id`, `name`, `subscription_expiry_date`. - One row per qualifying user, with the values copied unchanged from `users`. - Sort by `subscription_expiry_date` ascending, then `user_id` ascending. `user_id` is unique, so the order is fully determined. - There are no computed columns, so no rounding applies. ### Example Input `users`: | user_id | name | subscription_expiry_date | |---|---|---| | 1 | Alice | 2024-06-01 | | 2 | Bob | 2024-06-05 | | 3 | Carol | 2024-06-08 | | 4 | Dave | 2024-06-09 | | 5 | Eve | 2024-05-31 | Expected output: | user_id | name | subscription_expiry_date | |---|---|---| | 1 | Alice | 2024-06-01 | | 2 | Bob | 2024-06-05 | | 3 | Carol | 2024-06-08 | Alice expires today, Bob within the window and Carol exactly 7 days from today, so all three are included. Dave is 8 days out and Eve expired yesterday, so both are excluded. ### Constraints and Clarifications - `users` has between 1 and 50 million rows. - `subscription_expiry_date` may be NULL. Those users are excluded. - Today is always `2024-06-01`, so the window runs from 2024-06-01 through 2024-06-08, both days included. Use the literal date; the server's current date would return different rows on different days. - Users whose subscription already expired (before 2024-06-01) are excluded. - `user_id` is the primary key, so no user appears twice.

Overview: Write a SQL query that lists users whose subscription expires between a fixed date and seven days later, with both ends included, sorted by expiry date and user id. It tests inclusive date ranges, NULL handling and an index-friendly filter on a table with tens of millions of rows.

Wix logo
Wix
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Return all users whose subscription expires within the next 7 days, treating today as the fixed date 2024-06-01. An expiry date of today or exactly 7 days later is included. Return user_id, name and subscription_expiry_date, ordered by subscription_expiry_date ascending, then user_id ascending.

Input Table(s)

users

ColumnTypeMeaning
user_idINTUser identifier
nameVARCHAR(100)User's name, never NULL
subscription_expiry_dateDATEDay the subscription expires, may be NULL

Primary key: user_id.

Output Contract

  • Columns, in this order: user_id , name , subscription_expiry_date .
  • One row per qualifying user, with the values copied unchanged from users .
  • Sort by subscription_expiry_date ascending, then user_id ascending. user_id is unique, so the order is fully determined.
  • There are no computed columns, so no rounding applies.

Example

Input users:

user_idnamesubscription_expiry_date
1Alice2024-06-01
2Bob2024-06-05
3Carol2024-06-08
4Dave2024-06-09
5Eve2024-05-31

Expected output:

user_idnamesubscription_expiry_date
1Alice2024-06-01
2Bob2024-06-05
3Carol2024-06-08

Alice expires today, Bob within the window and Carol exactly 7 days from today, so all three are included. Dave is 8 days out and Eve expired yesterday, so both are excluded.

Constraints and Clarifications

  • users has between 1 and 50 million rows.
  • subscription_expiry_date may be NULL. Those users are excluded.
  • Today is always 2024-06-01 , so the window runs from 2024-06-01 through 2024-06-08, both days included. Use the literal date; the server's current date would return different rows on different days.
  • Users whose subscription already expired (before 2024-06-01) are excluded.
  • user_id is the primary key, so no user appears twice.