Explained step by step
A SQL (or relational) database stores data in tables with columns and rows, and defines relationships between those tables. Each table represents a kind of thing (users, orders) and each row is one specific thing. It's like a filing cabinet with folders that refer to each other: an order's card says 'see customer 42'. It's queried with SQL, and examples are PostgreSQL, MySQL, MariaDB and Amazon Aurora.
SQL databases have clear advantages: they're simple and accurate, easy to query, keep data consistent and are flexible, because the same data can be read in many ways without reorganizing tables. It's like a well-ordered filing cabinet: you can search by customer, date or amount without moving any folder. That's why almost every app starts with PostgreSQL or MySQL.
The primary key is a unique identifier marking each row of a table. It can't repeat: there are no 2 users with id 42. Thanks to it, the database finds an exact row instantly and other tables can point to it. It's like a person's ID number: there can be 2 John Smiths, but never 2 with the same number.
A foreign key is a column pointing to the primary key of another table, which relates the 2 tables. In the orders table, the user_id column stores the id of the user who placed each order. It's like the customer number on an invoice: it doesn't repeat all your details, it just says who you are. That way 1 user can have 10 orders without copying their name 10 times.
A materialized view is a query's result already computed and stored for later use. Since the data is pre-computed, reading it is much faster than running the query every time. It's mostly used for complex or frequently repeated queries. It's like having the month's summary already printed instead of adding up every invoice whenever someone asks. If the query takes 5 seconds, the view answers in milliseconds.
The N+1 problem happens when code runs 1 query to fetch a list and then N more, one per item, to fetch data that could have come with the first. With 100 orders, that's 101 queries instead of 1 or 2. It's like going to the supermarket 100 times to buy 1 thing per trip instead of taking the whole list. It's common with ORMs and GraphQL, and it's fixed by fetching everything in 1 query.
SQL databases have downsides too. They're expensive to maintain, and changing the schema is hard: adding a column to a table of 1 billion rows can take hours. JOINs between big tables cost performance. And they scale poorly horizontally: splitting a SQL database across several servers is complicated. It's like a brick building: solid, but adding a floor is a big job.