What Is a Database and Where Does SQL Fit?
Learn how relational databases organize data into tables, rows and columns, what primary keys do, and why SQL is the language used to ask questions.
Welcome back!
Step 1 of 12Learn
From spreadsheet chaos to a database
Maya runs a small online bookstore. At first she tracked everything in one spreadsheet: each row was an order, with the customer's name, email, city, the book title, author, price and date. It worked for the first hundred orders.
Then problems piled up. A loyal customer who ordered 20 times had her email typed 20 times, three of them with typos. When a customer moved, Maya had to find and fix every row with the old city. Two people editing the file at once overwrote each other's changes. And answering "which authors sold best in Denver last quarter?" meant an afternoon of filters and copy-paste.
A database solves these problems. It is an organized collection of data managed by software called a database management system (DBMS). Popular ones include PostgreSQL, MySQL, SQLite, Microsoft SQL Server and Oracle.
Most business data lives in relational databases, which store data in tables:
- Each table describes one kind of thing: books, customers, orders.
- Each row (or record) is one instance: one book, one customer.
- Each column (or field) is one attribute, with a fixed data type: a title is text, a price is a decimal number, an order date is a date.
Instead of repeating a customer's details on every order, the orders table stores just a customer ID that points to one row in the customers table. Change a customer's city once, and every order sees the update. This idea of linking tables through shared key values is the "relational" part, and it's the foundation for everything in this course.
Step 2 of 12Practice · Match the pairs
Match each database term to its meaning in Maya's bookstore.
Tap an answer, then tap the place where it belongs (or drag it there).
- Table
- Row
- Column
- DBMS
Step 3 of 12Practice · Diagram
Maya's bookstore database at a glance
A database holds tables; each table has columns. Tap a column to see its role.
Which column links each order to the customer who placed it?
Tap the shape that answers the question, then press Check.
- Bookstore database
- books
One row per book on sale.
- customers
One row per customer, stored once.
- orders
One row per order placed.
- book_id (primary key)
Unique number for each book.
- title
- price
- customer_id (primary key)
Unique number for each customer.
- name
- order_id (primary key)
Unique number for each order.
- customer_id
A foreign key: it points to a row in customers, so the customer's details never have to be copied into orders.
- total
Step 4 of 12Tap to reveal
Database systems you will hear about
PostgreSQL
A free, open-source database known for following the SQL standard closely and for advanced features. Widely used by startups, governments and data teams, and an excellent choice for learning.
MySQL and MariaDB
Free, open-source databases that power a huge share of websites, including many content management systems. MariaDB began as a community fork of MySQL and remains largely compatible with it.
SQLite
A tiny database stored in a single file, with no server to run. It is built into phones, browsers and countless apps, which may make it the most widely deployed database in the world. Ideal for practice.
Microsoft SQL Server and Oracle
Commercial databases common in large companies, banks and government agencies. Their dialects, T-SQL and PL/SQL, add their own extensions on top of standard SQL.
Cloud data warehouses
Services such as BigQuery, Snowflake and Redshift store huge amounts of data for analysis. You query them with SQL too, so everything in this course carries over.
Step 5 of 12Learn
Keys: how rows are identified and linked
Every well-designed table has a primary key: a column whose value uniquely identifies each row. In the books table, that's book_id. Two books can share a title (there are many books called Home), an author or a price, but never a book_id.
Primary keys have two rules:
- Unique: no two rows have the same value.
- Not null: every row must have one.
They are usually simple integers assigned automatically by the database (1, 2, 3, ...), because names and emails change and can repeat.
A foreign key is a column in one table that refers to the primary key of another table. The orders table has a customer_id column; each value must match a customer_id that exists in the customers table. That link lets the database answer questions that span tables: which customer placed order 104? How much has each customer spent?
Here is the bookstore database you'll use throughout the course:
- books(book_id, title, author, genre, price, year, stock)
- customers(customer_id, name, city, email)
- orders(order_id, customer_id, order_date, total)
In orders, order_id is the primary key and customer_id is a foreign key pointing to customers.
The database enforces these rules. If someone tries to add an order for customer 999, which doesn't exist, the database rejects it. That protection is a big reason businesses trust databases more than spreadsheets: the data can't quietly drift into nonsense.
Step 6 of 12Practice · Select all that apply
Which columns would make a good primary key for a customers table? Select all that apply.
Select all that apply.
Step 7 of 12Learn
What SQL is
SQL (Structured Query Language, pronounced "S-Q-L" or "sequel") is the standard language for working with relational databases. It was developed in the 1970s and standardized in the 1980s, and it's still everywhere: in banking systems, phone apps, analytics dashboards and data science notebooks.
SQL is declarative. You describe what you want, not how to get it:
SELECT title, price
FROM books
WHERE genre = 'Sci-Fi';This says: give me the title and price of every Sci-Fi book. You don't tell the database which file to open, how to loop through rows or which index to use. The DBMS has a query optimizer that works out an efficient plan for you.
SQL statements fall into a few families:
- Querying: SELECT, to read data. Most of this course is about SELECT, because it's what analysts and most developers write daily.
- Changing data: INSERT, UPDATE and DELETE.
- Defining structure: CREATE TABLE, ALTER TABLE and DROP TABLE.
- Controlling access and transactions: GRANT, COMMIT, ROLLBACK.
A few conventions:
- Keywords are not case-sensitive:
selectandSELECTwork the same. Writing keywords in capitals is a common style that makes queries easier to read. - Text values go in single quotes:
'Sci-Fi'. Double quotes are for identifiers in standard SQL. - A semicolon ends a statement.
- Whitespace and line breaks don't matter, so put each clause on its own line for readability.
Every DBMS speaks a dialect of SQL. The core you'll learn here works almost identically everywhere; differences show up in details like date functions and how to limit rows.
Step 8 of 12Compare
One big spreadsheet vs. a relational database
| Aspect | Single spreadsheet | Relational database |
|---|---|---|
| Customer details | Copied onto every order row | Stored once in customers |
| Changing an email | Edit every row, hope you found them all | Update one row |
| Rules on data | Anything can be typed anywhere | Types, keys and constraints are enforced |
| Many users at once | Conflicting copies and overwrites | Built for concurrent access |
| Asking questions | Filters and manual formulas | SQL queries across linked tables |
Step 9 of 12Practice · Sort into groups
Sort each SQL statement into its family.
Tap an item, then tap its group (or drag it there).
Step 10 of 12Note
Step 11 of 12Practice · True or false
In SQL, the keywords SELECT and select mean the same thing.
Step 12 of 12Practice · Choose one
Why does Maya's orders table store a customer_id instead of the customer's name, email and city on every row?
Lesson complete!