SQL for Lottery Analysis series
- Part 1: Count how often each number is drawn (you are here)
- 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
Free practice data: SQLite database · Powerball CSV · Pick 3/Pick 4 CSV
Every chart on DrawAnalytics starts life as a database query. This series shows how to write those queries yourself, using SQLite and free sample files of real draw results. Part 1 assumes no SQL at all. By the end of Part 5 you'll have counted frequencies, measured gaps, found jackpot streaks, compared states and tested whether any of it helps pick numbers.
Step 1: Get the data and a tool
Download the DrawAnalytics SQL sample database (one SQLite file). The same data is also available as two CSV files: powerball_draws.csv and pick_draws_2025.csv.
Everything here uses SQLite, the free database engine that stores a whole database in one file. Two easy ways to run the queries:
- DB Browser for SQLite (sqlitebrowser.org), an open-source desktop app. Open
drawanalytics-sql-sample.db, go to the Execute SQL tab, paste a query and press the run button. - The sqlite3 command-line shell. Run
sqlite3 drawanalytics-sql-sample.db, then type.headers onand.mode columnso results print as a readable table.
Parts 2 to 5 use window functions, which need SQLite 3.25.0 or newer (released in 2018). Any current version of either tool is fine. If you'd rather load the CSV files into another database, the column names are the same, though small details such as date functions differ between database engines.
Step 2: Count the rows
A query starts with SELECT (what you want back) and FROM (which table). COUNT(*) counts rows, and MIN and MAX find the smallest and largest values. AS just names a column in the output.
SELECT COUNT(*) AS draws,
MIN(draw_date) AS first_draw,
MAX(draw_date) AS last_draw
FROM powerball_draws;Result:
| draws | first_draw | last_draw |
|---|---|---|
| 1440 | 2015-10-07 | 2026-11-30 |
That's 1,440 Powerball draws. Powerball moved to 69 white balls and 26 red balls in October 2015 (Wikipedia), and the table starts at the first drawing under that format, so every row is drawn from the same pools.
Step 3: Sort and limit
ORDER BY sorts the rows, DESC makes it newest first, and LIMIT keeps only the first few. Dates are stored as text in YYYY-MM-DD form, which sorts correctly as plain text.
SELECT draw_date, n1, n2, n3, n4, n5, powerball
FROM powerball_draws
ORDER BY draw_date DESC
LIMIT 5;Result:
| draw_date | n1 | n2 | n3 | n4 | n5 | powerball |
|---|---|---|---|---|---|---|
| 2026-11-30 | 1 | 4 | 32 | 51 | 68 | 23 |
| 2026-11-28 | 1 | 25 | 38 | 44 | 67 | 2 |
| 2026-11-25 | 23 | 43 | 57 | 58 | 68 | 12 |
| 2026-11-23 | 7 | 9 | 27 | 40 | 58 | 19 |
| 2026-11-21 | 5 | 29 | 33 | 36 | 45 | 22 |
Step 4: Filter with WHERE
WHERE keeps only the rows that match a condition, and AND combines conditions. Here are the draws in 2026 where the red Powerball was 7:
SELECT draw_date, n1, n2, n3, n4, n5, powerball
FROM powerball_draws
WHERE powerball = 7
AND draw_date >= '2026-01-01'
ORDER BY draw_date;Result:
| draw_date | n1 | n2 | n3 | n4 | n5 | powerball |
|---|---|---|---|---|---|---|
| 2026-05-11 | 24 | 30 | 37 | 56 | 64 | 7 |
| 2026-08-08 | 5 | 9 | 35 | 54 | 63 | 7 |
| 2026-09-16 | 2 | 21 | 24 | 25 | 64 | 7 |
| 2026-09-28 | 17 | 21 | 29 | 42 | 49 | 7 |
Step 5: Group and count
To count how often each red ball has been drawn, GROUP BY puts the rows into one group per Powerball number, and COUNT(*) counts each group:
SELECT powerball, COUNT(*) AS times_drawn
FROM powerball_draws
GROUP BY powerball
ORDER BY times_drawn DESC
LIMIT 5;Result:
| powerball | times_drawn |
|---|---|
| 14 | 68 |
| 4 | 67 |
| 21 | 65 |
| 24 | 63 |
| 20 | 63 |
Remove the LIMIT 5 line to see all 26.
Step 6: Turn five columns into one
The white balls are harder, because each draw stores them in five columns. To count ball 21, you'd have to check n1, n2, n3, n4 and n5. The fix is to stack the five columns into one long list with UNION ALL, which glues the results of several queries together. Saving that as a view means you can use it like a table from now on (SQLite documentation):
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;
Each draw now appears five times, once per white ball:
SELECT COUNT(*) AS white_balls_drawn
FROM white_balls;Result:
| white_balls_drawn |
|---|
| 7200 |
1,440 draws times five balls is 7,200 rows, as it should be.
Step 7: Most and least drawn white balls
SELECT ball, COUNT(*) AS times_drawn
FROM white_balls
GROUP BY ball
ORDER BY times_drawn DESC, ball
LIMIT 5;Result:
| ball | times_drawn |
|---|---|
| 21 | 128 |
| 61 | 127 |
| 64 | 126 |
| 23 | 121 |
| 28 | 121 |
SELECT ball, COUNT(*) AS times_drawn
FROM white_balls
GROUP BY ball
ORDER BY times_drawn ASC, ball
LIMIT 5;Result:
| ball | times_drawn |
|---|---|
| 13 | 80 |
| 26 | 84 |
| 46 | 84 |
| 49 | 86 |
| 34 | 87 |
Ball 21 has been drawn 128 times and ball 13 only 80. Sorting by ball as well as the count breaks ties in a fixed order, so you get the same answer every time.
Step 8: Compare with what chance predicts
A raw count means little on its own. If every ball is equally likely, each draw picks 5 of 69 balls, so each ball should turn up in about 5/69 of the draws. Over 1,440 draws that's 1,440 × 5 ÷ 69 ≈ 104.3 times. A query can work that out with a subquery, a query inside brackets whose answer is used as a value. Writing 5.0 instead of 5 matters, because SQLite divides whole numbers as whole numbers.
SELECT ball,
COUNT(*) AS times_drawn,
ROUND((SELECT COUNT(*) FROM powerball_draws) * 5.0 / 69, 1) AS expected,
ROUND(COUNT(*) - (SELECT COUNT(*) FROM powerball_draws) * 5.0 / 69, 1) AS difference
FROM white_balls
GROUP BY ball
ORDER BY difference DESC
LIMIT 5;Result:
| ball | times_drawn | expected | difference |
|---|---|---|---|
| 21 | 128 | 104.3 | 23.7 |
| 61 | 127 | 104.3 | 22.7 |
| 64 | 126 | 104.3 | 21.7 |
| 32 | 121 | 104.3 | 16.7 |
| 28 | 121 | 104.3 | 16.7 |
SELECT MIN(times_drawn) AS fewest,
MAX(times_drawn) AS most,
ROUND(AVG(times_drawn), 1) AS average
FROM (
SELECT ball, COUNT(*) AS times_drawn
FROM white_balls
GROUP BY ball
);Result:
| fewest | most | average |
|---|---|---|
| 80 | 128 | 104.3 |
So the counts run from 80 to 128 around an average of 104.3. Is that spread suspicious, or just what randomness looks like? It's a fair question, and Part 5 answers it with a proper test. Our guide to reading frequency charts honestly explains why spreads like this appear even in perfectly fair games.
Try it yourself
- Count draws by day of the week with
GROUP BY draw_day. (Monday draws only began in August 2021, per Wikipedia, so Mondays will be well behind.) - Find how many times ball 21 was drawn in each year. Hint:
substr(draw_date, 1, 4)pulls out the year, and you'll needWHERE ball = 21on thewhite_ballsview. - Compare the results with our live most common Powerball numbers page, which runs the same kind of count on the full history.
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 2: Find "overdue" numbers with window functions
Sources
- Powerball, Wikipedia
- Previous Results, Powerball.com
- CREATE VIEW, SQLite documentation
- Command Line Shell For SQLite, SQLite documentation
- DB Browser for SQLite
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.