SQL Tutorial for Beginners: Your First Query
SQL (Structured Query Language, pronounced "ess-cue-ell" or "sequel") is the language for asking questions of relational databases. Banks, online shops, phone apps and analytics tools all keep their data in tables and use SQL to read and change it. It is one of the most durable skills in tech: queries written decades ago still run today.
Tables, rows and columns
A relational database stores data in tables. Each table describes one kind of thing, such as authors, and has a fixed set of named columns like name and country. Each row is one record: one author. Every row in our authors table has an id, a whole number that uniquely identifies it, called the primary key. Other tables refer to authors by that id, which is how tables are linked.
The sample database for this track is a small, fictional bookstore with five tables:
authors(id, name, country, born)books(id, title, author_id, genre, price, year, stock)customers(id, name, city, email, joined)orders(id, customer_id, ordered_on)order_items(order_id, book_id, qty)
Your first query
SELECT * FROM authors; means "give me every column (*) of every row in the authors table". Press Run to execute it. The first time, your browser loads SQLite compiled to WebAssembly and builds the sample database in memory; nothing is sent to a server.
Expected output
id | name | country | born 1 | Maya Lindqvist | Sweden | 1974 2 | Daniel Okafor | Nigeria | 1981 3 | Rosa Delgado | Mexico | 1968 4 | Tom Becker | USA | 1990 5 | Aiko Tanaka | Japan | 1985
The expected output shows the result in the plain text format of the sqlite3 command-line tool: a header line with column names, then one line per row, with values separated by |. The runner shows the same result as a table.
How SQL reads
SQL is declarative: you describe the result you want, and the database works out how to get it. Keywords such as SELECT and FROM are not case-sensitive, but writing them in capitals is a strong convention that makes queries easy to scan. Table and column names are usually lowercase. A semicolon ends each statement, and -- starts a comment that runs to the end of the line.
Common mistakes
- Misspelling a table name, which gives
no such table. - Using double quotes for text. In SQL, text values use single quotes:
'Sweden'. Double quotes are for identifiers such as column names. - Forgetting the semicolon between two statements.
Summary
- Relational data lives in tables of rows and columns.
SELECT * FROM table;returns every column of every row.- Keywords are written in capitals by convention; text values use single quotes.
Quiz: Get started