← Back to Blog

SQL for Lottery Analysis, Part 1: Counting Number Frequency

December 2, 2026  ·  9 min read  ·  Education

SQL for Lottery Analysis series

  1. Part 1: Count how often each number is drawn (you are here)
  2. Part 2: Find "overdue" numbers with window functions
  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

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:

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:

drawsfirst_drawlast_draw
14402015-10-072026-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_daten1n2n3n4n5powerball
2026-11-301432516823
2026-11-281253844672
2026-11-25234357586812
2026-11-237927405819
2026-11-2152933364522

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_daten1n2n3n4n5powerball
2026-05-1124303756647
2026-08-08593554637
2026-09-162212425647
2026-09-2817212942497

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:

powerballtimes_drawn
1468
467
2165
2463
2063

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:

balltimes_drawn
21128
61127
64126
23121
28121
SELECT ball, COUNT(*) AS times_drawn
FROM white_balls
GROUP BY ball
ORDER BY times_drawn ASC, ball
LIMIT 5;

Result:

balltimes_drawn
1380
2684
4684
4986
3487

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:

balltimes_drawnexpecteddifference
21128104.323.7
61127104.322.7
64126104.321.7
32121104.316.7
28121104.316.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:

fewestmostaverage
80128104.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

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 2: Find "overdue" numbers with window functions

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.