Retrieve rows from one or more tables.
The fundamental statement for retrieving data from one or more tables. It is the primary way to filter, transform, and shape data before presenting it. Watch out for SELECT * in production -- it is fragile to schema changes and can pull more data than needed.
Compatibility
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | Native | all | Use LIMIT at end |
| PostgreSQL | Native | all | LIMIT/OFFSET |
| SQL Server | Native | 2005 | Use TOP or OFFSET FETCH |
| Oracle | Native | 12c | OFFSET/FETCH |
| SQLite | Native | all | Uses LIMIT/OFFSET |
Details
Core retrieval statement, with dialect-specific paging syntax.
Standard Syntax
SELECT *
FROM table_name
WHERE condition
ORDER BY column
LIMIT 10;
Version Support
MySQL: Native in all listed versions
PostgreSQL: Native in all listed versions
SQL Server: Native since 2005
Oracle: Native since 12c
SQLite: Native in all listed versions
Per-Database Syntax & Notes
MySQL Native syntax
Use LIMIT at end
SELECT * FROM tbl WHERE … ORDER BY col LIMIT 10 OFFSET 5;
PostgreSQL Native syntax
LIMIT/OFFSET
SELECT * FROM tbl WHERE … ORDER BY col LIMIT 10 OFFSET 5;
SQL Server Native syntax
Use TOP or OFFSET FETCH
SELECT TOP 10 * FROM tbl WHERE … ORDER BY col;
-- or with OFFSET FETCH:
SELECT * FROM tbl WHERE … ORDER BY col
OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY;
Oracle Native syntax
OFFSET/FETCH
SELECT * FROM tbl WHERE … ORDER BY col
OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY;
SQLite Native syntax
Uses LIMIT/OFFSET
SELECT * FROM tbl WHERE … ORDER BY col LIMIT 10 OFFSET 5;