A database is an organised store of data that a program can search and update quickly. The most common kind is a relational database, which keeps data in tables. A table looks like a spreadsheet: each row is one record, such as one customer, and each column is one piece of information about it, such as their name or city.
SQL is the language you use to work with those tables. You write a short statement such as "select the names from the customers table", and the database sends back the matching rows. SQL reads very close to English, which is why people with all kinds of jobs, not just programmers, learn it.
In this lesson you'll learn the vocabulary of databases, meet the example shop database used throughout this course, and run your first query.
Tables, rows and columns
Here is the customers table from our example shop:
| id | name | city | joined |
|---|---|---|---|
| 1 | Ana | Lisbon | 2023-02-10 |
| 2 | Ben | Leeds | 2023-05-22 |
| 3 | Chloe | Lyon | 2024-01-03 |
| 4 | Dev | Leeds | 2024-03-15 |
| 5 | Ema | Porto | 2024-06-30 |
- The table has 5 rows, one per customer.
- It has 4 columns:
id,name,cityandjoined. - Every column has a data type:
idholds whole numbers,nameandcityhold text, andjoinedholds dates.
Primary keys
Two customers could share a name, so each row needs something that is guaranteed to be unique. That's the job of the primary key, usually a column called id. No two rows can have the same primary key, and it never changes.
Other tables refer to a customer by this number. For example, an orders table stores customer_id = 4 instead of copying Dev's name and city into every order. Linking tables through keys is what makes a database relational, and you'll use it in the JOIN lesson.
The example shop database
This course uses three small tables. You'll meet products and orders properly in later lessons.
- customers: id, name, city, joined
- products: id, name, category, price, stock
- orders: id, customer_id, product_id, quantity, order_date
To practise on your own computer, install SQLite with a viewer such as DB Browser for SQLite, create the tables, and type the queries from each lesson into its "Execute SQL" tab.
Your first query
SELECT * FROM customers;SELECTsays you want to read data.*means all columns.FROM customersnames the table.- The semicolon marks the end of the statement. Some tools let you leave it out, but it's a good habit.
SQL keywords aren't case-sensitive, so select * from customers works too. Writing keywords in CAPITALS is a common convention that makes them stand out from table and column names.
SQL Syntax
Basic SELECT
SELECT * FROM table_name;SQL Summary
- A relational database stores data in tables of rows and columns.
- Each row is one record; each column is one field with a data type.
- A primary key, usually
id, uniquely identifies every row. - SQL is the language for reading and changing data in tables.
SELECT * FROM table;returns every row and column of a table.
SQL Examples
Each example shows the code, the output it produces and what each part does.
Read a whole table
SELECT * FROM customers;id | name | city | joined
---+-------+--------+-----------
1 | Ana | Lisbon | 2023-02-10
2 | Ben | Leeds | 2023-05-22
3 | Chloe | Lyon | 2024-01-03
4 | Dev | Leeds | 2024-03-15
5 | Ema | Porto | 2024-06-30Line-by-line explanation
- Line 1Every column (*) from every row of the customers table.
The products table
SELECT * FROM products;id | name | category | price | stock
---+--------------+------------+-------+------
1 | Notebook | Stationery | 3.50 | 120
2 | Pen | Stationery | 1.20 | 300
3 | Backpack | Bags | 34.99 | 15
4 | Water bottle | Kitchen | 12.00 | 40
5 | Lunch box | Kitchen | 8.75 | 0
6 | Desk lamp | Home | 24.50 | 22Line-by-line explanation
- Line 1The same pattern works for any table. Only the name after FROM changes.
A tiny table definition
This is how the customers table could be created in SQLite. You'll rarely type this by hand, but it shows where data types and the primary key come from.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
joined DATE
);Table "customers" created.Line-by-line explanation
- Line 2id is the primary key, so every row must have a different id.
- Line 3NOT NULL means a name must always be given.
- Lines 4 to 5city and joined are optional.
Common SQL Mistakes
Forgetting FROM
SELECT * customers;SELECT * FROM customers;Without FROM the database doesn't know which table you mean and reports a syntax error.
Spelling the table name wrongly
SELECT * FROM customer;SELECT * FROM customers;Table names must match exactly, so the first query fails with "no such table".
SQL in Real Projects
SQL behind the scenes
When you open your orders page on a shopping site, the server runs a query like "select the orders where customer_id is yours" and turns the rows into the page you see. Data analysts run similar queries to answer questions such as "which product sold best last month?", which you'll learn to write in this course.
SQL FAQ
Is SQL a programming language?
SQL is a declarative query language: you describe the result you want, such as "customers in Leeds ordered by name", and the database decides how to find it. It isn't used to build whole applications on its own, but most databases add procedural features, and SQL is a core skill in almost every programming and data job.
What is the difference between SQL and MySQL?
SQL is the language. MySQL, PostgreSQL, SQLite, SQL Server and Oracle are database systems that understand it. They all share the same core, such as SELECT, WHERE, JOIN and GROUP BY, and each adds its own functions and small syntax differences, which this course points out where they matter.
How long does it take to learn SQL?
With regular practice, most people can write useful SELECT queries with filters, sorting and grouping within a couple of weeks, and joins and subqueries within a month or two. Database design, performance tuning and advanced features take longer and are best learned while working on real data.
Is SQL case-sensitive?
Keywords aren't: SELECT, select and Select all work, though writing keywords in capitals is a common convention. Whether table and column names, and text comparisons such as 'Leeds' = 'leeds', are case-sensitive depends on the database and its settings.
What is the difference between SQL and NoSQL databases?
SQL databases store data in tables with a defined structure and use joins to connect related tables. NoSQL databases, such as MongoDB or Redis, store documents, key-value pairs or graphs with more flexible structures. Relational databases remain the standard choice for business data that needs consistency, like orders and payments.
SQL Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the query so it returns every column from the products table.
* products;
In a database table, what is a row?
Why does almost every table have an id primary key?
How many rows does SELECT * FROM customers; return for the example shop?
Which column is the best primary key for a customers table?
SQL Quiz
5 questions to check what you learned.
What does SQL stand for?
- Simple Query Logic
- Sequential Question Language
- Structured Query Language
- Server Quick Lookup
What does
*mean inSELECT * FROM products?- Multiply the columns
- All columns
- All tables
- The first row
Which statement about SQL keywords is true?
- They must always be uppercase
- They must always be lowercase
- Only SELECT is case-sensitive
- They aren't case-sensitive, but uppercase is a common convention
What is a relational database?
- A database that stores data in tables that can be linked by keys
- A database that stores only numbers
- A spreadsheet file
- A database for family trees
What does SQL stand for?
- Simple Query Logic
- System Quick List
- Structured Query Language
- Sorted Question Language