Should I use an ORM or write raw SQL?

Updated October 2026 · How we answer

Short answerUse an ORM for most CRUD and simple queries to speed development; drop to raw SQL for complex reports or performance-critical paths. Many teams mix both.

When an ORM pays off

An ORM (Object-Relational Mapper) maps database tables to objects in your code, so you write less boilerplate and avoid manual SQL string building. It handles parameterization, which helps prevent SQL injection, and provides migrations to keep schema changes in sync.

For standard create, read, update, delete operations, an ORM can cut development time significantly. It also makes your code more portable if you might switch databases later, though that portability is rarely perfect.

  • Rapid prototyping and MVPs
  • Teams with mixed database skill levels
  • Apps with straightforward data models
  • Projects that need automatic migrations

When raw SQL wins

Raw SQL gives you full control over query plans, hints, and database-specific features like window functions or CTEs. For complex analytics, bulk operations, or queries that must be tuned to the millisecond, handwritten SQL often outperforms ORM-generated queries.

Many ORMs let you execute raw SQL for specific queries, so you don't have to choose one exclusively. A common pattern is to use the ORM for 90% of operations and raw SQL for the remaining 10% that are complex or performance-sensitive.

  • Complex reporting and analytics
  • Bulk inserts or updates
  • Queries needing database-specific optimizations
  • Legacy databases with unusual schemas

Common mistakes

  • Assuming an ORM always generates optimal SQL; it often produces inefficient queries for complex relationships.
  • Believing raw SQL is inherently unsafe; with parameterized queries, it's just as secure as an ORM.
  • Thinking you must pick one approach for the entire project; mixing them is common and practical.
From our shopsCaseMorph: Type an idea, see a custom phone case in seconds, then print a one-of-one.