Your first SELECT
Ask the books table for a few of its columns.
SELECT names the columns and FROM names the table. Every row comes back.
Show the title and author of every book in the library.
| book_id | title | author | genre | year | pages |
|---|---|---|---|---|---|
| 1 | Pride and Prejudice | Jane Austen | Romance | 1813 | 432 |
| 2 | The Great Gatsby | F. Scott Fitzgerald | Fiction | 1925 | 180 |
| 3 | Dracula | Bram Stoker | Horror | 1897 | 418 |
| 4 | Mockingjay | Suzanne Collins | Science Fiction | 2010 | 390 |
| 5 | The Time Machine | H.G. Wells | Science Fiction | 1895 | 118 |
| 6 | Angels & Demons | Dan Brown | Thriller | 2000 | 616 |
| 7 | Charlotte's Web | E.B. White | Children | 1952 | 184 |
| 8 | The Fault in Our Stars | John Green | Romance | 2012 | 313 |
| 9 | The Fellowship of the Ring | J.R.R. Tolkien | Fantasy | 1954 | 423 |
Run your query to see the result table.
Press the green Run button or Ctrl+Enter.
How SELECT, FROM works
What is SQL?
SQL (say "sequel" or "S-Q-L") is the language for asking a database for data. Libraries, banks, shops and apps keep their data in databases, and analysts use SQL to get answers out of them.
What is a table?
A table is like a sheet with a name: columns with headings, and one row per thing, such as a book or a loan. Every value in a column is the same kind: text, a whole number or a decimal.
How do I write a
SELECT?SELECT title,author FROM books; List the columns after
SELECT, with commas between them, then the table afterFROM. The semicolon ends the query.What does
SELECT* do?SELECT * FROM books;shows every column. It's handy for a first look at a table, but in real work list the columns you need: it's clearer and faster.Do capital letters matter?
Not for keywords:
selectworks the same asSELECT. Writing keywords in capitals is a habit that makes queries easier to read.How is this like Excel?
Picking columns is like hiding the ones you don't need, or
CHOOSECOLS. The difference: SQL never changes the table, it just shows you a result.
Know someone fighting with a spreadsheet? Send them this page.