DEV Community

Arhan Khan
Arhan Khan

Posted on

SQLAlchemy for Beginners: Python's Most Popular Way to Talk to Databases

SQLAlchemy is the standard way Python talks to databases. It gives you two tools in one library: a SQL expression language for writing queries in Python, and an ORM for mapping database rows to Python objects. Almost every serious Python backend — Flask, FastAPI, Django-adjacent projects — uses it or something built on top of it.

How it works: at the low level, you build queries with Python expressions and SQLAlchemy compiles them to SQL for Postgres, MySQL, or SQLite. At the ORM level, you define a class like User with columns, and each row becomes a User object you can read and update like normal Python. You can start with raw expressions and grow into the ORM when your models get complex — the library does not force you to choose on day one.

SQLAlchemy is a good fit for beginners because it meets you where you are. If you already know Python, you can write your first query in ten minutes, and the generated SQL is always visible, so you keep learning the database underneath. If you are still learning Python itself, a good reading list helps — these best Python books for beginners are a solid place to start alongside your database work.

A quick example — defining a model and querying it:

from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import declarative_base, Session

Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String)

engine = create_engine("sqlite:///app.db")
Base.metadata.create_all(engine)

with Session(engine) as session:
    users = session.query(User).all()
    print([u.name for u in users])
Enter fullscreen mode Exit fullscreen mode

That code creates the table, defines the model, and reads every user — no SQL strings anywhere.

Who is it for? Python developers building APIs and data tools; data analysts who need to query databases from scripts; students learning backend development in Python.

Limits to know: SQLAlchemy has a learning curve — sessions, the ORM, and the expression language are three things to learn, not one. It can feel heavy for a tiny script that only needs one query. And the ORM can hide slow queries (the N+1 problem) if you never look at the SQL it generates.

To start today: install SQLAlchemy, point it at a SQLite file, define one model with three columns, insert a row, and read it back.

Top comments (0)