Skip to content
IT glossary
BackendUpdated December 3, 2026

ORM

An ORM lets application code read and write data in a relational database through objects. It maps those objects to tables and translates operations on them into SQL.

Also known as: Object-Relational Mapping

What an ORM does

Object-relational mapping bridges the way an application represents data and the way a relational database stores it. Your code might represent a book as a Book object with an author attribute. The database stores the book in one table and the author in another, with an author_id foreign key between them. The mapping tells the ORM how to turn database rows into objects and how to persist those objects.

From code to SQL

A mapping defines which tables and columns a model uses, how rows are identified, and how models are related. The ORM uses that information to generate SQL. Building a query doesn't necessarily send it to the database: in Django, for instance, a queryset typically runs when you iterate over it. Writes vary by library, too. Some APIs issue SQL when you call save(), while others track changes and send them in a flush. Sending a write and committing the transaction are distinct steps, even when a library handles both for you.

Example: listing books and their authors

Suppose a Django model named Book has an author foreign key. This code prints the title and author of each published book:

books = Book.objects.filter(published=True).select_related("author")
for book in books:
    print(book.title, book.author.name)

Iteration runs a single query. select_related() tells Django to join the author table and load the author alongside each book. Without it, the loop would fetch the books first, then query for the author of each book on first access. For N books that makes N+1 queries, even if several books share the same author.

When the abstraction helps

ORMs are useful for routine reads and writes: creating a customer, looking up an order, or updating a product's price. They save you from repeatedly writing SQL and turning result rows into application objects by hand. But the database still decides how to execute the SQL. If an endpoint is slow, inspect the SQL it actually runs, count the queries, and use EXPLAIN to see how the database plans an expensive query. For a complex report, parameterized SQL can be clearer than forcing the query through the ORM's API.

What interviewers look for

Expect to reason about query counts and transaction boundaries, not just define the acronym. If you're asked to fix N+1, choose a loading strategy that fits the relationship. A join is often a good fit when each book has one author. If each book has many reviews, a join repeats the book's columns for every matching review. A batched second query can retrieve the reviews for all the selected books without that duplication. Explain what you'd measure to choose between the two, such as query count and the amount of data returned.