Writing Million Dollar SQL

The stakes are high. You’re in a time crunch. Once the code goes live, there’s no undoing bugs.

You’ve got your best and brightest on it. A 23 year-old making no money.


Looking back, I’m still surprised. You’d think that algorithmically giving away a $1 million grand prize would merit someone other than a 23 year-old a year out of college writing the code.

Now, and this is just me, if I was working on a project where one mistake could cost my company $1 million and everyone knew I wrote all the code, I would simply not make a mistake that cost my company $1 million.

Anyway, here’s the story about how I did not make a mistake that cost my company $1 million, and how I wrote the code for a Caesars Entertainment marketing promotion that drove eight figures of incremental revenue.

Caesars Entertainment

You’ve probably heard of Caesars Palace. Out of college, I worked for its parent company, which owned about 50 properties across the world, but mostly in the US.

It was an incredibly data-driven company, with an analytics department of about 80 when I joined. At the time, I was part of a small data science team focusing mainly on forecasting and machine learning.

The Project

In 2019, Caesars and the NFL announced that Caesars was becoming the official casino sponsor of the NFL. And naturally, the Caesars marketing team wanted to do something big for the start of football season.

The campaign they envisioned sounded a lot like McDonald’s Monopoly. You spend money at a property, whether in the casino, restaurants, hotel, etc., you can earn different kinds tokens on our mobile app. You could also mail in a form to receive complimentary entries. Completing different sets of tokens earned prizes, and completing every set won you the $1 million.

Sounds fun! However, when the marketing team spoke to a vendor to arrange the project, the vendor wanted several months and seven figures, neither of which marketing wanted to do. So they asked the data science team to help.

The data scientist assigned to the project did an excellent job setting up the math about how the promotion should work – how many of each token should exist, what is the expected cost of prizes, etc. However, less than two months before the go-live date, he took a new job.

And so I was asked to step in. You know, normal 23 year-old things, just writing code to dole out $1 million.

What I Did

At this point, we know how many of which token were supposed to exist in the game universe, but we had two big remaining tasks.

  1. Write the logic to actually assign tokens to guests as they are earned
  2. Run simulations using historical data to confirm the math worked out as expected

I don’t want to bore you too much with the technical details. I built out the logic in Python, and the data was large enough that we had to do things like size up our virtual machines (for non-technical readers, think computers in the cloud), use a library called dask rather pandas to store the data, and run the simulations in parallel so that we could run them in a day rather than a week.

In this case, what do I mean by simulations? We knew that tokens were earned by spend, so we pulled historical spend and backtested how the promo would have turned out if we ran it at that time.

The simulation results came back supportive of our initial estimates. At that point, my director thought it would be easier on the day-to-day tokens assignments to run in SQL (a language used to query databases), so we translated the Python code into a series of SQL queries (if only we used a tool like dbt to run them).

The launch was still a few weeks away, so we continued to run simulations to catch any remaining edge cases, add tests to the code, and set up copious amounts of logging and recordkeeping in case we were audited. We also worked with legal and security teams on making sure security measures were in place and access was severely restricted (not even I could see anything once we went live).

My work was done, but this began the waiting period – will my code actually work in the real world?

Crossing Fingers

You know who doesn’t say “the house always wins”? Anyone who listens to gaming company earnings calls (I’ll have a post on this eventually). Bad luck in high stakes baccarat was a free space on the bingo card.

But when casinos take big bets, whether in blackjack, baccarat, or any other game, they know the exact odds and have enough of a sample size to feel confident.

They’ve validated those odds with billions of simulations and reps against real players trying to win the casino’s money.

In our case, we had neither. We did as many simulations as we could (remember, the simulation scripts took a long time to run), but we hadn’t confirmed everything would run smoothly in the real world. We felt good about our calculated odds, but those odds fly out the window if it turns out I made a mistake in the code and the token assignments go beserk.

And as the guy who wrote every line of the SQL code for token assignment (and therefore who would win which prizes), any such error most likely came from me.

That’s what made this project so nerve-wracking. 

It would have made things so much smoother if we did all the token assignments at once, because the suspense would be finite. But we assigned tokens once a day for three months. Every day I walked into the office not knowing if someone won big because of a mistake by me.

Fortunately, the day never came where someone won the million because of my mistake.


Overall, the campaign went great. When my parents visited me in Las Vegas that fall, it was really cool to point towards the ad on a billboard and show them what I was working on. I was told it was the best performing marketing campaign in the company’s history, with eight-figure incremental revenue.

Victor, the data engineer that wrote out our orchestration, and I both won Employee of the Quarter awards within the analytics department. We each got a free a dinner at Nobu.

There are some projects where you aren’t qualified for your role. This was one. But I didn’t let that get in my way, and it paid off for everybody.