Essential SQL queries
| Command | Description |
|---|---|
SELECT * FROM table | Select all columns |
SELECT col1, col2 FROM table | Select specific columns |
SELECT DISTINCT col FROM table | Unique values only |
SELECT COUNT(*) FROM table | Count rows |
| Command | Description |
|---|---|
WHERE col = value | Exact match |
WHERE col > value | Comparison |
WHERE col LIKE "%text%" | Pattern match |
WHERE col IN (1, 2, 3) | Match any in list |
WHERE col IS NULL | Check for null |
WHERE a AND b | Multiple conditions |
WHERE a OR b | Either condition |
| Command | Description |
|---|---|
ORDER BY col ASC | Sort ascending |
ORDER BY col DESC | Sort descending |
LIMIT 10 | Limit results |
OFFSET 20 | Skip rows |
| Command | Description |
|---|---|
INNER JOIN t2 ON t1.id = t2.id | Match in both tables |
LEFT JOIN t2 ON t1.id = t2.id | All from left + matches |
RIGHT JOIN t2 ON t1.id = t2.id | All from right + matches |
| Command | Description |
|---|---|
INSERT INTO table (cols) VALUES (vals) | Insert row |
UPDATE table SET col = val WHERE ... | Update rows |
DELETE FROM table WHERE ... | Delete rows |
| Command | Description |
|---|---|
GROUP BY col | Group rows |
HAVING COUNT(*) > 5 | Filter groups |
SUM(col), AVG(col), MAX(col) | Aggregate functions |