SQL Cheat Sheet
Complete SQL reference: SELECT, JOINs, aggregation, subqueries, and functions.
Data Querying (SELECT)
SELECT * FROM table;Select all columnsSELECT col1, col2 FROM table;Select specific columnsSELECT DISTINCT col FROM table;Select unique valuesSELECT * FROM t WHERE col = val;Filter rows with WHERESELECT * FROM t WHERE col LIKE '%pat%';Pattern matching with LIKESELECT * FROM t WHERE col IN (1, 2, 3);Filter by list of valuesSELECT * FROM t WHERE col BETWEEN 10 AND 20;Filter by rangeSELECT * FROM t WHERE col IS NULL;Filter NULL valuesSELECT * FROM t ORDER BY col ASC;Sort results ascendingSELECT * FROM t ORDER BY col DESC;Sort results descendingSELECT * FROM t LIMIT 10;Limit number of rows returnedSELECT * FROM t LIMIT 10 OFFSET 20;Pagination — skip 20, return 10Aggregation
SELECT COUNT(*) FROM table;Count all rowsSELECT COUNT(col) FROM table;Count non-NULL valuesSELECT SUM(col) FROM table;Sum of column valuesSELECT AVG(col) FROM table;Average of column valuesSELECT MIN(col), MAX(col) FROM table;Minimum and maximumSELECT col, COUNT(*) FROM t GROUP BY col;Group and countSELECT col, COUNT(*) FROM t GROUP BY col HAVING COUNT(*) > 5;Filter groups with HAVINGJOINs
SELECT * FROM a INNER JOIN b ON a.id = b.a_id;Inner join — matching rows onlySELECT * FROM a LEFT JOIN b ON a.id = b.a_id;Left join — all rows from aSELECT * FROM a RIGHT JOIN b ON a.id = b.a_id;Right join — all rows from bSELECT * FROM a FULL OUTER JOIN b ON a.id = b.a_id;Full outer join — all rowsSELECT * FROM a CROSS JOIN b;Cartesian product of both tablesData Manipulation
INSERT INTO t (c1, c2) VALUES (v1, v2);Insert a rowINSERT INTO t (c1, c2) VALUES (v1, v2), (v3, v4);Insert multiple rowsUPDATE t SET col = val WHERE id = 1;Update specific rowsDELETE FROM t WHERE id = 1;Delete specific rowsDELETE FROM t;Delete all rows (keep table)TRUNCATE TABLE t;Delete all rows fast (DDL)Table & Schema
CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100));Create a tableDROP TABLE t;Delete a tableALTER TABLE t ADD col INT;Add a columnALTER TABLE t DROP COLUMN col;Remove a columnALTER TABLE t RENAME TO new_name;Rename a tableCREATE INDEX idx ON t (col);Create an indexDROP INDEX idx;Remove an indexSubqueries & CTEs
SELECT * FROM t WHERE id IN (SELECT id FROM other);Subquery in WHERESELECT * FROM (SELECT * FROM t LIMIT 5) sub;Subquery in FROM (derived table)WITH cte AS (SELECT * FROM t) SELECT * FROM cte;Common Table Expression (CTE)SELECT *, ROW_NUMBER() OVER (PARTITION BY col ORDER BY date) FROM t;Window functionUseful Functions
COALESCE(col, default_val)Return first non-NULL valueNULLIF(a, b)Return NULL if a = b, else aCASE WHEN cond THEN val ELSE other ENDConditional expressionCAST(col AS VARCHAR)Type castingCONCAT(str1, str2)Concatenate stringsUPPER(col) / LOWER(col)Change caseTRIM(col)Remove leading/trailing whitespaceLENGTH(col)String lengthSUBSTRING(col, 1, 5)Extract substringNOW() / CURRENT_TIMESTAMPCurrent date and timeDATE_TRUNC('month', col)Truncate date to month (PostgreSQL)À propos de cet outil
Référence rapide SQL couvrant SELECT, INSERT, UPDATE, DELETE, les JOINs, l'agrégation et les fonctions courantes.
Utilisez le champ de recherche pour filtrer les commandes instantanément par mot-clé.
Comment utiliser
- Parcourez les sections ou tapez un mot-clé dans le champ de recherche.
- La liste se filtre instantanément pendant la saisie.
- Utilisez le snippet SQL directement dans votre éditeur de requêtes ou votre terminal.
- Effacez la recherche pour afficher à nouveau toutes les sections.
Cas d'utilisation
Utile pour les développeurs qui écrivent des requêtes, les étudiants qui apprennent SQL et les ingénieurs lors de revues de code ou de migrations.
Questions fréquentes
- Couvre-t-il PostgreSQL, MySQL et SQLite? — La plupart de la syntaxe est ANSI SQL et fonctionne sur les principales bases de données. Les fonctions spécifiques au dialecte sont signalées.
- Puis-je rechercher une commande spécifique? — Oui — saisissez n'importe quel mot-clé ou fonction dans le champ de recherche et la liste se filtre instantanément.
- La liste est-elle exhaustive? — Non. Elle couvre le SQL quotidien le plus courant. Consultez la documentation officielle pour les fonctionnalités avancées.