REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
PostgreSQL SELECT statement
PostgreSQL SELECT statement is used to extract records from one or more tables into PostgreSQL.
In its simplest form, the syntax for the SELECT statement in PostgreSQL
SELECT_id
FROM tabs
[WHERE conds];
The full syntax for the SELECT PostgreSQL statement
SELECT [ ALL | DISTINCT | DISTINCT ON (distinct_expressions_id) ]
expressions_id
FROM tabs
[WHERE conds]
[GROUP BY_id]
[HAVING cond]
[ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS FIRST | NULLS LAST ]]
[LIMIT [ number_rows | ALL]
[OFFSET offset_value [ ROW | ROWS ]]
[FETCH { FIRST | NEXT } [ fetch_rows ] { ROW | ROWS } ONLY]
[FOR { UPDATE | SHARE } OF table [ NOWAIT ]];
Parameters and arguments of the statement
- ALL – Optional. Returns all matching strings.
- DISTINCT – Optional. Removes duplicates from the result set. Learn more about the DISTINCT operator.
- DISTINCT ON – Optional. Removes duplicates based on different_expressions. Learn more about the DISTINCT ON operator.
- expressions_id – The table or calculations you want to get. Use * if you want to select all columns.
- tabs – The tables from which you want to get the records. At least one table must be specified in the FROM statement.
- WHERE conds – Optional. The conditions that must be met for the records to be selected.
- GROUP BY_id – Optional. It collects data from several records and groups the results into one or more columns.
- HAVING cond – Optional. It is used in combination with GROUP BY to limit groups of returned rows to only those whose TRUE condition.
- ORDER BY expression_id – Optional. It is used to sort the entries in your resulting set.
- LIMIT – Optional. If LIMIT is specified, it controls the maximum number of records to extract. The maximum number of records specified by the number_rows will be returned in the resulting set. The first line returned by LIMIT will be defined as offset_value.
- FETCH – Optional. If FETCH is specified, it controls the maximum number of records to extract. At most, the maximum number of records specified by fetch_rows will be returned in the resulting set. The first line returned by FETCH will be defined as offset_value.
- FOR UPDATE – Optional. Records affected by the query are blocked from being written until the transaction is completed.
- FOR SHARE – Optional. Records affected by a query may be used by other transactions, but cannot be updated or deleted by those other transactions.
Example: select all fields from one table
Let’s see how to use PostgreSQL query SELECT to select all fields in a table.
SELECT *
FROM categories
WHERE category_id >= 2500
ORDER BY category_id ASC;
In this example of the SELECT PostgreSQL statement, we used * to specify that we want to select all fields from the category table where category_id is greater than or equal to 2500. The resulting set is sorted by category_id in ascending order.
Example: select individual fields from one table
You can also use PostgreSQL statement SELECT to select individual fields from a table, unlike all fields from a table.
For example:
SELECT category_id, category_name, comments
FROM categories
WHERE category_name = 'Hardware'
ORDER BY category_name ASC, comments DESC;
This PostgreSQL example SELECT will only return the category_id, category_name, and comments fields from the category table, where category_name is ‘Hardware’. The results are sorted by category_name in ascending order and then by comments in descending order.
Example: selecting fields from several tables
You can also use the SELECT operator of PostgreSQL to extract fields from multiple tables.
SELECT products.product_name, categories.category_name
FROM categories
INNER JOIN products
ON categories.category_id = products.category_id
ORDER BY product_name;
This PostgreSQL SELECT example connects two tables to get the resulting set, which displays the product_name and category_name fields where the category_id value matches in both the categories and product tables. The results are sorted by product_name in ascending order.
PostgreSQL: Select From | Course
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...