# Writing SQL Queries

> Read and write data with SELECT, JOIN, GROUP BY, subqueries and window functions. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/sql-querying
Last updated: 2026-11-28

Read and write data with SELECT, JOIN, GROUP BY, subqueries and window functions.

Beginner · about 12 hours · difficulty 2/5

Learn first: [The Relational Model](https://www.internhack.in/roadmaps/tracks/backend/relational-model)

SQL is the language for talking to relational databases, and it is one of the most durable skills in software. Even if you use an ORM daily, you need to read and write SQL to debug and optimize.

### Build up in this order

1. **CRUD**: `SELECT ... FROM ... WHERE`, `INSERT`, `UPDATE`, `DELETE`. Always double-check the `WHERE` clause on updates and deletes.
2. **Sorting and limiting**: `ORDER BY`, `LIMIT`, `OFFSET`.
3. **Joins**: `INNER JOIN` (only matches), `LEFT JOIN` (keep all rows from the left even without a match). These answer most real questions.
4. **Aggregation**: `COUNT`, `SUM`, `AVG`, `GROUP BY` and `HAVING` (filter after grouping).
5. **Subqueries and CTEs**: `WITH recent AS (...) SELECT ...` makes complex queries readable.
6. **Window functions**: `ROW_NUMBER()`, `RANK()`, running totals with `SUM() OVER (...)`.
7. **Upserts**: `INSERT ... ON CONFLICT DO UPDATE` in PostgreSQL.

### Safety

Never build SQL by concatenating user input. Use **parameterized queries** (`WHERE email = $1`) so the driver sends data separately from the query text. This prevents SQL injection.

### Pitfalls

- `NULL` is not equal to anything, including `NULL`. Use `IS NULL`.
- `SELECT *` in application code hides which columns you depend on and fetches more than needed.

You are ready to move on when you can answer "top 5 customers by revenue last month, including customers with zero orders" in a single query.

## Key concepts

- **SELECT, INSERT, UPDATE, DELETE**
- **INNER and LEFT JOIN**
- **GROUP BY and HAVING**
- **Subqueries and CTEs**
- **Window functions**
- **Upserts**
- **Parameterized queries**: Your main defense against SQL injection.
- **NULL semantics**

## Mini project

Load a sample dataset (for example the PostgreSQL Pagila or a Kaggle CSV) and write 15 progressively harder queries, ending with window functions and CTEs.

## Check yourself

Why does a LEFT JOIN followed by a WHERE condition on the right table sometimes behave like an INNER JOIN?

## Free resources

- [SQLBolt interactive lessons](https://sqlbolt.com/) (SQLBolt)
- [PostgreSQL: Queries](https://www.postgresql.org/docs/current/queries.html) (PostgreSQL)
- [Select Star SQL](https://selectstarsql.com/) (selectstarsql.com)

Previous: [The Relational Model](https://www.internhack.in/roadmaps/tracks/backend/relational-model) · Next: [PostgreSQL](https://www.internhack.in/roadmaps/tracks/backend/postgresql)

## More in Storing Data Relationally

- [The Relational Model](https://www.internhack.in/roadmaps/tracks/backend/relational-model)
- [Writing SQL Queries](https://www.internhack.in/roadmaps/tracks/backend/sql-querying)
- [PostgreSQL](https://www.internhack.in/roadmaps/tracks/backend/postgresql)
- [MySQL, SQLite and Other Engines](https://www.internhack.in/roadmaps/tracks/backend/other-relational-engines)
- [Schema Design and Normalization](https://www.internhack.in/roadmaps/tracks/backend/schema-design-normalization)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
