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.

Last updated: Proudly published by: Jeff Wahl8 min read

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

Advent of Code
  • LanguageAny you like
  • StartsDecember 1
  • LengthEnds mid-December
  • Your dataYour own puzzle input
Advent of SQL
  • 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.

Prepare for the interview
01 / Open invite
02min.

Know Advent of Code SQL the way the interviewer who asks it knows it.

a Advent of Code SQL query, the same shape a screen would give you.
The diff against expected. Where ties broke. What you missed.
sandbox
1SELECT user_id,
2 COUNT(*) AS sessions
3FROM events
4WHERE ts >= NOW() - INTERVAL '7 day'
5
Execute your solution0.4s avg.
NetflixInterview question
Solve a Advent of Code SQL problem

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.

Bars of the new rows a recursive query adds at each step: 1 at step 0, 2 at step 1, 1 at step 2, 1 at step 3 and 0 at step 4, where it stops, for a result of 5 rowsBars of the new rows a recursive query adds at each step: 1 at step 0, 2 at step 1, 1 at step 2, 1 at step 3 and 0 at step 4, where it stops, for a result of 5 rows

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?+
Yes. The event's own about page says puzzles can be solved in any programming language you like. Load the puzzle input into a table, one row per line, and write the answer as a query. Days that parse text and count fit SQL well; loop or simulation days need a recursive query.
Is there an Advent of SQL?+
Yes, a separate event. Its 2024 edition emailed 24 Christmas-themed SQL challenges, 1 a day from December 1 to December 24, each with a database dump to load, and took answers through a form on the challenge page. It is not run by the people who run Advent of Code.
Is there an Advent of Data Engineering?+
I found no official event under that name. The closest fits are Advent of Code solved with data engineering habits (parse, model, test), a SQL calendar for the query side, and a weekly data engineering challenge on the DataDriven.io community page that runs all year.
Which database should I use for Advent of Code in SQL?+
Any engine that supports recursive common table expressions and string functions will do. PostgreSQL is a common pick because its documentation describes recursive evaluation precisely and its regular expression and array functions help with parsing.
Can I use AI to solve Advent of Code?+
The event's FAQ answers no: the puzzles are designed to be interesting for humans to solve. If you want a clean practice record, write the query yourself and use a model only afterwards, to compare approaches.
Can I publish my Advent of Code SQL solutions on GitHub?+
You may link to or reference puzzles, but the event asks you not to post puzzle text or your puzzle inputs in repositories. Publish the queries and a loader script, and leave the inputs out.
When do Advent of Code puzzles unlock?+
At midnight EST (UTC-5), starting December 1. Since the event changed its schedule, puzzles come out every day and end in mid-December.
02 / Why practice

The candidate who gets the offer

  1. 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

  2. 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

  3. 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