ORMs map classes to tables and methods to queries—productive for CRUD. Raw SQL wins for complex reports, bulk operations, and performance-critical paths where you need full control.
ORM benefits
- Less boilerplate for simple CRUD
- Type-safe models and migrations integration
- Database abstraction across dev SQLite and prod Postgres
Raw SQL benefits
SELECT date_trunc('month', ordered_at) AS month,
SUM(total) AS revenue
FROM orders
GROUP BY 1
ORDER BY 1;Dialect-specific functions, window queries, and EXPLAIN tuning are often clearer in raw SQL—common in Data Science notebooks via pandas/SQLAlchemy text().
Hybrid approach
Use ORM for app transactions; drop to raw SQL for admin dashboards, migrations, and batch ETL. Log and review generated SQL in development.
Important interview questions and answers
- Q: ORM lazy loading?
A: Related objects fetched on access—can cause N+1 unless eager loaded. - Q: When skip ORM?
A: Heavy aggregates, bulk COPY, or queries needing specific index hints/plans.
Self-check
- One advantage of ORM for CRUD?
- One case where raw SQL is preferable?
Tip: Log ORM-generated SQL in dev; switch to raw SQL for complex reports when needed.
Interview prep
- ORM advantage?
Less CRUD boilerplate and integrated migrations.
- Raw SQL when?
Complex reports, bulk ops, performance tuning with EXPLAIN.