SELECT
,, and
Syntax
SELECT [DISTINCT] columns FROM table [JOIN ...] [WHERE conditions] [GROUP BY columns [HAVING conditions]] [ORDER BY columns [ASC|DESC]] [LIMIT offset, count];
Parameters
| Parameters | Description | Examples | Level |
|---|---|---|---|
WHERE |
SELECT * FROM users WHERE status = 1; |
Common | |
JOIN |
SELECT * FROM orders o JOIN users u ON o.user_id = u.id; |
Common | |
GROUP BY |
SELECT status, COUNT(*) FROM orders GROUP BY status; |
Common | |
ORDER BY |
SELECT * FROM orders ORDER BY created_at DESC; |
Common | |
LIMIT |
SELECT * FROM users LIMIT 10 OFFSET 20; |
Common | |
HAVING |
SELECT user_id, COUNT(*) c FROM orders GROUP BY user_id HAVING c > 5; |
Advanced |
Examples
+
SELECT id, username, email, created_at FROM users WHERE status = 1 AND created_at >= '2026-01-01' ORDER BY created_at DESC LIMIT 20 OFFSET 0;
20, OFFSET 0
JOIN
SELECT o.order_no, o.total_amount, u.username, o.created_at FROM orders o INNER JOIN users u ON o.user_id = u.id WHERE o.status = 1 ORDER BY o.created_at DESC LIMIT 10;
INNER JOIN
SELECT DATE(created_at) AS order_date,
COUNT(*) AS order_count,
SUM(total_amount) AS daily_total
FROM orders
WHERE status IN (1, 2, 3)
GROUP BY DATE(created_at)
ORDER BY order_date DESC
LIMIT 30;and
SELECT username, email FROM users WHERE id IN ( SELECT user_id FROM orders GROUP BY user_id HAVING COUNT(*) >= 10 );
10
Common Errors
ERROR 1054 (42S22): Unknown column 'xxx' in 'where clause', and
ERROR 1055: 'column' isn't in GROUP BY (sql_mode=ONLY_FULL_GROUP_BY)SELECT GROUP BY, ANY_VALUE()
Tips
- SELECT *, and
- WHERE id > last_id LIMIT 20 LIMIT offset, 20()
- JOIN