Readineer
INTRODUCTIONSQL Patterns / Getting started

Introduction to Databases and SQL

Before learning individual SQL patterns, it helps to understand what a database is, how relational databases organise information, and what SQL lets you do with it.

10 min read7 sections
01Foundation

What is a database?

Before databases were widely used, applications often stored information directly in files. Imagine a telephone directory kept in a file named telephone_directory.csv:

telephone_directory.csv
person_idfirst_namelast_namephone_numberaddresscity
1AshaRao987654321012 Lake RoadBengaluru
2BenThomas912345678045 Park StreetBengaluru
3CarlaShah998877665518 Hill ViewMumbai
4DevMehta900001234512 Lake RoadBengaluru

For a small amount of information, a file works perfectly well: a program opens it, reads the contents, and searches for the record it needs. As the data grows and more applications and users work with it, several problems appear.

Why plain files struggle as data grows

Every question needs custom code

A file stores data but offers no way to ask questions of it. Each new question (“Asha’s number,” “who lives at 12 Lake Road?”) means writing another file-scanning program.

Search slows as the file grows

Reading millions of records to find a match is slow. Databases build indexes that locate matching rows efficiently, by name, phone, address, or city.

Safe updates are hard

Changing one number means finding the record, rewriting the file, and not corrupting the rest, especially when several users write at once.

Duplication drifts out of sync

When the same details are repeated across rows, updating one copy and missing another leaves conflicting information.

Files enforce no rules

Nothing stops a duplicate identifier, a missing name, or an invalid phone number. Databases define constraints that reject bad data.

Relationships aren’t guaranteed

Nothing ensures a person_id in one file exists in another. A relational database enforces the link and rejects orphaned records.

Access is all-or-nothing

File permissions grant the whole file or none of it. Databases grant access per table, per operation, per view, and per role.

Recovery after a crash is hard

A crash mid-write can leave a half-updated file. Databases use transactions, logs, and backups so a set of changes completes fully or is safely undone.

Two problems, made concrete

Duplication drifts into conflict

Storing a row per phone number repeats Asha’s name and address. If she moves and only one row is updated, the file now disagrees with itself:

same person, two addresses
first_namephone_numberaddress
Asha987654321025 River Road
Asha988888888812 Lake Road

A relational database stores the person once and connects several phone numbers to that single record.

Files enforce no rules

Suppose every person must have a unique id, a name, a valid phone number, and a city. A plain file happily accepts none of that:

invalid records a file won’t catch
person_idfirst_namephone_numbercity
1Asha9876543210Bengaluru
1BenunknownNULL
3NULL9988776655Mumbai

The id 1 is duplicated, unknown is not a phone number, Ben’s city is missing, and the third record has no name. A database’s constraints can require values, forbid duplicate identifiers, and restrict data types.

02The tool

What a database provides

A database is an organised collection of related information. The software that stores, retrieves, updates, and protects it is a Database Management System, or DBMS: for example PostgreSQL, MySQL, Microsoft SQL Server, Oracle Database, or SQLite.

A DBMS helps applications store structured information, search large volumes quickly, update records safely, support many concurrent users, reduce duplication, enforce data-quality rules, maintain relationships, control access, and recover from failure.

Key idea
Files are still useful for documents, exports, logs, and small datasets. Databases earn their place when growing, related data must be searched, updated, validated, shared, and protected by many users at once. One of the most widely used ways to organise that data is the relational model.
03The relational model

Information organised into related tables

In the relational model, information lives in tables. Each table represents one kind of thing, holding rows (individual records) and columns (their attributes). An orders table might look like this:

Orders
order_idcustomer_idproduct_idquantityorder_datestatus
1001110122026-07-01paid
1002210312026-07-02pending
1003110232026-07-03shipped
1004310112026-07-04cancelled

Product and customer details do not need to be repeated inside every order. They live in their own tables, connected by shared identifiers.

Products
product_idproduct_namecategoryprice
101Wireless MouseAccessories750
102Mechanical KeyboardAccessories3200
103Office ChairFurniture8500
104Monitor StandFurniture1800
Customers
customer_idcustomer_namecitycountry
1AshaBengaluruIndia
2BenLondonUK
3CarlaDubaiUAE

Order 1001 stores product_id = 101 and customer_id = 1. Looking those up tells us Asha ordered a Wireless Mouse, without repeating her details or the product’s in every order row.

How the tables relate: keys

The three tables describe one business process, Customers → Orders → Products, linked through shared identifying columns called keys.

Primary key

Uniquely identifies each row in a table, for example customers.customer_id or orders.order_id. No two rows share the same value.

Foreign key

Refers to a row in another table. orders.customer_id points at customers.customer_id, and orders.product_id points at products.product_id.

Key idea
Store each fact once and connect tables by key. If Asha moves from Bengaluru to Mumbai, her city changes in exactly one place, and every order still points to the right customer.

Thinking in rows and columns

Most questions become questions about tables, rows, columns, and relationships:

  • Which orders have been paid? Filter orders by status.
  • Which products cost more than 2,000? Filter products by price.
  • Which products has Asha ordered? Combine customers, orders, and products.
  • How much revenue did each product generate? Combine orders and products, compute an amount, and group by product.
04The language

What is SQL?

SQL (pronounced “S-Q-L” or “sequel”) stands for Structured Query Language, the language commonly used to work with relational databases. It lets you describe the information you want without spelling out every step to retrieve it.

Return products over 2,000
SELECT product_id, product_name, price
FROM products
WHERE price > 2000;

The query states which columns you want, which table holds them, and which rows to include. The database decides how to run it efficiently. This is what it means to call SQL a declarative language.

Imperative: every step

Open the table, read the first row, check the price, keep it when above 2,000, repeat until every row is checked.

Declarative: the result

“The id, name, and price of products priced above 2,000.” You state what you want; the database works out the steps.

05Capabilities

What you can do with SQL

SQL does more than read data. It can filter, sort, combine, summarise, add, update, remove, and even define the structure of information in a relational database.

Retrieve columns
SELECT product_name, price
FROM products;
Filter rows
SELECT product_name, price
FROM products
WHERE price > 2000;
Sort results
SELECT product_name, price
FROM products
ORDER BY price DESC;
Combine tables
SELECT o.order_id, c.customer_name, p.product_name
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
JOIN products  AS p ON p.product_id  = o.product_id;
Summarise data
SELECT status, COUNT(*) AS order_count
FROM orders
GROUP BY status;
Add data
INSERT INTO products (product_id, product_name, category, price)
VALUES (105, 'Laptop Sleeve', 'Accessories', 1200);
Update data
UPDATE orders
SET status = 'shipped'
WHERE order_id = 1002;
Remove data
DELETE FROM orders
WHERE order_id = 1004;
Create structures
CREATE TABLE products (
  product_id   INTEGER PRIMARY KEY,
  product_name VARCHAR(100),
  category     VARCHAR(50),
  price        DECIMAL(10, 2)
);

Because of this, SQL is used by software engineers, data analysts, data engineers, database administrators, data scientists, and business teams alike. The same core ideas appear across PostgreSQL, MySQL, SQL Server, Oracle, SQLite, Snowflake, BigQuery, and DuckDB; the exact syntax varies, but the relational concepts stay the same.

06A useful mental model

A SQL query returns another table

An important idea: the result of a query is itself a table. Running:

Query
SELECT product_id, product_name, price
FROM products
WHERE price > 2000;
Result
product_idproduct_nameprice
102Mechanical Keyboard3200
103Office Chair8500

Nothing was removed from the database. The query produced a new table containing only the requested rows and columns. As queries grow, they follow the same shape: begin with one or more tables, filter unwanted rows, combine related tables, calculate new values, group similar rows, sort, and return a new table.

07The path ahead

What comes next

We build SQL knowledge gradually, from the operations used in everyday queries toward more advanced analytical patterns.

  1. Selecting columns and reading tables. SELECT … FROM …, aliases, removing duplicates, and the shape of a result.
  2. Filtering rows with WHERE. Comparisons, AND/OR, IN, LIKE, BETWEEN, NULL, precedence, and date filters.
  3. Sorting and limiting results. ORDER BY, ASC/DESC, LIMIT, and database-specific alternatives.
  4. Calculations and expressions. Order totals, combining text, CASE categories, missing values, and type conversions.
  5. Aggregate functions and grouping. COUNT, SUM, AVG, MIN, MAX, GROUP BY, and HAVING.
  6. Joining related tables. INNER JOIN, LEFT JOIN, multi-table joins, missing matches, and duplicate rows.
  7. Subqueries and CTEs. Scalar and correlated subqueries, EXISTS/NOT EXISTS, and WITH.
  8. Window functions. Ranking, running totals, comparing to a previous row, and rolling averages.
  9. Set operations. UNION, UNION ALL, INTERSECT, and EXCEPT.
  10. Modifying data safely. Insert, update, delete, transactions, and not touching more rows than intended.
  11. Designing reliable SQL queries. The mistakes that return believable but incorrect results: grouping, join fan-out, NULL, date boundaries, and type mismatches.

The goal is not only SQL that runs. It is SQL that expresses the business requirement clearly, returns the correct result, and stays understandable when someone reads it later.

Up next · Pattern 01
Filtering rows with the WHERE clause

With this foundation in place, we begin with one of the most frequently used SQL operations: keeping exactly the rows you meant.

Continue to P01 →Browse all patterns