# SQL and Postgres Basics

> Enough SQL to store conversations, metadata and embeddings in Postgres, which is where many production RAG systems keep their data. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/ai-engineer/sql-basics
Last updated: 2026-11-28

Enough SQL to store conversations, metadata and embeddings in Postgres, which is where many production RAG systems keep their data.

Beginner · about 10 hours · difficulty 2/5

AI applications still need ordinary data: users, conversations, documents, feedback, costs. **Postgres** handles all of that, and with the pgvector extension it can also store embeddings, which is why it is our recommended starting point for vector search later in this track.

### What to learn

- **Querying**: `SELECT`, `WHERE`, `ORDER BY`, `LIMIT`, `JOIN`, `GROUP BY`.
- **Schema design**: tables for documents, chunks and conversations; primary and foreign keys.
- **Indexes**: why a query is slow and how an index fixes it. Vector indexes follow the same idea.
- **JSONB columns**: handy for storing flexible metadata or raw model responses.
- **Parameterized queries**: never build SQL by concatenating strings, especially with text that came from a user or a model.
- **An ORM or query builder**: SQLAlchemy, Prisma or Drizzle are common, but know the SQL they generate.

### Pitfalls

Letting an LLM write and execute SQL against production without guardrails is dangerous. If you build text-to-SQL features later, use a read-only role and limits.

### Good enough to move on

You can design a small schema for documents and chat messages, insert and query it from code with parameterized queries, and add an index to speed up a filter.

## Key concepts

- **SELECT, JOIN, GROUP BY**
- **Schema design and keys**
- **Indexes**
- **JSONB columns**
- **Parameterized queries**: Prevents SQL injection
- **ORMs (SQLAlchemy, Prisma, Drizzle)**

## Mini project

Create a Postgres schema for conversations and messages, insert sample chats from code, and write a query that returns the 10 most active users with their message counts.

## Check yourself

Why is concatenating model-generated text into a SQL string dangerous, and what is the fix?

## Free resources

- [PostgreSQL tutorial](https://www.postgresql.org/docs/current/tutorial.html) (postgresql.org)
- [SQLBolt interactive lessons](https://sqlbolt.com/) (SQLBolt)

Previous: [Async I/O and Concurrency](https://www.internhack.in/roadmaps/tracks/ai-engineer/async-programming) · Next: [Tokens and Context Windows](https://www.internhack.in/roadmaps/tracks/ai-engineer/tokens-and-context)

## More in Engineering Foundations for AI Work

- [Python for AI Engineers](https://www.internhack.in/roadmaps/tracks/ai-engineer/python)
- [TypeScript for AI Engineers](https://www.internhack.in/roadmaps/tracks/ai-engineer/typescript)
- [Git and Project Hygiene](https://www.internhack.in/roadmaps/tracks/ai-engineer/git)
- [HTTP, REST APIs and JSON](https://www.internhack.in/roadmaps/tracks/ai-engineer/http-apis-json)
- [Async I/O and Concurrency](https://www.internhack.in/roadmaps/tracks/ai-engineer/async-programming)
- [SQL and Postgres Basics](https://www.internhack.in/roadmaps/tracks/ai-engineer/sql-basics)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [AI Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/ai-engineer)
