SQL
SQL query syntax and common operations
Database
Basic Queries
SELECT * FROM tableSelect all columns
SELECT col1, col2 FROM tableSelect specific columns
SELECT DISTINCT col FROM tableUnique values only
SELECT * FROM table LIMIT 10Limit results
Filtering
WHERE col = 'value'Equals
WHERE col != 'value'Not equals
WHERE col > 10Greater than
WHERE col LIKE '%text%'Pattern match
WHERE col IN (1, 2, 3)In list
WHERE col BETWEEN 1 AND 10Range
WHERE col IS NULLNull check
AND / ORCombine conditions
Sorting & Grouping
ORDER BY col ASCSort ascending
ORDER BY col DESCSort descending
GROUP BY colGroup rows
HAVING COUNT(*) > 1Filter groups
Joins
INNER JOIN t2 ON t1.id = t2.idMatching rows only
LEFT JOIN t2 ON t1.id = t2.idAll left + matching
RIGHT JOIN t2 ON t1.id = t2.idAll right + matching
FULL OUTER JOINAll rows from both
Aggregates
COUNT(*)Count rows
SUM(col)Sum values
AVG(col)Average value
MIN(col) / MAX(col)Min/max value
Data Modification
INSERT INTO t (col) VALUES (val)Insert row
UPDATE t SET col = val WHEREUpdate rows
DELETE FROM t WHEREDelete rows
TRUNCATE TABLE tDelete all rows
Table Operations
CREATE TABLE t (col TYPE)Create table
ALTER TABLE t ADD col TYPEAdd column
DROP TABLE tDelete table
CREATE INDEX idx ON t(col)Create index