← Python lessons

SQL SELECT and FROM

Part of a free Python course — sign up to save your progress and get AI hints.Sign up free

An SQL SELECT query reads data from a database table. SELECT says which columns to show, and FROM says which table to read them from.

The examples below use this books table. Change a query and press Run to see its result.

Table: books

titleauthorpages
WonderR. J. Palacio320
MatildaRoald Dahl240
HolesLouis Sachar272

Show every column with SELECT *

The star * means every column:

SELECT *FROM books;

The first line names all three columns: title, author and pages. Each line under it is one row of books. The ; ends the query.

Choose the columns you want

Instead of *, list the columns you want after SELECT, with a comma between them:

SELECT author, titleFROM books;

Every row is still there, but each one shows only the author and the title, in the order you listed them. A column name must match the table exactly: SELECT titel gives the error no such column: titel.

Note: SQL keywords work in any case, so select also works. Writing them in capitals makes a query easier to read.

See also: SQL ORDER BY.

Your turn

Change the query so it shows only the title and pages of every book.

🎯 Goal — make your query show exactly:
title   | pages
--------+------
Wonder  | 320
Matilda | 240
Holes   | 272
(3 rows)
-- change the * so the query shows only title and pagesSELECT *FROM books;