← Back to Blog

SQL for Lottery Analysis, Part 2: Finding Overdue Numbers

December 2, 2026  ·  9 min read  ·  Education

SQL for Lottery Analysis series

  1. Part 1: Count how often each number is drawn
  2. Part 2: Find "overdue" numbers with window functions (you are here)
  3. Part 3: Measure jackpot rollover streaks
  4. Part 4: Same number, same day, different states
  5. 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:

balllast_drawn
632026-08-15
102026-09-02
392026-09-07
202026-09-14
302026-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_datedraw_seq
2026-11-301440
2026-11-281439
2026-11-251438

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:

balldraws_since_drawn
6346
1038
3936
2033
3029
5228
626
5925
6523
1423

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:

ballaverage_gaplongest_gap
3416.3112
3514.8112
4513.7109
1414.697
461791

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:

gapsaverage_gapexpected_gapgaps_of_46_or_more
713113.6813.8239

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:

powerballlast_drawndraws_since_drawn
262026-05-2382
62026-07-0165
162026-07-1559
82026-07-2057
182026-08-2940

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

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.

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

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.

Need to talk to someone about gambling?

Free, confidential help is available 24/7. Call or text 1-800-GAMBLER (1-800-426-2537) or visit ncpgambling.org.

DrawAnalytics is an informational and entertainment service. We provide historical lottery data analysis and pattern exploration tools. We do not sell predictions, we do not guarantee any outcome, and we make no representation that any tool on this site improves a user's probability of winning any lottery game. Lottery drawings are random. Past results do not predict future drawings. DrawAnalytics is independent and is not affiliated with or endorsed by any state lottery, the Multi-State Lottery Association, Powerball or Mega Millions. Game names are trademarks of their respective owners. Results are collected from public sources and may contain errors; official lottery results prevail. You must be 18 or older (21+ in some states) to play state lottery games. If you or someone you know has a gambling problem, call 1-800-GAMBLER or visit ncpgambling.org.