Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL SELECT Statement

Postgres support the SELECT statement to retrieve records from the zero or more tables or views in the database.

Syntax:
SELECT [ * | column1, column2,... | expression ]
    [ FROM [table1 [, table2,..] [,view1, view2..]
    [ WHERE condition ]
    [ GROUP BY [ column1 [,column2,..]]]
    [ HAVING condition ]
    [ WINDOW window_name AS ( window_definition )]
    [ ORDER BY expression [ ASC | DESC | USING operator ] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]
    [ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF table_name] [ NOWAIT | SKIP LOCKED ]]

With the SELECT statement,

  • Specify one or more column names after the SELECT clause for which you want to retrieve the data. Multiple column names should be separated by a comma.
  • Optionally specify expression also with the SELECT clause.
  • Specify asterisk(*) after SELECT to select all the columns. The select list can also contain expressions or literal values.
  • Specify one or more table names from which you want to select data after the FROM clause.
  • Optionally, use WHERE, GROUP BY, HAVING, ORDER BY, WINDOW, LIMIT, OFFSET, FETCH, FOR clause.

SQL keywords are case-insensitive, so you can write SELECT, Select, or select. For formatting purposes, we will keep all SQL keywords in capital letters.

Let's see how to use the SELECT statement to fetch the data from the following employee table.

The following SELECT statement retrieves the data from the single column of the employee table.

Example: SELECT Query
SELECT first_name FROM employee;

The following shows the result in pgAdmin.

The following SELECT statement retrieves the data from multiple columns of the employee table.

Example: SELECT Query
SELECT emp_id, first_name, last_name FROM employee;

The following shows the result in pgAdmin.

The following SELECT statement uses * to retrieve the data from all columns of the employee table.

Example: SELECT Query
SELECT * FROM employee;

The following shows the result in pgAdmin.

Expression with SELECT Clause

You can use the expression in the SELECT statement. The following concatenates first_name and last_name columns using the || operator.

Example: SELECT Query
SELECT first_name || ' ' || last_name FROM employee;

The following shows the result in pgAdmin.

It can also return the result of the basic math operation.

Example: SELECT Query
SELECT 6 * 4;

Column Alias

An alias lets you assign a temporary name to a column or expression in the SELECT statement. Alias makes a column more readable in the result set of the SELECT query. Column alias exists only for the duration of the query.

The following query renames the first_name column to Name and last_name to Surname in the result.

Example: Column Alias in SELECT Query
SELECT first_name AS Name, last_name Surname FROM employee;

The following shows the result in pgAdmin.

Use double-quotes (") to include space or change the case of the column alias name, as shown below.

Example: Column Alias
SELECT first_name AS "First Name", last_name "Surname"
FROM employee;

The following shows the result in pgAdmin.

Thus, you can use the SELECT statement to retrieve data from the table. You will learn how to retrieve data from multiple tables in the join section.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.