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)Über dieses Tool
Schnellreferenz für SQL mit SELECT, INSERT, UPDATE, DELETE, JOINs, Aggregation und gängigen Funktionen.
Nutzen Sie das Suchfeld, um Befehle sofort nach Stichwörtern zu filtern.
Anleitung
- Durchsuchen Sie die Abschnitte oder geben Sie ein Stichwort in das Suchfeld ein.
- Die Liste filtert sofort beim Tippen.
- Fügen Sie den SQL-Schnipsel in Ihren Abfrage-Editor oder das Terminal ein.
- Leeren Sie die Suche, um alle Abschnitte wieder anzuzeigen.
Anwendungsfälle
Nützlich für Entwickler, die Ad-hoc-Abfragen schreiben, Studierende, die SQL lernen, und Ingenieure bei Code-Reviews oder Datenbankmigrationen.
Häufige Fragen
- Deckt es PostgreSQL, MySQL und SQLite ab? — Die meiste Syntax ist ANSI SQL und funktioniert in den gängigen Datenbanken. Dialektspezifische Funktionen sind vermerkt.
- Kann ich nach einem bestimmten Befehl suchen? — Ja — geben Sie ein Schlüsselwort oder eine Funktion ein, und die Liste filtert sofort.
- Ist die Liste vollständig? — Nein. Sie enthält den gebräuchlichsten SQL-Alltag. Für erweiterte Funktionen lesen Sie die offizielle Dokumentation.