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
| Question | Raw SQL | Query builder | ORM |
|---|---|---|---|
| Do you learn SQL? | Yes, directly | Mostly | Only if you look at the generated SQL |
| Safe from SQL injection? | If you always use placeholders | Yes, for normal use | Yes, for normal use |
| Amount of code | More | Medium | Less for simple tasks |
| When something goes wrong | You see the exact query | Usually easy to see | Can be hard to see what was sent |
| Common beginner trap | Gluing strings together | Few | Hidden 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
- Raw SQL can be repetitive. That is fine for a small project, and it shows you why ORMs exist.
- You might be tempted to glue strings. Make a rule: every value goes through a placeholder, every time. Our Node.js placeholder how-to shows how.
- Your course or framework may require an ORM. Then use it, and still read the SQL it generates.
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.