Library/SQL that runs/SQL in the interview

SQL window functions

100 min6 partsCoreFundamentalsChecked Nov 2026
Start the lessonShort steps, each with something to do
What you'll learn
  • What a window function keeps that GROUP BY collapses, and what PARTITION BY, ORDER BY and the frame each do inside OVER
  • ROW_NUMBER, RANK and DENSE_RANK on ties, and the tie-break that makes a top N come out the same on every run
  • LAG and LEAD in date order, without dropping the rows that have no next row
  • Running totals, the default frame, and why rows on the same date share one total
  • ROWS versus RANGE, and a 7-day window by date with no off-by-one
  • SQLite's text dates and julianday(), plus calendar tables and LEFT JOIN for the days and users that have no rows
Part 1 of 6about 14 min
Windows

Four queries the database grades

In a live SQL round the database grades you: the query runs, and the rows it returns are your answer. One data scientist who interviewed at Netflix in October 2025 reported that the first technical round was "pure SQL, had to actually run" (1 report), and that the second paired SQL with a case study (1 report). A report from an OpenAI data scientist loop, published in September 2026, describes 20 to 25 minutes of hands-on SQL in the live screen (1 report). A prep site's question bank lists an Airbnb screen question, linked to an October 2025 report, that asks you to "build a user-day panel for the inclusive date range" (1 source). Each is a single report, so read them as signals, not a syllabus. Our version of the practice reads like this:

A SQLite table bookings(booking_id, guest_id, listing_id, city, booked_on, nights, price) holds one row per booking, with booked_on as ISO text. Write four queries: each city's top 2 listings by revenue, each guest's days to their second booking, each city's bookings in the 7 days ending on each date, and a guest-day panel with a row for every guest on every day in the data's range.

The practice comes in four levels, one query each. Level 1 is TOP_LISTINGS_BY_CITY, ties to the lower listing_id. Level 2 is DAYS_TO_SECOND_BOOKING, NULL for guests with one booking. Level 3 is ROLLING_7D. Level 4 is GUEST_DAY_PANEL, with 0 on the days a guest didn't book. Each level has a broken version that runs, returns plausible numbers and fails.

You don't write them yet. The lesson shows every trap on a small table of its own: ties, dates stored as text, frames, and the days with no rows. At the end you solve the whole problem, level by level, in an interview simulation with a 40-minute timer.

This lesson needs no other lesson first. The lesson after it reads an AI's SQL for the same traps.

The lesson's table: seven orders from four users

Every query in this lesson runs on one small table, built so each trap shows.

order_iduser_idordered_onamount
112026-03-0130
222026-03-0120
312026-03-0345
432026-03-0325
522026-03-0935
612026-03-0245
742026-03-1050

It's small on purpose. Here are the same orders laid out by the day they happened:

Each order as #id, user, amount: #1 u1 30 is order 1, user 1, 30.

Mar 01Mar 02Mar 03Mar 04 to 08Mar 09Mar 10
#1 u1 30#6 u1 45#3 u1 45no orders#5 u2 35#7 u4 50
#2 u2 20#4 u3 25

Each odd detail breaks a query later in the lesson:

  • Order 6 was entered after orders 1 to 5, but it happened on March 2, between user 1's other two orders.
  • User 1 has two orders of 45.
  • March 1 and March 3 each have two orders.
  • Nothing happens from March 4 to March 8.

To follow along, load it into SQLite from Python. The practice tests run your queries the same way:

import sqlite3

con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE orders (order_id INTEGER, user_id INTEGER, "
            "ordered_on TEXT, amount INTEGER)")
con.executemany("INSERT INTO orders VALUES (?, ?, ?, ?)", [
    (1, 1, "2026-03-01", 30), (2, 2, "2026-03-01", 20),
    (3, 1, "2026-03-03", 45), (4, 3, "2026-03-03", 25),
    (5, 2, "2026-03-09", 35), (6, 1, "2026-03-02", 45),
    (7, 4, "2026-03-10", 50),
])
print(sqlite3.sqlite_version)
print(con.execute("""
    SELECT user_id, COUNT(*) FROM orders
    GROUP BY user_id ORDER BY user_id
""").fetchall())
3.53.4
[(1, 3), (2, 2), (3, 1), (4, 1)]

The first line is the SQLite version. Every query and every result in this lesson comes from this engine. Where engines differ, on frames, ties or dates, the lesson says which one it means.

The tests compare a list of tuples like the second line with the expected list. So the column order and the final ORDER BY are part of your answer.

From GROUP BY to OVER

The loader's query was a GROUP BY, and it collapsed each user's orders into one row:

SELECT user_id, COUNT(*) AS orders, SUM(amount) AS spend
FROM orders
GROUP BY user_id
ORDER BY user_id;
user_idordersspend
13120
2255
3125
4150

You get the totals and lose the orders. Now put OVER after the same SUM.

Predict

GROUP BY user_id returned 4 rows. How many rows does SUM(amount) OVER (PARTITION BY user_id) return?

Pick an answer above to see the rest

OVER has up to three parts: the partition, the order and the frame

Here's a window that uses all three. It counts each user's orders so far, order by order.

Predict

Order 2 is user 2's first order, on March 1, the same day as user 1's order 1. What's orders_so_far on order 2?

Pick an answer above to see the rest

The order a query runs in

The order the database runs a query in decides what each clause can see.

Predict

Can WHERE filter on a window function's result in the same SELECT?

Pick an answer above to see the rest
Part 1 done
Part 2 of 6about 14 min
Ranking

ROW_NUMBER, RANK and DENSE_RANK differ only on ties

All three number the rows in the window's order. Here they number the orders by date, and the dates have two ties: two orders on March 1 and two on March 3.

Predict

Orders 1 and 2 are both on March 1, and order 6 is on March 2. Under RANK by date, which numbers do orders 1, 2 and 6 get?

Pick an answer above to see the rest

Ties under ROW_NUMBER

Number the orders by amount alone, with ROW_NUMBER() OVER (ORDER BY amount DESC), and orders 3 and 6 tie at 45.

Predict

Two rows tie on the window's ORDER BY. Which one does ROW_NUMBER number first?

Pick an answer above to see the rest

RANK and DENSE_RANK keep ties at the cutoff, so pick the function your question means

Rank the amounts with RANK instead of ROW_NUMBER, and a top 2 comes back as three rows, because both 45s rank 2:

WITH ranked AS (
    SELECT order_id, amount,
           RANK() OVER (ORDER BY amount DESC) AS rnk
    FROM orders
)
SELECT order_id, amount, rnk
FROM ranked
WHERE rnk <= 2
ORDER BY rnk, order_id;
order_idamountrnk
7501
3452
6452

Here is the same cutoff, <= 2, under each of the three:

OrderAmountROW_NUMBER, tie-break by idRANKDENSE_RANK
#7501, kept1, kept1, kept
#3452, kept2, kept2, kept
#6453, cut2, kept2, kept
#5354, cut4, cut3, cut
Rows kept233

Neither function is wrong. They answer different questions:

  • The two largest, ties to the lower id: ROW_NUMBER with the id in the ORDER BY.
  • Every order tied for a top-2 spot: RANK.
  • Every order at one of the two highest amounts: DENSE_RANK.

RANK and DENSE_RANK split when the tie is at the top:

A and B tie for first, and C has the next amount. Keep <= 2:

RANKDENSE_RANK
A1, kept1, kept
B1, kept1, kept
C3, cut2, kept

Both tied orders get RANK 1, and the next one gets 3, so rank <= 2 stops at the pair. DENSE_RANK gives the next amount a 2 and keeps it.

In the editor, write TOP_TWO_AMOUNTS: every order at one of the two highest amounts, the third question in the list.

PARTITION BY restarts the numbering in each group

Each user's orders, oldest first:

SELECT user_id, order_id, ordered_on,
       ROW_NUMBER() OVER (PARTITION BY user_id
                          ORDER BY ordered_on, order_id) AS rn
FROM orders
ORDER BY user_id, rn;
user_idorder_idordered_onrn
112026-03-011
162026-03-022
132026-03-033
222026-03-011
252026-03-092
342026-03-031
472026-03-101

Each user starts again at 1, and rn = 1 is each user's first order. Sort by the amount, largest first, and rn = 1 is each user's largest order instead. User 1 has two orders of 45, so the tie-break decides which one.

In the editor, finish LARGEST_ORDER: each user's largest order, ties to the lower order_id.

CheckpointEach user's top order, ties settled

On this lesson's orders table, finish LARGEST_ORDER: each user's largest order, ties to the lower order_id. Fill in the window that numbers each user's orders and the filter that keeps the top one. One row per user, ordered by user_id.

Part 2 done
Part 3 of 6about 18 min
LAG and dates

LAG and LEAD read earlier and later rows in the window's order

Some questions compare a row with the one before it.

Predict

What does LAG(ordered_on) return on a user's first order?

Pick an answer above to see the rest

Sort by the date, not the id, because ids can be out of date order

The window sorts by the date and uses the id only to break ties within a date. Sort by the id alone, and user 1 travels back in time:

SELECT user_id, order_id, ordered_on,
       LAG(ordered_on) OVER w AS prev_on,
       julianday(ordered_on) - julianday(LAG(ordered_on) OVER w) AS gap_days
FROM orders
WINDOW w AS (PARTITION BY user_id ORDER BY order_id)
ORDER BY user_id, order_id;
user_idorder_idordered_onprev_ongap_days
112026-03-01NULLNULL
132026-03-032026-03-012.0
162026-03-022026-03-03-1.0
222026-03-01NULLNULL
252026-03-092026-03-018.0
342026-03-03NULLNULL
472026-03-10NULLNULL

User 1's three orders, in the two orders the windows used:

ORDER BY order_idGapORDER BY ordered_on, order_idGap
#1, 03-01#1, 03-01
#3, 03-032.0#6, 03-021.0
#6, 03-02-1.0, backwards#3, 03-031.0

Order 6 was written after order 3, but it happened the day before. An id follows the order rows were inserted in, not the order events happened in. Backfills and late-arriving data pull the two apart. Order by the event's date.

In the editor, write PREVIOUS_ORDER: each order's previous order, by date.

Days to the second order: keep row 1, and keep the NULLs

Now the days from each user's first order to their second. Number the rows, read the next date with LEAD, and keep row 1:

SELECT user_id, days FROM (
    SELECT user_id,
           CAST(julianday(LEAD(ordered_on) OVER w) - julianday(ordered_on)
                AS INTEGER) AS days,
           ROW_NUMBER() OVER w AS n
    FROM orders
    WINDOW w AS (PARTITION BY user_id ORDER BY ordered_on, order_id)
)
WHERE n = 1
ORDER BY user_id;
user_iddays
11
28
3NULL
4NULL

Users 3 and 4 ordered once. LEAD finds no next row, so their days are NULL. And NULL is the right answer: the question asks for every user, and no second order is what happened.

Filter on the row number, never on the value. Change the filter to WHERE n = 1 AND days IS NOT NULL, and the same query returns only users 1 and 2.

In the editor, write DAYS_TO_NEXT: the days until each user's next order, NULL on their last.

CheckpointDays until the next order

Write DAYS_TO_NEXT: for every order, the whole days until the same user's next order by date, and NULL on each user's last order. Columns order_id, days, ordered by order_id.

SQLite has no date type, so do date math with julianday() and date()

SQLite has no date type. The practice stores dates as ISO text, YYYY-MM-DD. Each result in this section is what SELECT returns for that expression in SQLite 3.53.

How text dates sort

Predict

As text, which sorts first: '2026-3-10' or '2026-3-9'?

Pick an answer above to see the rest

Subtracting two dates in SQLite

The next trap is subtraction.

Predict

In SQLite, what's '2026-03-09' - '2026-03-03'?

Pick an answer above to see the rest

BETWEEN on a column of timestamps

Timestamps add one more trap.

Predict

Is '2026-03-03 14:00:00' BETWEEN '2026-03-02' AND '2026-03-03'?

Pick an answer above to see the rest
Part 3 done
Part 4 of 6about 23 min
Frames

The frame decides which rows an aggregate adds up

Three counts over the same rows.

Predict

Orders 1 and 2 are both on March 1. What's COUNT(*) OVER (ORDER BY ordered_on) on order 1?

Pick an answer above to see the rest

With no frame written, rows that tie on the ORDER BY share one total

Here are the two frames for order 1 from the last step:

Order 1's frameHoldsCount
running: no frame written#1 and #2, peers on March 12
running_rows: ROWS, a unique key#1 only1

That's the default frame: with ORDER BY and no frame, you get RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

RANGE treats rows with the same ORDER BY value as peers, and peers enter the frame together. PostgreSQL's default is the same.

running_rows steps one order at a time, because it says ROWS and sorts on a unique key. The unique key alone would do it: once no two rows tie there are no peers, and the default frame stops exactly at the current row.

Which one you want depends on the question. Revenue through the end of each day wants the peers in. The balance after each order wants ROWS and a tie-break. Don't let the default choose for you.

In the editor, write BALANCE: the running_rows idea with SUM(amount), the revenue one order at a time.

ROWS counts rows, and RANGE measures the ORDER BY value

A frame has a unit and two ends. The unit decides what PRECEDING and FOLLOWING count:

  • ROWS counts rows. 6 PRECEDING reaches back six rows, whatever dates they hold.
  • RANGE measures the ORDER BY value. 6 PRECEDING reaches back to every row whose value is at most 6 below the current row's.
  • GROUPS counts peer groups. 1 PRECEDING reaches back one group, so the frame holds the current row's peers and the set of peers just before them.

Here's the difference made visible. The query counts orders per date. Then for each date it sums the counts inside each frame, and lists which days of March the frame holds, with group_concat:

WITH daily AS (
    SELECT ordered_on, COUNT(*) AS n
    FROM orders
    GROUP BY ordered_on
)
SELECT ordered_on, n,
       group_concat(substr(ordered_on, 9), ' ') OVER by_rows AS rows_frame,
       SUM(n) OVER by_rows AS rows_sum,
       group_concat(substr(ordered_on, 9), ' ') OVER by_days AS range_frame,
       SUM(n) OVER by_days AS range_sum
FROM daily
WINDOW by_rows AS (ORDER BY julianday(ordered_on)
                   ROWS BETWEEN 6 PRECEDING AND CURRENT ROW),
       by_days AS (ORDER BY julianday(ordered_on)
                   RANGE BETWEEN 6 PRECEDING AND CURRENT ROW)
ORDER BY ordered_on;
ordered_onnrows_framerows_sumrange_framerange_sum
2026-03-012012012
2026-03-02101 02301 023
2026-03-03201 02 03501 02 035
2026-03-09101 02 03 09603 093
2026-03-10101 02 03 09 10709 102

Through March 3 they agree, because the first three dates are consecutive. March 9 is where they split. On a calendar:

6 PRECEDING for the row dated March 9
ROWS the row and the 6 before it reach back to March 1
RANGE day numbers within 6 of March 9: March 3 to 9
12345678910
day of March

The dashed lines mark the days that have a row.

The ROWS frame reaches back to March 1. It holds the current row and up to six before it, and only three rows come before it. The RANGE frame stops at March 3, six days back.

With gaps between dates, ROWS covers more than a week. With several rows per date, it covers less. Only RANGE over a day number means the last 7 days.

In the editor, write LAST_THREE_DATES: a ROWS frame on purpose, the orders over the last three dates that had any.

Watch · 13 min

Ankit Bansal runs ROWS and RANGE side by side, over prices and then over dates, where PostgreSQL wants an interval as the offset.

Rows Between vs Range Between in SQL Window Functions | Real Difference Explained with Examples · Ankit Bansal · Apr 2026YouTube

In SQLite, a 7-day window by date is RANGE 6 PRECEDING over a day number

The practice's third query wants, for every date with bookings, the bookings in the 7 days ending on that date. It takes two steps:

  1. Count per date, so each date is one row.
  2. Sum those counts over a RANGE frame on the day number.
WITH daily AS (
    SELECT ordered_on, COUNT(*) AS n
    FROM orders
    GROUP BY ordered_on
)
SELECT ordered_on,
       SUM(n) OVER (ORDER BY julianday(ordered_on)
                    RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS orders_7d,
       SUM(n) OVER (ORDER BY julianday(ordered_on)
                    RANGE BETWEEN 7 PRECEDING AND CURRENT ROW) AS orders_8d
FROM daily
ORDER BY ordered_on;
ordered_onorders_7dorders_8d
2026-03-0122
2026-03-0233
2026-03-0355
2026-03-0934
2026-03-1024

Count the days for March 9:

The frame for March 9, over the daily counts
6 PRECEDING 7 days, 3 orders
7 PRECEDING 8 days, 4 orders
23456789
day of March

Orders: 1 on March 2, 2 on March 3 and 1 on March 9, at the dashed lines.

The 7 days ending March 9 are March 3 through March 9: the date itself and the 6 before it. That's 6 PRECEDING.

For March 9, 7 PRECEDING starts on March 2, which makes eight days. It counts 4 on both March 9 and March 10, where the right answers are 3 and 2.

The same off-by-one hides in the last 30 days, which is 29 PRECEDING, and in every inclusive range. The number of days is the end minus the start, plus one.

In the editor, write WINDOW_START: the first day of each date's 7-day window, so you can see the frame's reach.

Two SQLite rules for a RANGE offset

Two SQLite rules decide whether that frame works at all.

Predict

In SQLite, can a RANGE offset ORDER BY the date text itself?

Pick an answer above to see the rest

DISTINCT over the raw rows gives the same counts with no CTE

Since peers share a frame, you can skip the CTE. Run the window over the raw rows, where every order on a date gets the same count. Then collapse the repeats with DISTINCT, which runs after the windows:

SELECT DISTINCT ordered_on,
       COUNT(*) OVER (ORDER BY julianday(ordered_on)
                      RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS orders_7d
FROM orders
ORDER BY ordered_on;
ordered_onorders_7d
2026-03-012
2026-03-023
2026-03-035
2026-03-093
2026-03-102

Both are correct, and the CTE version is easier to read.

In the editor, finish REVENUE_2D: the store's revenue over the 2 days ending on each date.

CheckpointA two-day window by date

Finish REVENUE_2D: for every date with orders, the store's revenue over the 2 days ending on that date, which is that date and the day before. daily already sums each date's revenue. Columns ordered_on, revenue_2d, ordered by date.

Part 4 done
Part 5 of 6about 19 min
Empty days

Days and users with no rows need rows of their own

A window only sees rows that exist. Nothing happened from March 4 to March 8, so those days have no rows to count, to show, or to line up in a ROWS frame. When the question needs every day, or every user, build those rows first and join the data onto them.

A recursive CTE builds a calendar

SQLite builds a calendar with a recursive CTE. Here it builds the five empty days.

Predict

The recursive step adds a day while day < '2026-03-08', starting from March 4. How many rows come out?

Pick an answer above to see the rest

LEFT JOIN onto the calendar, and then a ROWS frame counts days

Now join each day's revenue onto the calendar, and fill the gaps with 0:

WITH RECURSIVE days(day) AS (
    SELECT '2026-03-01'
    UNION ALL
    SELECT date(day, '+1 day') FROM days WHERE day < '2026-03-10'
), daily AS (
    SELECT ordered_on, SUM(amount) AS revenue
    FROM orders
    GROUP BY ordered_on
)
SELECT d.day,
       COALESCE(daily.revenue, 0) AS revenue,
       SUM(COALESCE(daily.revenue, 0)) OVER (
           ORDER BY d.day
           ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
       ) AS revenue_7d
FROM days AS d
LEFT JOIN daily ON daily.ordered_on = d.day
ORDER BY d.day;
dayrevenuerevenue_7d
2026-03-015050
2026-03-024595
2026-03-0370165
2026-03-040165
2026-03-050165
2026-03-060165
2026-03-070165
2026-03-080115
2026-03-0935105
2026-03-105085

A LEFT JOIN keeps every row of the table on its left, here every day, and fills the right side's columns with NULL where nothing matches. COALESCE(daily.revenue, 0) returns its first argument that isn't NULL. So a day the LEFT JOIN found no revenue for gets 0 instead of NULL.

Every day has a row now, with 0 where nothing happened. So ROWS BETWEEN 6 PRECEDING AND CURRENT ROW really is seven days. On March 9 the frame holds March 3 to 9: 70, five zeros and 35, which is 105.

That's the calendar-table answer to the practice problem's follow-up about days with no bookings. For several cities, cross join the cities with the calendar first, and partition the window by city.

In the editor, write DAILY_ORDERS: every day with its order count, 0 on the empty ones.

COUNT a column from the joined table, not *

Two habits keep the empty rows from vanishing again. The first: count a column from the joined table, not *.

Predict

After the LEFT JOIN, March 4 has one row whose order columns are all NULL. What does COUNT(*) give for March 4?

Pick an answer above to see the rest

Where a condition on the joined table goes

The second habit is about where a condition goes.

Predict

You LEFT JOIN days to orders, then filter o.amount >= 40 in WHERE. What happens to the days with no big orders?

Pick an answer above to see the rest

The user-day panel is every user times every day

The Airbnb screen question asks for a user-day panel over an inclusive date range (1 source): one row per user per day, whether or not the user did anything. It's the calendar move in two dimensions.

Predict

Five users over March 9 and 10, both ends included. How many rows does the panel have?

Pick an answer above to see the rest
Part 5 done
Part 6 of 6about 12 min
Follow-ups

A worked example

Put the pieces together on the orders table. For each order, find the user's order number, the days since their previous order, their running spend, and the store's orders in the 7 days ending on that date:

SELECT user_id, ordered_on, amount,
       ROW_NUMBER() OVER u AS nth,
       CAST(julianday(ordered_on) - julianday(LAG(ordered_on) OVER u)
            AS INTEGER) AS gap_days,
       SUM(amount) OVER u AS running_spend,
       COUNT(*) OVER (ORDER BY julianday(ordered_on)
                      RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS store_7d
FROM orders
WINDOW u AS (PARTITION BY user_id ORDER BY ordered_on, order_id)
ORDER BY user_id, nth;
user_idordered_onamountnthgap_daysrunning_spendstore_7d
12026-03-01301NULL302
12026-03-024521753
12026-03-0345311205
22026-03-01201NULL202
22026-03-093528553
32026-03-03251NULL255
42026-03-10501NULL502

Check one row by hand, user 2's order on March 9:

columnvaluewhy
nth2user 2's orders by date are March 1, then March 9
gap_days8March 1 to March 9
running_spend5520 + 35, and u sorts on a unique key, so the default frame stops at this order
store_7d3the window runs March 3 to 9: orders 3 and 4 on March 3, order 5 on March 9

Two windows with different partitions in one SELECT are fine, since each function uses its own OVER. store_7d has no PARTITION BY, so it counts the whole store. The two March 3 orders are peers, and they share a count of 5.

What interviewers ask next

The practice problem asks how you'd handle days with no bookings. Have that answer ready, and these others with it.

Include the ties: RANK keeps every tie, DENSE_RANK keeps the top levels

Switch ROW_NUMBER to RANK and drop the id from the ORDER BY. Then say what changes: a city can now return three or more listings.

If they mean the listings at the top two revenue levels, that's DENSE_RANK. Walk the difference with a tie for first: both tied listings get rank 1, and only DENSE_RANK gives the next listing a 2.

Show every date, even with no bookings: cross the cities with a calendar

Use the calendar table, crossed with the cities:

  1. Cities CROSS JOIN days.
  2. LEFT JOIN the daily counts.
  3. COALESCE to 0.
  4. PARTITION BY city.

With exactly one row per city per day, ROWS and RANGE give the same frame.

AVG over a 7-day frame

Averages over a window hide one more trap.

Predict

The week ending March 10 holds 2 orders, one on each of 2 days. What does AVG(n) over that RANGE frame report?

Pick an answer above to see the rest

LAST_VALUE and the default frame

LAST_VALUE looks like the easiest window function to use.

Predict

Under the default frame, what does LAST_VALUE(amount) return on user 1's first order?

Pick an answer above to see the rest

Without a window function, a self-join counts the same 7 days

A self-join spells out what the frame means:

SELECT d.ordered_on, COUNT(*) AS orders_7d
FROM (SELECT DISTINCT ordered_on FROM orders) AS d
JOIN orders AS o
  ON julianday(o.ordered_on)
     BETWEEN julianday(d.ordered_on) - 6 AND julianday(d.ordered_on)
GROUP BY d.ordered_on
ORDER BY d.ordered_on;
ordered_onorders_7d
2026-03-012
2026-03-023
2026-03-035
2026-03-093
2026-03-102

Same answers as the RANGE frame. The cost is in the join. Each date matches every order in its week, so the work grows with the number of dates times the orders per week. The window sorts once and moves its frame along.

It's a good second route for checking a window query you aren't sure of.

In the editor, write SELF_JOIN_7D: the same 7-day counts, with a self-join.

The practice problem, start to finish

That's every piece of the practice problem, and the follow-ups after it. The simulation asks for all four queries under a timer, so rehearse the order once.

Check yourself
5 questions
  1. 1.In Denver, listings 21 and 22 both earn 200, behind listing 20 at 270. You rank with RANK() OVER (PARTITION BY city ORDER BY revenue DESC) and keep rank <= 2. What comes back for Denver?

  2. 2.Guest 2 has booking 3 on 2026-01-04 and booking 5 on 2026-01-02. The window orders by booking_id alone. What does the days-to-second-booking query return for guest 2?

  3. 3.You add AND days IS NOT NULL to the outer WHERE of the days-to-second-booking query. What changes?

  4. 4.Austin's daily counts are 1 on January 1, 1 on January 2, 3 on January 4 and 1 on January 9. What does SUM(n) OVER (ORDER BY julianday(booked_on) ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) give for January 9?

  5. 5.The window orders by julianday(booked_on). Which frame counts January 3 through January 9 for the row dated January 9?

Where this shows up

The reports behind this lesson, each with its source.

Data scientist candidate (interviewed Oct 2025, rejected): the first technical round was pure SQL that had to run.

Candidate report·PracHub (first-person report, curated and edited by the site)·2026-08-25Source

Same report: the second technical round combined SQL with a case study.

Candidate report·PracHub (first-person report, curated and edited by the site)·2026-08-25Source

Same Sep 2026 DS report: the live technical screen included 20 to 25 minutes of hands-on SQL.

Candidate report·PracHub (first-person report, curated and edited by the site)·2026-09-28Source

DS technical screen SQL (same report): build a user-day panel in SQL.

Prep site·PracHub (prep site question bank)Source

Practice

Sources (4)
  1. Window Functions · SQLite documentation · May 9, 2025
    “The default means that aggregate window functions read all rows from the beginning of the partition up to and including the current row and its peers.”
  2. Date And Time Functions · SQLite documentation · Jul 11, 2026
    “If you want a precise time difference (in days or seconds) use the difference between two julianday() or unixepoch() calls.”
  3. 3.5. Window Functions · PostgreSQL 18 documentation · Sep 25, 2025
    “the row_number window function assigns sequential numbers to the rows within each partition, in the order defined by the ORDER BY clause (with tied rows numbered in an unspecified order)”
  4. 4.2.8. Window Function Calls · PostgreSQL 18 documentation · Sep 25, 2025
    “For numeric ordering columns it is typically of the same type as the ordering column, but for datetime ordering columns it is an interval.”