Skip to content
Lesson 1 of 10 · SQL for Beginners

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:

idtitleauthorgenrepriceyearstock
1The Silent OrchardM. Halefiction12.50201830
2Night TrainsR. Okaforfiction9.99202112
...

SELECT chooses the columns you want, and FROM says which table:

sql1 line
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:

sql1 line
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.

Exercise1 check
sql

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)
starter code1 line
SELECT
Exercise1 check
sql

Show 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)
starter code1 line
SELECT title,
Common misconception

“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.
  • AS renames a column or a calculated value.
  • A SELECT only reads: it never changes the table.
Check yourself

Check the basics.

1Multiple choice

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;`
2Multiple choice

What does SELECT * return?

  • AOnly the first column
  • BEvery column
  • COnly the first row
  • DNothing
3Multiple choice

Which clause names the table to read from?

  • ASELECT
  • BFROM
  • CAS
  • DWHERE
4Fill the blanks

To call a calculated column total, write price * 2 total.

Capitalisation, spacing and punctuation are ignored when grading.

5Multiple choice

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.