N
NexusDigitalLabs
← Back to Academy

Phase 1 — Foundations · Lesson 12 · 20 XP

SQL and Postgres

Core SQL covers most needs: SELECT to read, INSERT/UPDATE/DELETE to write, WHERE to filter, JOIN to combine tables through a shared key, GROUP BY to aggregate. A primary key uniquely identifies a row; a foreign key points to another table's primary key, which is how a JOIN connects them.

SELECT u.name, COUNT(o.id) AS orders
FROM users u
JOIN orders o ON o.user_id = u.id
GROUP BY u.name
ORDER BY orders DESC;

An index is a separate data structure that lets Postgres find matching rows without scanning the whole table — essential once a table has more than a few thousand rows. It isn't free: every index slows down writes a little and takes disk space, so you index columns you actually filter or join on, not every column.

Exercise

Design a small schema (users, todos) in your local Postgres, insert sample rows, and write three queries: a JOIN across both tables, a GROUP BY aggregation, and a query filtered on a column you've indexed.

Check yourself

1. What does a database index actually do, and why doesn't every column just get one automatically?

2. What's the difference between an INNER JOIN and a LEFT JOIN?

← Previous lesson

Mocking HTTP, ruff, git workflow

Answer the check-yourself questions to unlock this