Query

SQL SELECT

Query rows from one or more tables.

Availability: Common SQL with references for SQL standard, MySQL/MariaDB, PostgreSQL, SQL Server, SQLite and Oracle Database; syntax and feature support vary by engine and version.中文

Syntax

SELECT [DISTINCT] column_list FROM table_name [WHERE condition] [GROUP BY columns] [HAVING condition] [ORDER BY columns] [LIMIT count];

Examples

Example (standard-style syntax). Verify supported keywords in your database.

SELECT name, COUNT(*) AS total FROM users WHERE active = TRUE GROUP BY name HAVING COUNT(*) > 1 ORDER BY total DESC;

SQLite-oriented variant; dialect behaviour may differ.

SELECT name, COUNT(*) AS total FROM users WHERE active = 1 GROUP BY name HAVING COUNT(*) > 1 ORDER BY total DESC;

Parameters, options and clauses

Option / clauseDescription
DISTINCTRemove duplicate result rows.
WHEREFilter rows before grouping.
GROUP BY / HAVINGAggregate rows and filter grouped results.
ORDER BYSort the result set.
LIMIT / OFFSET / FETCHSet a row limit and offset; dialect syntax varies.

Dialect references

Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory