SQL for Lottery Analysis series
- Part 1: Count how often each number is drawn
- Part 2: Find "overdue" numbers with window functions
- 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 (you are here)
Free practice data: SQLite database · Powerball CSV · Pick 3/Pick 4 CSV
The first four parts measured the past: which numbers came up most, which have gone quiet, how long jackpots run and how often states match. The natural next question is whether any of it helps predict the next draw. This part tests that directly in SQL, on the same sample database. The answer is no, and the queries show why.
Step 1: Recreate the white_balls view
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;
Test 1: Is the frequency spread unusual?
Part 1 found white-ball counts from 80 to 128, against an expected 104.3 each. The standard check for "do these counts fit equal chances?" is the chi-square goodness-of-fit test (NIST). For each ball, take the difference between observed and expected, square it, divide by the expected count, and add up all 69 results. SQL does it in one query:
WITH counts AS (
SELECT ball, COUNT(*) AS observed
FROM white_balls
GROUP BY ball
),
expected AS (
SELECT COUNT(*) * 5.0 / 69 AS e
FROM powerball_draws
)
SELECT COUNT(*) AS balls,
ROUND(SUM((observed - e) * (observed - e) / e), 1) AS chi_square
FROM counts, expected;Result:
| balls | chi_square |
|---|---|
| 69 | 74.9 |
With 69 balls there are 68 degrees of freedom. The NIST table of critical values puts the 5% cut-off for 68 degrees of freedom at 88.250 (NIST). A fair draw produces a score above that only 5% of the time, so a score above it would be a reason for a closer look. Our score of 74.9 is well under it. The spread in Part 1 is what equal chances look like. Our chi-square explainer walks through the test step by step.
Test 2: Do hot numbers stay hot?
Split the 1,440 draws into two halves of 720. Take the 10 balls drawn most often in the first half and see how they did in the second:
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
),
counts AS (
SELECT ball,
SUM(draw_seq <= 720) AS first_720,
SUM(draw_seq > 720) AS last_720
FROM hits
GROUP BY ball
),
top_ten AS (
SELECT * FROM counts
ORDER BY first_720 DESC, ball
LIMIT 10
)
SELECT ROUND(AVG(first_720), 1) AS avg_first_720,
ROUND(AVG(last_720), 1) AS avg_last_720,
ROUND(720 * 5.0 / 69, 1) AS expected
FROM top_ten;Result:
| avg_first_720 | avg_last_720 | expected |
|---|---|---|
| 64.5 | 54.4 | 52.2 |
The ten hottest balls were drawn well above the expected count in the first half, then fell back close to it in the second. This is regression to the mean: the balls that ran hot by luck stop running hot, because luck doesn't carry over from one draw to the next. Our post on the law of large numbers explains the pattern.
Test 3: Backtest a "hot numbers" system
A backtest plays a strategy against past draws using only information available before each draw. This one goes through every draw from the 101st onward, picks the five white balls drawn most often in the previous 100 draws, and counts how many of them appeared. The BETWEEN t.draw_seq - 100 AND t.draw_seq - 1 join is what keeps the future out of each pick.
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
),
hot_counts AS (
SELECT t.draw_seq AS target_seq, h.ball, COUNT(*) AS times_drawn
FROM numbered AS t
JOIN hits AS h
ON h.draw_seq BETWEEN t.draw_seq - 100 AND t.draw_seq - 1
WHERE t.draw_seq > 100
GROUP BY t.draw_seq, h.ball
),
picks AS (
SELECT target_seq, ball
FROM (
SELECT target_seq, ball,
ROW_NUMBER() OVER (PARTITION BY target_seq
ORDER BY times_drawn DESC, ball) AS hot_rank
FROM hot_counts
)
WHERE hot_rank <= 5
)
SELECT COUNT(DISTINCT p.target_seq) AS draws_tested,
COUNT(h.ball) AS balls_matched,
ROUND(COUNT(h.ball) * 1.0 / COUNT(DISTINCT p.target_seq), 3) AS matches_per_draw,
ROUND(5 * 5.0 / 69, 3) AS expected_by_chance
FROM picks AS p
LEFT JOIN hits AS h
ON h.draw_seq = p.target_seq AND h.ball = p.ball;Result:
| draws_tested | balls_matched | matches_per_draw | expected_by_chance |
|---|---|---|---|
| 1340 | 479 | 0.357 | 0.362 |
If picks don't matter, five picks should match 5 × 5 ÷ 69 ≈ 0.362 white balls per draw on average. The hot-number system matched 0.357.
Test 4: Backtest an "overdue numbers" system
The opposite strategy picks the five balls that have gone longest without appearing, using the MAX(draw_seq) idea from Part 2:
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
),
last_seen AS (
SELECT t.draw_seq AS target_seq, h.ball, MAX(h.draw_seq) AS last_seq
FROM numbered AS t
JOIN hits AS h ON h.draw_seq < t.draw_seq
WHERE t.draw_seq > 100
GROUP BY t.draw_seq, h.ball
),
picks AS (
SELECT target_seq, ball
FROM (
SELECT target_seq, ball,
ROW_NUMBER() OVER (PARTITION BY target_seq
ORDER BY last_seq, ball) AS overdue_rank
FROM last_seen
)
WHERE overdue_rank <= 5
)
SELECT COUNT(DISTINCT p.target_seq) AS draws_tested,
COUNT(h.ball) AS balls_matched,
ROUND(COUNT(h.ball) * 1.0 / COUNT(DISTINCT p.target_seq), 3) AS matches_per_draw,
ROUND(5 * 5.0 / 69, 3) AS expected_by_chance
FROM picks AS p
LEFT JOIN hits AS h
ON h.draw_seq = p.target_seq AND h.ball = p.ball;Result:
| draws_tested | balls_matched | matches_per_draw | expected_by_chance |
|---|---|---|---|
| 1340 | 494 | 0.369 | 0.362 |
Test 5: Compare with picking the same numbers every time
For a baseline, here's the result of picking 1, 2, 3, 4 and 5 in every one of the same 1,340 draws:
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 COUNT(*) AS balls_matched,
ROUND(COUNT(*) * 1.0 / (SELECT COUNT(*) FROM numbered WHERE draw_seq > 100), 3) AS matches_per_draw
FROM hits
WHERE draw_seq > 100
AND ball IN (1, 2, 3, 4, 5);Result:
| balls_matched | matches_per_draw |
|---|---|
| 482 | 0.36 |
What the backtests show
Hot numbers matched 0.357 balls per draw, overdue numbers 0.369 and the fixed 1-2-3-4-5 pick 0.36. Chance predicts 0.362. Over 1,340 draws, chance alone moves this average by roughly ±0.03 either way (two standard errors; a single pick of five matches 0.36 balls on average with a standard deviation of about 0.56). All three results are inside that range, so none of the strategies did better than chance, including the one that ignores history completely.
That's the expected outcome. Powerball draws are independent: the machine and balls have no memory of earlier draws, so past frequency and past gaps carry no information about the next result. Our post on why each draw forgets covers it in more depth. The odds of a Powerball jackpot are 1 in 292,201,338 per play (Wikipedia) whichever numbers you choose.
So what is SQL good for?
- Checking claims. When a system or a website says a number is "due," you can now test the idea against real draws yourself, the way Tests 3 and 4 do.
- Checking data. Part 3 caught a back-to-back jackpot win our own query missed, by comparing the output with official records. That habit applies to any data set.
- Understanding the game. Gap lengths, run lengths and coincidences all follow from the odds. Seeing them in real data makes the math concrete.
- Learning SQL. Grouping, joins, views, CTEs and window functions are core tools in data work, and lottery results are a clean, public data set to practise them on.
If you play, play for fun, with money you can afford to lose. Our learn the math hub has more on odds and randomness, and help is available at 1-800-GAMBLER.
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.
Sources
- Chi-Square Goodness-of-Fit Test, NIST/SEMATECH e-Handbook of Statistical Methods
- Critical Values of the Chi-Square Distribution, NIST/SEMATECH e-Handbook of Statistical Methods
- Powerball, Wikipedia
- Gambler's fallacy, Wikipedia
- Window Functions, SQLite documentation
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.