SELECT produit, prix FROM produits;SQL cheat sheet
Find the right syntax, then practise it.
Eleven progressive references, from first queries to window functions.
1. Read and select
Control exactly which columns and rows are returned.
SELECT DISTINCT categorie FROM produits;LIMIT 102. Filter
Narrow the scope before analysing.
WHERE montant > 100WHERE ville IN ('Tunis', 'Sfax')WHERE montant BETWEEN 50 AND 2003. Sort
Make a ranking explicit and reproducible.
ORDER BY date_commande ASCORDER BY montant DESCORDER BY ville ASC, montant DESC4. Aggregate
Turn rows into metrics.
COUNT(*)SUM(montant)GROUP BY villeHAVING SUM(montant) > 5005. Join tables
Connect entities through their keys.
INNER JOIN commandes c ON c.id_client = clients.id_clientLEFT JOIN commandes c ON c.id_client = clients.id_client6. Handle NULL
A missing value is neither zero nor an empty string.
WHERE telephone IS NULLCOALESCE(telephone, 'Not provided')montant / NULLIF(quantite, 0)7. Structure an analysis
Break a long query into named steps.
WITH ventes_ville AS (...) SELECT * FROM ventes_ville;CASE WHEN montant >= 200 THEN 'High' ELSE 'Standard' END8. Analyse over time
Prepare advanced metrics used by Data Analysts.
DATE_TRUNC('month', date_commande)SUM(montant) OVER (ORDER BY date_commande)ROW_NUMBER() OVER (PARTITION BY ville ORDER BY montant DESC)9. Work with text
Clean and combine labels without losing missing values.
CONCAT(prenom, ' ', nom)LOWER(email)TRIM(reference)SUBSTRING(code FROM 1 FOR 3)10. Understand order and errors
SQL logically evaluates FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY and then LIMIT.
Check the schema and spellingGroup or aggregate every selected columnUse NULLIF(divisor, 0)Check commas, parentheses and single quotes11. Spot syntax differences
The same operation may use different syntax depending on the system.
Here and MySQL: LIMIT · SQL Server: TOPHere: || · MySQL: CONCAT · SQL Server: +Here: CURRENT_DATE · MySQL: CURDATE() · SQL Server: GETDATE()Syntax sticks through practice.
Use the free editor or choose a guided exercise with validation.