SQL Syntax Cheatsheet
SQL syntax from SELECT and JOIN to aggregation and table management
Your data never leaves the browser — all processing is done locally 0 server requests
Basic Queries (SELECT)
-
SELECT * FROM users;Select all columns from a table (selecting only needed columns is recommended in practice)
-
SELECT name, email FROM users;Select specific columns only
-
SELECT * FROM users WHERE age >= 20;Filter rows by condition (combine with AND/OR/NOT)
-
SELECT * FROM users ORDER BY created_at DESC;Sort results (ASC for ascending / DESC for descending)
-
SELECT * FROM users LIMIT 10;Return only 10 rows (MySQL/PostgreSQL; SQL Server uses TOP 10)
-
SELECT DISTINCT city FROM users;Return unique values, removing duplicates
-
SELECT name AS 이름 FROM users;Alias a column with a custom name
Conditions & Patterns
-
WHERE name LIKE '김%'Match values starting with 김 ('%@gmail.com' matches values ending with it)
-
WHERE status IN ('active', 'pending')Match rows where the value is one of the listed options
-
WHERE price BETWEEN 1000 AND 5000Match rows within a range (inclusive of both ends)
-
WHERE deleted_at IS NULLCheck for NULL using IS NULL / IS NOT NULL, not =
-
SELECT COALESCE(nickname, name) FROM users;Return the first non-NULL value from the list
-
CASE WHEN age >= 20 THEN '성인' ELSE '미성년' ENDReturn different values based on a condition
Aggregation & Grouping
-
SELECT COUNT(*) FROM orders;Count rows (COUNT(col) excludes NULLs)
-
SELECT SUM(amount), AVG(amount) FROM orders;Calculate sum and average (MIN/MAX work the same way)
-
SELECT dept, COUNT(*) FROM emp GROUP BY dept;Aggregate results grouped by a column
-
GROUP BY dept HAVING COUNT(*) > 5;Filter aggregated results (WHERE filters before, HAVING filters after aggregation)
-
SELECT ROUND(AVG(score), 1) FROM exams;Round the average to one decimal place
Joins
-
SELECT * FROM a INNER JOIN b ON a.id = b.a_id;Return only rows that match in both tables
-
SELECT * FROM a LEFT JOIN b ON a.id = b.a_id;Keep all rows from the left table, attach matching rows from right (NULL if no match)
-
SELECT * FROM a RIGHT JOIN b ON a.id = b.a_id;Keep all rows from the right table
-
SELECT name FROM a UNION SELECT name FROM b;Combine two result sets (UNION ALL keeps duplicates and is faster)
-
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id)Filter rows based on whether the subquery returns any results
Data Modification
※ UPDATE and DELETE without a WHERE clause affect all rows. Always run a SELECT with the same condition first to verify the target rows.
-
INSERT INTO users (name, email) VALUES ('김철수', 'kim@a.com');Insert a new row
-
INSERT INTO users (name) VALUES ('a'), ('b'), ('c');Insert multiple rows in a single statement
-
UPDATE users SET status = 'active' WHERE id = 3;Update values in rows matching the condition
-
DELETE FROM users WHERE id = 3;Delete rows matching the condition
-
TRUNCATE TABLE logs;Quickly remove all rows from a table ※ often cannot be rolled back
Tables & Indexes
-
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL);Create a new table
-
ALTER TABLE users ADD COLUMN age INT;Add a column to an existing table
-
ALTER TABLE users DROP COLUMN age;Drop a column from a table
-
DROP TABLE users;Drop a table along with its structure ※ caution
-
CREATE INDEX idx_users_email ON users(email);Create an index to improve query performance
-
ALTER TABLE orders ADD FOREIGN KEY (user_id) REFERENCES users(id);Add a foreign key constraint
SQL: Understand the Execution Order
SQL processes queries in this order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. The writing order differs from the execution order — understanding this resolves most aggregation errors and "column not found" confusions.
JOINs are the most important concept to master. INNER JOIN returns only matched rows (intersection). LEFT JOIN keeps all rows from the left table. Understanding these two covers the majority of real-world query needs.
⚠ UPDATE and DELETE statements without a WHERE clause affect every row in the table. Always run the equivalent SELECT with the same condition first to preview which rows will be affected.
FAQ
- What is the difference between WHERE and HAVING?
- WHERE filters rows before grouping/aggregation. HAVING filters the results after GROUP BY aggregation. If you need to filter on COUNT or SUM values, use HAVING — WHERE cannot reference aggregate functions.
- Why do LEFT JOIN results sometimes contain NULL values?
- LEFT JOIN returns all rows from the left table. When no matching row exists in the right table, those columns are filled with NULL. If you only want matched rows, use INNER JOIN instead.
- Does SQL syntax differ between databases?
- Core syntax (SELECT, JOIN, GROUP BY) is standardized and works across most databases. However, LIMIT vs TOP, string concatenation, date functions, and window functions vary between MySQL, PostgreSQL, SQL Server, and Oracle. This page uses MySQL/PostgreSQL syntax.