SQL for Lottery Analysis series
- Part 1: Count how often each number is drawn
- Part 2: Find "overdue" numbers with window functions (you are here)
- Part 3: Measure jackpot rollover streaks
- Part 4: Same number, same day, different states
- Part 5: Test it all: why none of it predicts a draw
Free practice data: SQLite database · Powerball CSV · Pick 3/Pick 4 CSV
An "overdue" number is one that hasn't been drawn for a while. Players watch them closely, and our own gap analysis pages track them. This part shows how to find them with SQL, using two tools that come up constantly in data work: common table expressions and window functions. It uses the same sample database as Part 1.
Step 1: Recreate the white_balls view
If you're starting from a fresh download, run this first. It's the view from Part 1 that lists every white ball on its own row. IF NOT EXISTS makes it safe to run twice.
CREATE VIEW IF NOT EXISTS white_balls AS
SELECT draw_date, n1 AS ball FROM powerball_draws
UNION ALL SELECT draw_date, n2 FROM powerball_draws
UNION ALL SELECT draw_date, n3 FROM powerball_draws
UNION ALL SELECT draw_date, n4 FROM powerball_draws
UNION ALL SELECT draw_date, n5 FROM powerball_draws;
Step 2: When was each ball last drawn?
Grouping by ball and taking the latest date gives a first answer:
SELECT ball, MAX(draw_date) AS last_drawn
FROM white_balls
GROUP BY ball
ORDER BY last_drawn
LIMIT 5;Result:
| ball | last_drawn |
|---|---|
| 63 | 2026-08-15 |
| 10 | 2026-09-02 |
| 39 | 2026-09-07 |
| 20 | 2026-09-14 |
| 30 | 2026-09-23 |
This has a catch. Dates measure time, but what matters in a lottery is the number of draws. Powerball added Monday drawings in August 2021 (Wikipedia), so a month now holds about 13 draws instead of about 9. Counting draws keeps old and new gaps comparable.
Step 3: Number the draws with ROW_NUMBER()
A window function calculates a value for each row by looking at other rows, without collapsing them into groups the way GROUP BY does. ROW_NUMBER() OVER (ORDER BY draw_date) numbers the draws 1, 2, 3 and so on in date order (SQLite documentation):
SELECT draw_date,
ROW_NUMBER() OVER (ORDER BY draw_date) AS draw_seq
FROM powerball_draws
ORDER BY draw_date DESC
LIMIT 3;Result:
| draw_date | draw_seq |
|---|---|
| 2026-11-30 | 1440 |
| 2026-11-28 | 1439 |
| 2026-11-25 | 1438 |
Step 4: Count draws since each ball was last seen
Long queries are easier to read in steps. A WITH clause, also called a common table expression or CTE, names an intermediate result so the next step can use it like a table (SQLite documentation). Here numbered gives every draw its sequence number, hits joins that number onto every white ball, and the final step subtracts each ball's last appearance from the latest draw number.
WITH numbered AS (
SELECT draw_date,
ROW_NUMBER() OVER (ORDER BY draw_date) AS draw_seq
FROM powerball_draws
),
hits AS (
SELECT n.draw_seq, w.ball
FROM white_balls AS w
JOIN numbered AS n ON n.draw_date = w.draw_date
)
SELECT ball,
(SELECT MAX(draw_seq) FROM numbered) - MAX(draw_seq) AS draws_since_drawn
FROM hits
GROUP BY ball
ORDER BY draws_since_drawn DESC
LIMIT 10;Result:
| ball | draws_since_drawn |
|---|---|
| 63 | 46 |
| 10 | 38 |
| 39 | 36 |
| 20 | 33 |
| 30 | 29 |
| 52 | 28 |
| 6 | 26 |
| 59 | 25 |
| 65 | 23 |
| 14 | 23 |
As of the November 30, 2026 drawing, ball 63 had gone 46 draws without appearing, the longest current drought. Your results will change if you use a newer download.
Step 5: Every gap, with LAG()
A single drought means more with history next to it. LAG(draw_seq) returns the value from the previous row, and PARTITION BY ball restarts the window for each ball, so subtracting gives the gap between one appearance of a ball and its next.
WITH numbered AS (
SELECT draw_date,
ROW_NUMBER() OVER (ORDER BY draw_date) AS draw_seq
FROM powerball_draws
),
hits AS (
SELECT n.draw_seq, w.ball
FROM white_balls AS w
JOIN numbered AS n ON n.draw_date = w.draw_date
),
gaps AS (
SELECT ball,
draw_seq - LAG(draw_seq) OVER (PARTITION BY ball ORDER BY draw_seq) AS gap
FROM hits
)
SELECT ball,
ROUND(AVG(gap), 1) AS average_gap,
MAX(gap) AS longest_gap
FROM gaps
WHERE gap IS NOT NULL
GROUP BY ball
ORDER BY longest_gap DESC, ball
LIMIT 5;Result:
| ball | average_gap | longest_gap |
|---|---|---|
| 34 | 16.3 | 112 |
| 35 | 14.8 | 112 |
| 45 | 13.7 | 109 |
| 14 | 14.6 | 97 |
| 46 | 17 | 91 |
The longest gap in the table is 112 draws. A summary of all the gaps puts the current drought in context:
WITH numbered AS (
SELECT draw_date,
ROW_NUMBER() OVER (ORDER BY draw_date) AS draw_seq
FROM powerball_draws
),
hits AS (
SELECT n.draw_seq, w.ball
FROM white_balls AS w
JOIN numbered AS n ON n.draw_date = w.draw_date
),
gaps AS (
SELECT ball,
draw_seq - LAG(draw_seq) OVER (PARTITION BY ball ORDER BY draw_seq) AS gap
FROM hits
)
SELECT COUNT(*) AS gaps,
ROUND(AVG(gap), 2) AS average_gap,
ROUND(69.0 / 5, 2) AS expected_gap,
SUM(gap >= 46) AS gaps_of_46_or_more
FROM gaps
WHERE gap IS NOT NULL;Result:
| gaps | average_gap | expected_gap | gaps_of_46_or_more |
|---|---|---|---|
| 7131 | 13.68 | 13.8 | 239 |
What the numbers say
The average gap, 13.68 draws across 7,131 gaps, is almost exactly what chance predicts. Each draw picks 5 of 69 white balls, so a given ball has a 5 in 69 chance of appearing in any draw, and the average wait for something with that chance is 69 ÷ 5 = 13.8 draws.
The last column is the key one. Droughts of 46 draws or longer, as long as or longer than ball 63's, have happened 239 times in this table. Long gaps are a normal part of random draws, not a sign that a ball is about to turn up. Each draw is independent, so ball 63 has the same 5 in 69 chance in the next draw as every other ball. Believing otherwise is known as the gambler's fallacy (Wikipedia), and our post on "due" numbers covers it in depth. Part 5 puts it to the test with this data.
The red ball
The same idea works for the red Powerball. Because it's one column, no view is needed:
WITH numbered AS (
SELECT draw_date, powerball,
ROW_NUMBER() OVER (ORDER BY draw_date) AS draw_seq
FROM powerball_draws
)
SELECT powerball,
MAX(draw_date) AS last_drawn,
(SELECT MAX(draw_seq) FROM numbered) - MAX(draw_seq) AS draws_since_drawn
FROM numbered
GROUP BY powerball
ORDER BY draws_since_drawn DESC
LIMIT 5;Result:
| powerball | last_drawn | draws_since_drawn |
|---|---|---|
| 26 | 2026-05-23 | 82 |
| 6 | 2026-07-01 | 65 |
| 16 | 2026-07-15 | 59 |
| 8 | 2026-07-20 | 57 |
| 18 | 2026-08-29 | 40 |
Red ball 26 had gone 82 draws. With one red ball drawn from 26, the average gap is 26 draws, so this drought is long, but no more meaningful than the white-ball ones.
Try it yourself
- Change the Step 5 query to find the balls with the shortest average gaps. (Hint:
ORDER BY average_gap.) - Find the dates of the 112-draw gaps by keeping
draw_seqin thegapsstep and joining back tonumbered. - Count how many gaps fall in each range (1 to 9, 10 to 19, and so on) with a
CASEexpression. Part 3 shows how.
About the practice data
The sample files come from the DrawAnalytics results database, the same records behind our Powerball pages and state results pages. They're free to use; if you publish something made with them, please credit DrawAnalytics with a link.
- powerball_draws: all 1,440 Powerball draws from October 7, 2015, the first drawing under today's 5-of-69 plus 1-of-26 format, to November 30, 2026. One row per draw: the five white balls (
n1ton5, smallest first), the redpowerball, the Power Play multiplier and the advertisedjackpotfor that draw. - pick_draws: the 50,956 Pick 3 and Pick 4 style draws from 2025 in our database, across every state game we track, one row per draw, with the number kept as text so a leading zero stays (
'073').
Two things are worth knowing. The jackpot column is the jackpot advertised for each draw; for a draw where the jackpot was won, it's the amount the lottery announced, which can be a little higher than the estimate before the draw. And a few individual draws in our database have been wrong before, so for anything that matters, such as checking a ticket, use the lottery's official results. Powerball's are at powerball.com. Developers who want more data can use our free results API.
Next: Part 3: Measure jackpot rollover streaks
Sources
- Window Functions, SQLite documentation
- The WITH Clause, SQLite documentation
- Powerball, Wikipedia
- Gambler's fallacy, Wikipedia
- Previous Results, Powerball.com
DrawAnalytics is an informational service. We do not sell predictions or guarantee outcomes. Lottery drawings are random — past results do not predict future drawings. Play responsibly. 1-800-GAMBLER.