SQL SELECT: read data from a table
SELECT is the first statement everyone learns in SQL, and the one you will write most often. It is used to read data: show the content of a table, pick a few columns, compute new values. It never changes the database, so you can try it without any risk.
In this lesson, you will see how to write a SELECT query, how to choose the columns to display, and why the famous SELECT * is handy… but not always a good idea.
Syntax
SELECT column1, column2, …
FROM table_name;How it works
A SELECT query reads almost like a sentence: “select these columns from this table”.
SELECTlists the columns to display, separated by commas. They appear in the order you write them.FROMnames the table that holds these columns.- The star
*means “all columns”.SELECT * FROM produitstherefore shows the whole table. - The semicolon
;ends the query. It is required when you chain several queries.
SQL keywords are not case-sensitive: select and SELECT work the same way. The convention is to write keywords in upper case and table and column names in lower case, so the query is easy to read.
Why avoid SELECT * in an application?
SELECT * is perfect for exploring a table. In a real application, you usually list the columns you need: the query transfers less data, and it does not break if someone adds or reorders columns in the table.
Diagram
| id | nom | categorie | prix |
|---|---|---|---|
| 1 | Clavier | Informatique | 49 |
| 2 | Plaid | Maison | 25 |
| 3 | Gourde | Sport | 12 |
| nom | prix |
|---|---|
| Clavier | 49 |
| Plaid | 25 |
| Gourde | 12 |
Simplified diagram to show the idea. The examples below use the real SQL Quest practice database (a fictional online shop: see the full schema).
Glossary: the sample database uses French names
clients- customers
commandes- orders
lignes_commande- order lines
produits- products
fournisseurs- suppliers
employes- employees
prenom / nom- first name / last name (or name)
ville- city
pays- country
prix- price
prix_unitaire- unit price
quantite- quantity
categorie- category
date_commande- order date
poste- job title
departement- department
salaire- salary
statut- status: livrée = delivered, expédiée = shipped, en cours = in progress, annulée = cancelled
Examples with their results
1. Show a whole table
The star shows every column of the fournisseurs (suppliers) table.
SELECT * FROM fournisseurs; Try it in the sandbox| id | nom | pays |
|---|---|---|
| 1 | TechNova | France |
| 2 | ElectroPlus | Allemagne |
| 3 | MaisonDeco | France |
| 4 | SportZone | Espagne |
| 5 | BookWorld | Belgique |
| 6 | GreenFood | Italie |
| 7 | AsiaParts | Chine |
7 rows in total
2. Choose your columns
Only the name and price of each product, in that order.
SELECT nom, prix FROM produits; Try it in the sandbox| nom | prix |
|---|---|
| Clavier mécanique | 15 |
| Souris sans fil | 103 |
| Écran 27" | 342 |
| Webcam HD | 101 |
| SSD 1To | 317 |
| Casque gaming | 98 |
| Hub USB-C | 49 |
| Lampe LED | 51.74 |
| Coussin | 10.86 |
| Plaid | 29.23 |
29 rows in total · 10 shown
3. Change the column order
The column order in the result follows the order written after SELECT, not the order of the table.
SELECT ville, nom, prenom FROM clients; Try it in the sandbox| ville | nom | prenom |
|---|---|---|
| Lille | Dubois | Samir |
| Lille | Martin | Samir |
| Paris | Laurent | Zoé |
| Toulouse | Lefebvre | Rayan |
| Marseille | Laurent | Léa |
| Bordeaux | Michel | Louis |
| Casablanca | Dubois | Adam |
| Montréal | Lambert | Omar |
| Bordeaux | Faure | Zoé |
| Lyon | Faure | Alice |
120 rows in total · 10 shown
Common mistakes
Forgetting the comma between two columns
Without a comma, SQL reads the second word as an alias (a new name) for the first column. The query does not fail: it returns a single column, which is even more misleading.
SELECT prenom nom FROM clients;SELECT prenom, nom FROM clients;Leaving a comma before FROM
A comma after the last column causes a syntax error.
SELECT prenom, nom, FROM clients;SELECT prenom, nom FROM clients;Misspelling the table name
Names must be exact. In our database, tables are plural: clients, produits, commandes…
SELECT * FROM client;SELECT * FROM clients;Exercises
Try writing the query yourself before opening the solution.
Challenge 1. Show the email address and the city of every customer.
Show a solution
SELECT email, ville FROM clients;| ville | |
|---|---|
| samir.dubois1@mail.com | Lille |
| samir.martin2@mail.com | Lille |
| zoe.laurent3@mail.com | Paris |
| rayan.lefebvre4@mail.com | Toulouse |
| lea.laurent5@mail.com | Marseille |
| louis.michel6@mail.com | Bordeaux |
| adam.dubois7@mail.com | Casablanca |
| omar.lambert8@mail.com | Montréal |
120 rows in total · 8 shown
Try it in the sandboxChallenge 2. Show the last name, job title and salary of every employee.
Show a solution
SELECT nom, poste, salaire FROM employes;| nom | poste | salaire |
|---|---|---|
| Dubois | PDG | 9,500 |
| Lefebvre | Directeur commercial | 6,200 |
| Roux | Directrice technique | 6,800 |
| Simon | Responsable RH | 4,800 |
| Simon | Commercial | 4,800 |
| Martin | Commercial | 3,400 |
| Moreau | Commercial | 4,600 |
| Lambert | Commercial | 4,200 |
25 rows in total · 8 shown
Try it in the sandboxThe interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.
Frequently asked questions
Does SELECT change the database?
No. SELECT only reads data. To change a table, you use INSERT, UPDATE or DELETE.
Do I have to write SELECT in upper case?
No, SQL keywords are not case-sensitive. Upper case is a convention that makes queries easier to read.
Is the semicolon required?
For a single query, most tools accept it without one. It becomes required as soon as you chain several queries, so get into the habit of always adding it.
In what order are the rows returned?
Without ORDER BY, the row order is not guaranteed. If the order matters, add an ORDER BY clause (see the dedicated lesson).