SELECT chooses, FROM indicates source
A read request usually begins with SELECT. The FROM clause then indicates the table consulted.
SQL keywords are often written in capital letters for readability, but SQL generally does not require this capitalization.
SELECT *
FROM produits;Select only useful columns
SELECT * is useful for exploring a small table, but a query for a report must name its columns.
The result becomes clearer, transfers less data and is more resilient to the future addition of new columns to the table.
- Separate the columns with a comma.
- The order written in SELECT becomes the order of the result.
- A mistake in a column name causes an explicit error.
SELECT produit, prix
FROM produits;Rename a column with AS
An alias only changes the title displayed in the result. It does not rename the column in the base.
Aliases are useful for a calculation, export, or report intended for a non-technical person.
SELECT
produit AS nom_produit,
prix AS prix_dt
FROM produits;Common errors
Most early errors are easy to correct by reading the message returned by the database.
- Forget the comma between two columns.
- Write the name of a non-existent table.
- Confusing text quotes and column names.
- Believing that SELECT modifies the data: a simple read does not change it.
Build a stable result for other tools
A query often powers a dashboard, an export or an application. The list of columns then becomes a contract: their name, order and type must remain predictable. This is why SELECT * is rarely a good choice in durable processing.
Use business aliases when technical names are unclear, but keep the convention consistent. An alias like chiffre_affaires is more explicit than total1 and makes it easier to reread several months later.
To check your result, separately check the number of records returned, the expected columns, and a few known values. This simple check quickly detects a bad table or misinterpreted column.
- Select only what will be used.
- Give understandable and stable aliases.
- Check the shape of the result before continuing.
SELECT
id_commande AS commande_id,
date_commande,
montant AS montant_dt
FROM commandes;Check your understanding
Show only produit and categorie from table produits.
SELECT -- your columns
FROM produits;Progress
Have you completed this lesson?
Your choice stays in this browser and also syncs with your account when you are signed in.