Advent of Code SQL
Can You Solve It in SQL? (2026)
Advent of Code accepts any programming language, so you can solve it in SQL. Parsing and counting days fit a query, and loop days need a recursive one. Advent of SQL is a separate 24 day calendar made only for SQL, and there is no official advent of data engineering, so this page shows how to use December as practice.
Can you solve Advent of Code in SQL?
Yes. Advent of Code accepts any programming language, so a query is a valid solution wherever you can express the puzzle as one. SQL handles the days that parse a text file and count or rank what is in it, and it needs a recursive query for the days that loop until something settles.
A second event, Advent of SQL, is a calendar made only for queries: 24 challenges from December 1 to December 24. If what you want is a SQL advent calendar rather than Advent of Code in SQL, that is the one.
There is no official advent of data engineering. The useful version is to treat December as practice in the habits the job rewards: load messy input into tables, model it, answer questions with queries you can read a month later, and check results on a small case before the real one.
Advent of Code vs Advent of SQL
- Language
- Starts
- Length
- Your data
- LanguageAny you like
- StartsDecember 1
- LengthEnds mid-December
- Your dataYour own puzzle input
- LanguageSQL, Postgres examples
- StartsDecember 1
- Length24 challenges
- Your dataA database dump
Which Advent of Code puzzles suit SQL
Sort each puzzle by what its second half asks. If it asks you to total or join rows you already have, SQL is shorter than a script. Reading ranges, matching pairs of lines, counting overlaps and scoring hands are set operations, so a GROUP BY or a window function does the work of a loop.
If it asks you to apply a rule again and again until nothing changes, you need iteration: a grid that evolves, a graph that is walked, a counter that grows each round. SQL does this with a recursive query, and the number of rounds is decided by the data, which is exactly the case recursion exists for.
A third group is closed to brute force in any language. One public write-up of the 2023 event in SQL found a day whose answer needed roughly 14.9 trillion steps, so simulating it was out and the way through was to notice a property of the input. Expect to read the input for structure before you write a line, in SQL as in anything else.
Budget matters too. The event states that every puzzle has a solution that runs in at most 15 seconds on 10-year-old hardware. A query that has not finished after a minute usually means a join that multiplies rows, or a recursive step that keeps rows it should drop.
Know Advent of Code SQL the way the interviewer who asks it knows it.
How a recursive query carries the loop days
A recursive common table expression has a starting query and a recursive query that reads the CTE's own previous output, joined by UNION or UNION ALL. The PostgreSQL documentation gives the evaluation: run the starting query into a working table, then while the working table isn't empty, run the recursive query against it, add its rows to the result and make them the new working table.
Take a graph of 6 edges (start to a, start to b, a to c, b to c, c to d, d to a) and ask which nodes the walk from start reaches. The working table holds 1, 2, 1, 1 rows on steps 0 to 3, and step 4 adds 0. The edge from d back to a would revisit a, but UNION drops rows already in the result, so the walk stops with 5 nodes instead of circling.


That drop is the whole safety mechanism, and it fails the moment you add a column that makes repeats look different, such as a step counter. The same documentation warns that a recursive part which never returns zero rows loops indefinitely, and it offers a CYCLE clause and a visited-path array for the cases where you need the depth as well. While testing, an outer LIMIT stops a runaway query early.
The reachability query behind a graph puzzle
WITH RECURSIVE reach(node) AS (
SELECT 'start'
UNION
SELECT e.dst
FROM edges e
JOIN reach r ON e.src = r.node
)
SELECT count(*) FROM reach;Counts the nodes reachable from start in an edges table with src and dst columns
How to set up a puzzle input for SQL
Every puzzle gives you a text file, and the first job is turning it into rows. Load each line into a table with a line number, raw(line_no, line), using your database's bulk load command or a short script. Keep the loader separate from the query so the query reads as the solution.
Parse in a first CTE and name the columns the puzzle talks about. Split on the delimiter and cast to integers. Keep the line number so you can reorder or join on position later. Most wrong answers come from this step, a stray blank line or a negative number cast the wrong way, and not from the clever part.
Run the puzzle's own small example first. The statement gives an expected answer for it, which is a test you did not have to write. When the example passes and the real input does not, the cause is almost always a shape the example never showed: a longer line, a duplicate key, an empty group.
Write part 1 and part 2 as 2 queries over the same parsed CTE. Part 2 usually changes one rule, and a readable part 1 makes the change a small edit.
Where Advent of Code in SQL gets hard, and the event's rules
Some puzzles are possible in SQL and not pleasant. A grid that updates in place, a program with registers, or a rule that depends on the order in which things happen all want mutable state, and a recursive query has to store state as a row per step. The query gets long, and the work per step grows with the number of rows you keep. Pick the days that suit sets and graphs, and solve the rest in a script without guilt.
The event's about page sets a few rules that affect a SQL repository. The global leaderboard was removed after 10 years, with private leaderboards remaining, so a friends' board is the way to compare. The FAQ answers no to using AI to solve the puzzles. And you may link to puzzles but should not post puzzle text or your inputs in repositories, so keep the inputs in a git-ignored folder and publish the queries.
For the loop days specifically, read recursive CTEs for hierarchies and graphs before the event and how a CTE differs from a subquery if the naming style is new to you.
How to get value out of a SQL advent calendar
A calendar rewards a steady cadence. Advent of SQL's 2024 edition sent 1 challenge a day for 24 days, with difficulty rising over the month and recursive SQL arriving around day 9. Skipping a few days costs little, since past challenges stay available after you sign in, so catching up in 1 sitting is a fair way to keep the habit.
The value is in the second pass. After a challenge works, rewrite it so a colleague could review it: name the CTEs for the business idea, remove the columns you carried by accident, and check that it still returns the same rows on a different input.
Compare notes with other people. The Advent of SQL community gathers on a subreddit, and the DataDriven.io discussion forum is a place to ask how someone else approached a query you found awkward.
An advent of data engineering plan for the other 11 months
December puzzles are short and self-contained. Data engineering work is long and messy, and a team shares it, so use the event for the query muscle and then point it at problems with an engineering shape. The DataDriven.io Weekly is one community data engineering challenge a week, scored blind on hidden data, with a brief, a small sample dataset and a starter file.
When the loop days feel solid, move to the questions an interviewer asks. SQL interview questions for data engineers cover the joins and aggregation that a recursive puzzle never touches, and SQL practice problems give you queries to write against data you did not design. The SQL round guide explains what the round looks for, and window function interview questions go deeper on the one feature most puzzle solutions skip.
Advent of Code in SQL FAQ
Can you do Advent of Code in SQL?+
Is there an Advent of SQL?+
Is there an Advent of Data Engineering?+
Which database should I use for Advent of Code in SQL?+
Can I use AI to solve Advent of Code?+
Can I publish my Advent of Code SQL solutions on GitHub?+
When do Advent of Code puzzles unlock?+
The candidate who gets the offer
- 01
Reading a solution is not the same as writing one
Every engineer who has frozen on a query they had read a dozen times knows the gap. The only preparation that closes it is producing the answer yourself, under time, before the interview does it for you
- 02
76% of hiring managers reject on the coding task, not the resume
From HackerRank's 2024 Developer Skills Report. Candidates who look strong on paper still fail the live screen if they haven't done timed, executable practice
- 03
5 problem shapes cover 80% of data engineer loops
Dedup, sessionization, top-N-per-group, slowly-changing dimensions, partition tricks. Writing the shapes by hand turns the unfamiliar into pattern recognition
Related guides
SQL interview questions
Questions asked in data engineering SQL rounds
SQL practice problems
Queries to write against data you didn't design
The SQL round
What the SQL round for data engineers looks for
Recursive CTEs
Hierarchies and date series in SQL, plus graph walks
Window function interview questions
Ranking and running totals, with lag questions
SQL CTEs
Common table expressions with examples
The data engineering challenge
Dirty, production-shaped data, scored blind each week.