Skip to content
Learn Netverks

Lesson

Step 33/36 92% through track

orm-vs-raw-sql

ORM vs raw SQL

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches ORM vs raw SQL: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

ORMs generate SQL—senior roles still debug raw queries and N+1 problems.

You will apply ORM vs raw SQL in contexts like: Postgres, MySQL, SQLite, warehouses, and ORMs that still expose SQL.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

Toward the end of the track—consolidate before dialect tracks, interview prep, and production checklist lessons.

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

  1. Q: ORM lazy loading?
    A: Related objects fetched on access—can cause N+1 unless eager loaded.
  2. Q: When skip ORM?
    A: Heavy aggregates, bulk COPY, or queries needing specific index hints/plans.

Self-check

  1. One advantage of ORM for CRUD?
  2. 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.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • When raw SQL?
  • SQL injection?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump