The SELECT statement is fundamental for retrieving data from databases. Here's a concise overview:
🧩 Basic Syntax
SELECT column1, column2, ...
FROM table_name
[WHERE condition]
[ORDER BY column]
[LIMIT number]
- Use
SELECT
to specify columns you want to retrieve FROM
defines the table sourceWHERE
filters records (optional)ORDER BY
sorts results (optional)LIMIT
restricts the number of rows (optional)
✅ Common Operations
- Filtering: Use
WHERE
with conditions like=
,>
,<
,LIKE
- Sorting:
ORDER BY
withASC
(ascending) orDESC
(descending) - Pagination: Combine
LIMIT
andOFFSET
for large datasets - Aggregation: Use
GROUP BY
withCOUNT
,SUM
,AVG
etc.
📖 Example
SELECT name, age
FROM users
WHERE age > 25
ORDER BY name ASC
LIMIT 10;
This query retrieves the first 10 users over 25 years old, sorted alphabetically.
⚠️ Best Practices
- Avoid using
SELECT *
for performance optimization - Use specific column names instead of
*
- Index columns used in
WHERE
clauses - Limit results when testing or debugging
For more details on SQL syntax, visit our SQL Reference Guide.