In this PostgreSQL post explains how to use the PostgreSQL SELECT statement with syntax and examples.
The PostgreSQL SELECT statement is used to retrieve records from one or more tables in PostgreSQL.
In its simplest form, the syntax for the SELECT statement in PostgreSQL is:
However, the full syntax for the PostgreSQL SELECT statement is:
Optional. Returns all matching rows.
Optional. Removes duplicates from the result set. Learn more about DISTINCT clause
Optional. Removes duplicates based on the distinct_expressions. Learn more about the DISTINCT ON clause.
The columns or calculations that you wish to retrieve. Use * if you wish to select all columns.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. The conditions that must be met for the records to be selected.
Optional. It collects data across multiple records and groups the results by one or more columns.
Optional. It is used in combination with the GROUP BY to restrict the groups of returned rows to only those whose the condition is TRUE.
Optional. It is used to sort the records in your result set.
Optional. If LIMIT is provided, it controls the maximum number of records to retrieve. At most, the number of records specified by number_rows will be returned in the result set. The first row returned by LIMIT will be determined by offset_value.
Optional. If FETCH is provided, it controls the maximum number of records to retrieve. At most, the number of records specified by fetch_rows will be returned in the result set. The first row returned by FETCH will be determined by offset_value.
Optional. Records affected by the query are write-locked until the transaction has completed
Optional. Records affected by the query can be used by other transactions but can not be updated or deleted by those other transactions
Let's look at how to use a PostgreSQL SELECT query to select all fields from a table.
In this PostgreSQL SELECT statement example, we've used * to signify that we wish to select all fields from the categories table where the category_id is greater than or equal to 2500. The result set is sorted by category_id in ascending order.
You can also use the PostgreSQL SELECT statement to select individual fields from the table, as opposed to all fields from the table.
For example:
This PostgreSQL SELECT example would return only the category_id, category_name, and comments fields from the categories table where the category_name is 'Hardware'. The results are sorted by category_name in ascending order and then comments in descending order.
You can also use the PostgreSQL SELECT statement to retrieve fields from multiple tables.
This PostgreSQL SELECT example joins two tables together to gives us a result set that displays the product_name and category_name fields where the category_id value matches in both the categories and products table. The results are sorted by product_name in ascending order.