Options for Your First App: Raw SQL, Query Builder or ORM

When your app needs to read and write data, you can write SQL yourself, use a query builder, or use an ORM. Each choice changes what you learn and what can go wrong. This paper lays out the options for a first project and makes a recommendation.

The problem

Beginners are often told to "just use an ORM" or "just write SQL". Both pieces of advice are right in some situations. Without understanding the trade-offs, it is hard to choose, and easy to get stuck when something does not work.

Background: three ways to talk to a database

Raw SQL. You write the SQL text yourself and send it through a database driver, using placeholders for values.

rows = conn.execute("SELECT name FROM students WHERE year = ?", (2,)).fetchall()

Query builder. A library lets you build SQL with code instead of text. It still looks like SQL, but the library assembles it. Knex for JavaScript is a well-known example.

const rows = await knex('students').select('name').where({ year: 2 });

ORM (object-relational mapper). You define classes, and the ORM turns objects into rows and rows into objects. You rarely see SQL. Examples include Django's ORM, SQLAlchemy, Prisma and Active Record in Rails.

names = [s.name for s in Student.objects.filter(year=2)]

Options compared

QuestionRaw SQLQuery builderORM
Do you learn SQL?Yes, directlyMostlyOnly if you look at the generated SQL
Safe from SQL injection?If you always use placeholdersYes, for normal useYes, for normal use
Amount of codeMoreMediumLess for simple tasks
When something goes wrongYou see the exact queryUsually easy to seeCan be hard to see what was sent
Common beginner trapGluing strings togetherFewHidden extra queries (the "N+1" problem)

Recommendation

For your first database-backed app, write raw SQL with placeholders. You will learn exactly what the database receives, and every later tool will make more sense.

For your second project, or once your first one grows, try an ORM or query builder, and turn on its logging so you can see the SQL it sends. Compare it with what you would have written yourself.

Risks of the recommendation

Conclusion

There is no single right tool, but there is a sensible order: SQL first, so you understand the database; then a higher-level tool, so you can work faster without losing sight of what happens underneath.

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.

This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.