This SQL Cheat Sheet collects the commands a working developer reaches for daily, arranged by task. Each section pairs the exact syntax with a plain description, covering database creation, data types, querying, filtering, joining, and indexing.
🗄️ Structure Commands: CREATE DATABASE, CREATE TABLE, ALTER, RENAME and DROP define and reshape the schema.
🔢 Typy danych: Numeric, text, date and binary types set the storage size and valid range of every column.
🔎 Retrieval Commands: SELECT with WHERE, IN, LIKE and REGEXP narrows a result set to the rows that matter.
✍️. Modification Commands: INSERT, UPDATE and DELETE change stored rows, and each accepts an optional condition.
📊 Aggregation Commands: GROUP BY with COUNT, MIN, MAX, SUM and AVG summarises data, and HAVING filters the groups.
🔗 Combination Commands: JOIN merges tables side by side, while UNION stacks the results of two queries.
UPDATE table_name SET column_name = new_value [WHERE condition];
Update command syntax
ORDER BY w SQL: polecenie DESC i ASC
Command
OPIS
SELECT statement... [WHERE condition | GROUP BY field_name(s) HAVING condition] ORDER BY field_name(s) [ASC | DESC];
ORDER BY clause basic syntax
SELECT {fieldName(s) | *} FROM tableName(s) [WHERE condition] ORDER BY fieldname(s) ASC | DESC [LIMIT N];
Składnia DESC i ASC
Polecenie SQL GROUP BY i HAVING
GRUPUJ WEDŁUG collapses rows into groups, and HAVING filters those groups after they are formed.
Grupuj według
Command
OPIS
SELECT statements... GROUP BY column_name1[, column_name2, ...] [HAVING condition];
GROUP BY syntax
Grouping i funkcje agregujące
Command
OPIS
SELECT t2, COUNT(t1) FROM table1 GROUP BY t2;
Załóżmy, że chcemy mieć w naszej bazie danych całkowitą liczbę wartości kolumn t2.
Klauzula HAVING
Command
OPIS
SELECT * FROM table2 GROUP BY t1_id, t4 HAVING t1_id = x1;
Returns all the t4 values for table2 rows whose t1_id is x1.
Polecenia SQL typu Wildcards dla Like, NOT Like, Escape, (%), ( _)
Wildcards let LIKE match partial strings.
% the percentage wildcard command in MySQL
Command
OPIS
SELECT statements... WHERE fieldname LIKE 'xxx%';
Basic syntax for the % percentage wildcard, which matches any number of characters
_ polecenie podkreślenia symbolu wieloznacznego
Command
OPIS
SELECT * FROM table1 WHERE t3 LIKE 'x2_';
The underscore matches exactly one character.
NIE Podobnie jak polecenie wieloznaczne
Command
OPIS
SELECT * FROM table1 WHERE t3 NOT LIKE 'x2_';
Suppose we want the table1 rows whose t3 value does not match the pattern.
Polecenie ucieczki słowa kluczowego ze znakiem wieloznacznym
Command
OPIS
LIKE '67#%' ESCAPE '#';
Checks for the literal string “67%”, because # escapes the wildcard.
Wyrażenia regularne SQL (REGEXP)
Command
OPIS
SELECT statements... WHERE fieldname REGEXP 'pattern';
Basic syntax of a regular expression match
Wyrażenie regularne Metaznaki
Command
OPIS
*
The asterisk (*) metacharacter matches zero or more instances of the string preceding it.
+
The plus (+) metacharacter matches one or more instances of the string preceding it.
?
The question mark (?) metacharacter matches zero or one instance of the string preceding it.
.
The dot (.) metacharacter matches any single character except a new line.
[abc]
The charlist [abc] matches any of the enclosed characters.
[^abc]
The charlist [^abc] matches any character excluding the ones enclosed.
[A-Z]
The [A-Z] range matches any upper case letter.
[a-z]
The [a-z] range matches any lower case letter.
[0-9]
The [0-9] range matches any digit from 0 through to 9.
^
The caret (^) anchors the match at the beginning.
|
The vertical bar (|) isolates alternatives.
[[:<:]]
The [[:<:]] marker matches the beginning of words.
[[:>:]]
The [[:>:]] marker matches the end of words.
[:class:]
The [:class:] marker matches a character class, for example [:alpha:] for letters, [:space:] for white space, [:punct:] for punctuation and [:upper:] for upper case letters.
Polecenia funkcji SQL
Funkcje ciągów
Command
OPIS
SELECT t1_id, t2, UCASE(t2) FROM table1;
The UCASE function takes a string as a parameter and converts all the letters to upper case.
Funkcje numeryczne
Command
OPIS
Przykład
DIV
Dzielenie liczb całkowitych
SELECT 23 DIV 6;
/
podział
SELECT 23 / 6;
-
Podłożetraccja
SELECT 23 - 6;
+
Dodatek
SELECT 23 + 6;
*
Mnożenie
SELECT 23 * 6 AS multiplication_result;
% or MOD
Moduł
SELECT 23 % 6; or SELECT 23 MOD 6;
FLOOR
Removes the decimal places from a number and rounds it down to the nearest whole number.
SELECT FLOOR(23 / 6) AS floor_result;
ROUND
Rounds a number with decimal places to the nearest whole number.
SELECT ROUND(23 / 6) AS round_result;
Przechowywane funkcje
Command
OPIS
CREATE FUNCTION sf_name ([parameter(s)])
Mandatory. Tells MySQL server to create a function named sf_name with the optional parameters defined in the parentheses.
RETURNS data type
Mandatory. Specifies the data type that the function returns.
DETERMINISTIC
The function returns the same value whenever the same arguments are supplied to it.
STATEMENTS
Kod proceduralny wykonywany przez funkcję.
Polecenia funkcji agregującej SQL
Command
OPIS
SELECT COUNT(t1_id) FROM table1 WHERE t1_id = 2;
funkcja LICZENIE
SELECT MIN(t3) FROM table2;
Funkcja MIN
SELECT MAX(t3) FROM table2;
Funkcja MAX
SELECT SUM(t4) FROM table3;
Funkcja SUM
SELECT AVG(t4) FROM table3;
AVG funkcjonować
Polecenia SQL IS NULL & NOT NULL
A NULL is an absent value, so it must be tested with IS NULL rather than the equals operator.
Command
OPIS
SELECT COUNT(t3) FROM table1;
COUNT ignores NULL values in the column.
CREATE TABLE table2(
t1_number int NOT NULL,
t2_names varchar(255),
t3 varchar(6)
);
These commands change a table that already contains data.
Command
OPIS
ALTER TABLE table_name ADD COLUMN column_name data_type;
ALTER syntax
DROP TABLE sample_table;
Składnia DROP TABLE
RENAME TABLE current_table_name TO new_table_name;
RENAME command syntax
ALTER TABLE table1 CHANGE COLUMN t1_names t1name char(250) NOT NULL;
CHANGE keyword
ALTER TABLE table1 MODIFY t1name char(50) NOT NULL;
MODIFY keyword
ALTER TABLE table1 ADD t4 date NULL AFTER t3;
AFTER keyword
LIMIT I PRZESUNIĘCIE SQL
Command
OPIS
SELECT {fieldname(s) | *} FROM tableName(s) [WHERE condition] LIMIT N;
Składnia słowa kluczowego LIMIT
SELECT * FROM table1 LIMIT 1, 2;
Offset in the LIMIT query: skip 1 row, then return 2.
SQL SubQuery commands
Command
OPIS
SELECT t1_name FROM table1 WHERE category_id = (SELECT MIN(t1_id) FROM table2);
Zapytania podrzędne
Polecenia SQL JOINS
ŁĄCZY combine columns from two tables into one result set.
Command
OPIS
SELECT * FROM table1 CROSS JOIN table2;
Krzyż DOŁĄCZ
SELECT table1.t1, table1.t2, table2.t1
FROM table1, table2
WHERE table2.id = table1.table2_id;
INNER JOIN
SELECT A.t1, B.t2, B.t3
FROM table2 AS A
LEFT JOIN table1 AS B
ON B.table2_id = A.id;
LEFT JOIN
SELECT A.t1, A.t2, B.t3
FROM table1 AS A
RIGHT JOIN table2 AS B
ON B.id = A.table2_id;
WŁAŚCIWE DOŁĄCZENIE
SELECT A.t1, B.t2, B.t3
FROM table2 AS A
LEFT JOIN table1 AS B
USING (table2_id);
Klauzule „ON” i „USING”.
Polecenia UNION SQL
UNION stacks the rows of two queries, whereas a JOIN widens them into more columns.
Command
OPIS
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;
UNION syntax. Duplicate rows are removed.
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;
UNION ALL keeps duplicate rows.
Polecenia SQL w widokach
odwiedzajacy store a query under a name so it can be reused like a table.
Command
OPIS
CREATE VIEW view_name AS SELECT statement;
Składnia widoków
DROP VIEW general_v_movie_rentals;
Spadekping widoki
Polecenia indeksu SQL
An index is a lookup structure that makes searches on a column far faster.
Command
OPIS
CREATE INDEX id_index ON table_name(column_name);
Dodaj podstawową składnię indeksu
DROP INDEX index_id ON table_name;
Podstawowa składnia indeksu upuść
FAQ
No. Keywords such as SELECT and WHERE work in any case. Uppercase is a convention separating commands from identifiers. Table names, however, can be case sensitive on Linux.
DELETE removes selected rows and can be rolled back. TRUNCATE empties the whole table quickly and resets AUTO_INCREMENT. DROP removes the table definition itself.
FROM and JOIN run first, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY and finally LIMIT. This is why a column alias defined in SELECT cannot be used inside WHERE.
Yes. Text-to-SQL assistants translate a question into a query using the table schema. Always read the generated WHERE and JOIN conditions, because a plausible query can return wrong rows.
Tak. Reviewing AI output, tuning slow queries and reading an execution plan all demand fluency in the commands. A reference sheet speeds recall; it does not replace understanding.
Podsumuj ten post następująco:
Bądź na bieżąco ze sztuczną inteligencjąGet Tygodniowa AI Umiejętności, trendy, praktyczne porady.
Zapisz się do newslettera
Pomyślnie subskrybowałeś. Sprawdź swoją skrzynkę odbiorczą.