Ben Barbersmith

SQL is hard

— Posted on Mar 11, 2021

Okay, time for some real talk.

If you’re like most devs, you don’t write SQL. Instead you write less efficient and less maintainable data processing code in JavaScript, Python, Java, PHP, Go, Ruby, etc.

You miss out on the real power of your database.

Why? Because SQL is mysterious and you don’t know what it can do for you.

SQL is hard because it’s declarative, not imperative. You have to say “this is the result I want” instead of “follow these steps”. That makes it feel unfamiliar to most devs. And without experience, this unfamiliar approach makes SQL hard to write and reason about.

Writing SQL as a beginner is a mindfuck. Something you’d solve with a simple for loop can require mind-bending queries. Working primarily with columns of data instead of arrays of objects changes the way you need to think. You can’t just solve problems by adding more code.

Worse: there’s a performance penalty that gets applied if you phrase your request (“this is the result I want”) in the wrong way.

It’s not enough to know the syntax of SQL. Just like JS, PHP, Ruby and Python, the syntax is just the beginning. You need to understand the fundamentals, too.

Unfortunately, most of us (me included!) started off with ORMs or other database abstraction layers. You write models and the libraries handle the database and generate SQL for you.

But when things go wrong or you need more power, that makes the database and accompanying SQL even more indecipherable! We end up trying to learn SQL when we hit the limitations of our abstraction layer. Then we have to reason about the libraries and our database tables and our queries at the same time. But we didn’t even write the tables or queries to begin with!

No wonder we fear SQL.

Besides, so much about SQL and database performance is straight-up mysterious.

  • How do migrations work?
  • What do foreign keys actually do?
  • Why is this join slow?
  • When will deletes cascade?
  • What is a materialized view for?
  • What are the different flavours of SQL used by different databases?

But this stuff can be taught and learned!

Nobody starts with a deep understanding of SQL, but you CAN acquire it. Understanding SQL will stop you from fearing it, and that will make you more productive in the future.

Why is knowing SQL so valuable? Once you get it, you can:

  • Write more concise data-crunching code.
  • Benefit from the insanely optimised performance that databases offer.
  • Use your chosen ORMs and database abstractions without fear, knowing how they work under the hood.
  • Write code that can be re-used by anything that touches the database.
  • Write queries for analytics platforms, data warehouses, etc.
  • Easily answer ad-hoc questions about user behaviour.
  • Solve data issues introduced by backend or frontend bugs without resorting to hacky patches

SQL is a multi-purpose tool with a ton of upside.

I learned all about SQL during my 7 years at Google. Now I want to teach it to the world.

I’m building a course on this topic: SQL for Humans. Even better, I’m building it in public so you can get involved as I create it. For updates, just follow me on Twitter: @benbarbersmith.

Mar 11, 2021 @benbarbersmith