Tables and your first SELECT
Read how a table is laid out and use SELECT to pull columns out of it.
15 min
A database keeps data in tables. A table is a grid: each column is one kind of fact (a title, a price) and each row is one thing (one book). SQL (Structured Query Language, said "ess-queue-ell" or "sequel") is the language you use to ask a database questions.
The bookshop you will use all course has a books table:
| id | title | author | genre | price | year | stock |
|---|---|---|---|---|---|---|
| 1 | The Silent Orchard | M. Hale | fiction | 12.50 | 2018 | 30 |
| 2 | Night Trains | R. Okafor | fiction | 9.99 | 2021 | 12 |
| ... |
SELECT chooses the columns you want, and FROM says which table:
SELECT title, price FROM books;That returns every row, but only those two columns. SELECT * means every column; fine for a quick look, but name your columns in real work.
You can calculate in a query. Use AS to give the result a name:
SELECT title, price * 0.9 AS sale_price FROM books;The original table is never changed by a SELECT. A query only reads.
SQL keywords are not case-sensitive, but the convention is SELECT, FROM in capitals and names in lowercase. Put each clause on its own line when a query grows. End a statement with ;.
These lessons use SQLite, the database built into phones, browsers and most apps. Nearly everything you learn works in PostgreSQL and MySQL too; where they differ we will say so.
FROM picks the table, SELECT picks the columns, and the result is a new, temporary table.
Show each book's title and price. Run it, look at the result, then check it.
Tables you can query
- books (id, title, author, genre, price, year, stock)
SELECTShow the title of each book and a column called sale_price with the price reduced by 10 percent and rounded to 2 decimal places.
For ROUND, write ROUND(value, 2).
Tables you can query
- books (id, title, author, genre, price, year, stock)
SELECT title,“A SELECT changes the table it reads from.”
It feels like the data is being "selected" and moved or marked.
What is actually true
SELECT never modifies anything. It builds a temporary result from the table. Changing data takes other statements (INSERT, UPDATE, DELETE), which come later.
How to catch yourself
If you worry you might break something, run a SELECT: it is always safe.
- A table is rows (things) and columns (facts about them).
SELECT columns FROM table;returns the chosen columns for every row.ASrenames a column or a calculated value.- A SELECT only reads: it never changes the table.
Check the basics.
Which statement returns only the author column?
- A`SELECT author FROM books;`
- B`SELECT books FROM author;`
- C`FROM books SELECT author;`
- D`GET author books;`
What does SELECT * return?
- AOnly the first column
- BEvery column
- COnly the first row
- DNothing
Which clause names the table to read from?
- ASELECT
- BFROM
- CAS
- DWHERE
To call a calculated column total, write price * 2 total.
Capitalisation, spacing and punctuation are ignored when grading.
After SELECT title FROM books; runs, how many rows does books have?
- AFewer than before
- BThe same as before
- CZero
- DOne
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.