SELECT (Query Rows)

Last updated: 2026-11-04

SELECT (Query Rows)

SELECT is the core command for reading data in PostgreSQL, retrieving rows from one or more tables (or views). It supports WHERE filtering, GROUP BY aggregation, HAVING group filtering, ORDER BY sorting, and LIMIT pagination — and nearly every query starts with it.

Kindcommand
Example LanguageSQL

📝 Syntax

SELECT [DISTINCT] <column or expression> [AS alias]
FROM <table> [AS alias]
[WHERE <condition>]
[GROUP BY <grouping expression>]
[HAVING <group condition>]
[ORDER BY <sort column> [ASC | DESC]]
[LIMIT <rows> [OFFSET <offset>]]

⚙️ Parameters

FROMThe table or view the rows come from
WHERERow-level filter, applied before grouping
GROUP BYGrouping key that sets the aggregation grain
HAVINGFilters groups; may reference aggregates
ORDER BYSort order, ascending by default
LIMIT / OFFSETCaps the row count and skips leading rows

Returns:A result set of zero or more rows, with columns defined by the select list. No match yields an empty result set (0 rows), not NULL.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

DROP TABLE
CREATE TABLE
INSERT 0 5
name  | dept  |  salary 
------+-------+---------
Carol | sales | 15000.00
Alice | eng   | 12000.00
Dave  | sales | 11000.00
dept  | headcount | avg_salary
------+-----------+-----------
sales |         2 |   13000.00
eng   |         2 |   10500.00

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhat is the difference between WHERE and HAVING?
AWHERE filters rows before grouping, HAVING filters groups after aggregation; only HAVING may reference aggregates such as count() and sum().
QWhy can ORDER BY use a select alias but WHERE cannot?
AAn alias only exists after the select list is evaluated, and ORDER BY runs after that; WHERE runs before it, so it can only reference original columns.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏