Counting the Cost of Learning an ORM Before SQL

An ORM (object-relational mapper) is a library that lets you work with database rows as if they were objects in your programming language. Many tutorials start with one. This page weighs the cost of learning an ORM first against learning plain SQL first, without pretending there is one right answer.

Two learning paths

Path A: ORM first. You write code like Book.objects.filter(author="Ann") in Django or Book.where(author: "Ann") in Rails. The ORM writes the SQL for you.

Path B: SQL first. You write SELECT * FROM books WHERE author = 'Ann'; yourself, using a database driver (the small library that sends SQL to the database), then pick up an ORM later.

What each path costs you up front

CostORM firstSQL first
Time to first working appUsually shorterA little longer
New ideas to learn at onceFramework, ORM and database togetherDatabase and SQL, then the framework
Understanding errorsErrors may come from several layersErrors usually point to your SQL

What each path costs you later

Later costORM firstSQL first
Debugging slow pagesHarder if you cannot read the SQL the ORM sendsEasier, you recognise the queries
Writing a report the ORM cannot expressNeed to learn SQL then, under pressureAlready comfortable
Moving to another frameworkORM skills do not transfer directlySQL transfers to almost any stack

A way to estimate for yourself

Ask three questions and note your answers:

  1. Do I need something working this week? If yes, the ORM path gets you there faster.
  2. Will I work with data for years? If yes, SQL is a skill that keeps paying back.
  3. Do I enjoy seeing how things work underneath? If yes, you will likely enjoy SQL first.

Two or three "years and underneath" answers suggest SQL first. A pressing deadline suggests ORM first, with SQL soon after.

A middle path many learners like

  1. Spend a few evenings learning SELECT, INSERT, UPDATE and DELETE on a tiny SQLite database.
  2. Then start with your framework's ORM.
  3. Turn on the ORM's query logging so you can see the SQL it produces. Read it. You will recognise most of it.

This costs only a little extra time at the start and removes most of the later costs in the table above.

Common beginner traps either way

The takeaway

Neither path is wrong. The cheapest overall route for most people is "a little SQL first, then the ORM, with the query log switched on". When you are ready for performance topics such as counting queries per page, the sister site databaseintegrate.com covers them for working developers.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.