By the end of this lesson you will be able to
- explain, in your own words, what a database is
- describe how a database keeps its information in tables made of rows and columns
- say what SQL is, and why so many people learn it
- run a piece of SQL on this website for the first time
Information that needs a home
Think of a small bookshop in Calicut. Every day it needs to know which books it has, how much each one costs, how many copies are on the shelf, who its customers are, and what each customer has ordered. The owner could write all of this in a notebook, and for a week or two that would work. But after a year there would be thousands of lines, and simple questions would take an afternoon to answer: Which books by Charles Dickens do we have? Which customers have not ordered anything this year? What did we sell in March?
A database is an organised collection of information, kept on a computer so that it can be stored safely, searched quickly and kept up to date. Almost every organisation you deal with keeps one: a bank keeps your account and every payment; a school keeps its pupils, subjects and marks; a railway keeps its trains, seats and bookings; an online shop keeps its products, customers and orders. When you look up your train ticket or your exam result on a website, a database is answering a question behind the scenes.
Tables, rows and columns
Most databases keep their information in tables. A table looks very much like a page of a spreadsheet or a ruled register: a grid with a heading at the top of each column. Here are the first few entries in the bookshop's table of books:
| book_id | title | author | genre | price | stock |
|---|---|---|---|---|---|
| 1 | Pride and Prejudice | Jane Austen | Novel | 350.0 | 12 |
| 2 | Emma | Jane Austen | Novel | 320.0 | 4 |
| 3 | Great Expectations | Charles Dickens | Novel | 399.0 | 7 |
| 8 | The Adventures of Sherlock Holmes | Arthur Conan Doyle | Mystery | 275.0 | 15 |
- Each row (going across) holds everything about one thing: here, one book. A database person may also call a row a record.
- Each column (going down) holds one kind of information about every row: every book's title, or every book's price. A column may also be called a field.
- Each column has a name, such as
titleorprice. Column names are usually written in small letters, with an underscore_instead of a space, as inbook_id. - The
book_idcolumn gives every book its own number, which no other book shares. That number is how the database tells two rows apart, even if two books had the same title. You will learn much more about these numbers, called keys, in Module 6.
A database usually has several tables, one for each kind of thing it keeps. The bookshop in this course has four: books, customers, orders (one row for each order a customer places), and order_items (which books were in each order, and how many). Keeping each kind of thing in its own table, and linking the tables by those ID numbers, is the idea at the heart of what is called a relational database, the most widely used kind.
For a short list, a spreadsheet is fine. A database becomes the better tool when the information grows large, when many people or programs use it at the same time, when mistakes must be prevented (for example, a price that is not a number, or an order for a customer who does not exist), and when you need to answer new questions quickly. A database can search millions of rows in a moment, and it checks the rules you give it every time data is added or changed.
What is SQL?
SQL is the language used to work with relational databases. The letters stand for Structured Query Language. A query is a question you ask the database, such as “show me every book that costs less than 250 rupees”, and SQL is the precise way of writing it down so that the database can answer. Some people say the name letter by letter, “S-Q-L”; others say “sequel”. Both are correct.
SQL does more than ask questions. With it you can also add new rows, change and remove rows, and create new tables. You will do all of these in this course.
SQL was created in the 1970s and has been the standard ever since, which makes it one of the most useful and longest-lasting skills in computing. It is used by programmers, of course, but also by accountants, scientists, librarians, analysts and managers: anyone who needs answers from a large amount of information.
Many different database systems understand SQL. Among the best known are MySQL, PostgreSQL, SQLite, Microsoft SQL Server and Oracle. They all share the same core language, which is what this course teaches, but each has small differences of its own, a little like British and American English. This course uses SQLite, a small but complete database system that is built into countless phones, apps and web browsers. Where another system does something differently, the lesson will tell you.
How this course works
This is a text course with no videos, but it is not a course you only read. Every lesson has boxes of real SQL, right here on the page, connected to a real database. You read them, run them, change them, and see what happens.
Try it now: tap the blue Run button below the box. The database's answer will appear underneath it as a table.
You have just asked a database a question and received its answer: the title, author and price of every book the bookshop sells. You do not need to understand that line yet; Lesson 1.2 takes it apart piece by piece. For now, try changing price to stock and pressing Run again.
Above the code there is a bar called Tables in this database. Tap it to see every table in the practice database and the names of their columns. It will be useful whenever you need to check the exact name of a column.
The database runs inside your own web browser, using SQLite. Nothing is installed and nothing is sent to a server. Each time you press Run, the page builds a brand-new copy of the practice database, runs your SQL on it, and throws that copy away afterwards. So even when you later learn to change and delete data, you can experiment freely: the next Run always starts fresh.
What you will learn
This course has three practice databases, all small enough to understand at a glance: the bookshop you have just seen, a school with pupils, subjects and marks, and a small library with books, members and loans. Using them, you will learn to:
- choose the columns and rows you want, and sort them (Modules 1 to 3)
- calculate new values and summarise whole tables, such as totals and averages (Modules 4 and 5)
- combine information from several tables in one answer (Modules 6 and 7)
- add, change and remove data safely (Module 8)
- design and create tables of your own, finishing with a complete library database (Modules 9 and 10)
Each lesson assumes only what earlier lessons taught, so if a word is unfamiliar, it will have been explained already or will be explained where it first matters.
Quick check
Three questions to confirm the main ideas before you move on.
1. In a table of books, what does one row hold?
A row goes across the table and holds everything about one thing, here one book. A column goes down and holds one kind of information (such as the price) for every row.
2. What is a query?
SQL stands for Structured Query Language: a query is a question you ask the database. The column that gives each row its own number is called a key, which you will meet again in Module 6.
3. This course uses SQLite. What does that mean for what you learn?
SQLite is one of several database systems that understand SQL. They share the same core language and differ only in details, which the lessons point out. Nothing needs installing: SQLite runs inside your browser on these pages.
Summary
- A database is an organised collection of information, kept on a computer so that it can be stored safely, searched quickly and kept up to date.
- A relational database keeps its information in tables. Each row holds everything about one thing; each column holds one kind of information, and has a name.
- A database usually has several tables, one for each kind of thing, linked by ID numbers.
- SQL (Structured Query Language) is the language for asking a database questions, called queries, and for changing its data and its tables.
- Every SQL box on this course runs on a fresh copy of a real practice database inside your browser. Nothing needs to be installed, and nothing you type can break anything.
Found a mistake on this page, or something unclear? Report a problem and mention “SQL Lesson 1.1”.
