PostgreSQL Select : Different Ways To Retrieve Data Using Select Statement

  • Post comments:0 Comments
  • Reading time:5 mins read
  • Post category:SQL

In this post, we are going to learn about PostgreSQL Select statement. A SQL select statement retrieves information from the database. With a SELECT statement, you can use the following capabilities.

Projection: Select the columns in a table that are returned by a query. select as few or as many of the columns required.

Selection:  Select the rows in a table that are returned by a query. Various criteria can be used to restrict the rows that are retrieved.

Joining: Bring together the data that is stored in different tables by specifying the link between them by Joins.

PostgreSQL Select:

Prerequisites

For you to select data from a table or materialized view, the table or materialized view must be in your own schema or you must have the SELECT privilege on the table or materialized view.

For you to select rows from the base tables of a view:

  • You must have the SELECT privilege on the view, and
  • Whoever owns the schema containing the view must have the SELECT privilege on the base tables.

The SELECT ANY TABLE system privilege also allows you to select data from any table or any materialized view or the base table of any view.

General Guidelines

  • In each SQL statement, PostgreSQL SELECT and FROM clauses are mandatory. Without both of them, SQL statements are not valid (there is no point in seeking to display data without indicating what should be retrieved and from where).
  • The order of the PostgreSQL SELECT and FROM clauses cannot be changed, the PostgreSQL SELECT clause will always be listed first; you cannot write an Oracle SELECT statement that begins with a FROM clause.
  • It is possible to specify a column’s name multiple times; the data of this column will simply appear again and again, according to the number of times the column was specified.
  • To enhance readability – even though the PostgreSQL SQL syntax is neither case-sensitive nor sensitive to spaces or line breaks, ensure writing in an orderly manner: write the keywords in capital letters, names of columns/tables in small letters, insert a line break after each command and indents when required.

Below is the query which we use to select all the rows from a table “persons”

postgreSQL Select statement with Projection:

syntax : select {column} from table;

Multi-column projection:

Example:

PostgreSQL describe table:

In postgreSQL, a table structure can be described by using

\d tablename

Example.,

What is an alias in SQL Select statement?

Alias in SQL statement are of two types

1. table alias: is used to give an alias name for a table
2. column alias: is used to give an alias name for a column.

Example:

Consider the below table

Here, column firstname is aliased to F_N.

PostgreSQL select distinct :

Distinct is used to display unique records in SQL statement

The SELECT DISTINCT statement is used to return only distinct (different) values from a table.

consider the below example.,

Here More than one person is having the same address, so in order to retrieve the unique address that the persons are living in  we use DISTINCT.

 

Words from postgreshelp

Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestion/feedback.

If you want to be updated with all our articles

please follow us on Facebook Twitter
Please subscribe to our newsletter.

 

Leave a Reply