Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PostgreSQL Expressions

PostgreSQL Expressions

An expression is composed of one or more values, operators, and PostgresSQL functions.

A PostgreSQL expression is similar to a formula; we can apply it in a query statement to find the result set in the database that matches specified conditions.

Syntax

The syntax of the SELECT statement is as follows:

SELECT column1, column2, columnN

FROM table_name

WHERE [CONDITION | EXPRESSION];

PostgreSQL expressions can be of different types, which we will discuss next.

boolean expression

A Boolean expression reads data based on a specified condition:

SELECT column1, column2, columnN

FROM table_name

WHERE SINGLE VALUE MATCHTING EXPRESSION;

Create the COMPANY table (Download the COMPANY SQL file ), with the following data:

runoobdb# select * from COMPANY;

 id | name  | age | address   | salary

----+-------+-----+-----------+--------

  1 | Paul  |  32 | California|  20000

  2 | Allen |  25 | Texas     |  15000

  3 | Teddy |  23 | Norway    |  20000

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

  6 | Kim   |  22 | South-Hall|  45000

  7 | James |  24 | Houston   |  10000

(7 rows)

The following uses a Boolean expression (SALARY=10000) to query data:

runoobdb=# SELECT * FROM COMPANY WHERE SALARY = 10000;

 id | name  | age | address  | salary

----+-------+-----+----------+--------

  7 | James |  24 | Houston  |  10000

(1 row)

Numeric Expressions

Numeric expressions are commonly used for mathematical operations in query statements:

SELECT numerical_expression as  OPERATION_NAME

[FROM table_name WHERE CONDITION] ;

numerical_expression is a mathematical operation expression, as shown in the example below:

runoobdb=# SELECT (17 + 6) AS ADDITION ;

 addition 

----------

       23

(1 row)

In addition, PostgreSQL has some built-in mathematical functions, such as:

The following example queries the total number of records in the COMPANY table:

runoobdb=# SELECT COUNT(*) AS "RECORDS" FROM COMPANY;

 RECORDS

---------

       7

(1 row)

Date Expressions

A date expression returns the current system date and time and can be used for various data operations. The following example queries the current time:

runoobdb=# SELECT CURRENT_TIMESTAMP;

       current_timestamp       

-------------------------------

 2019-06-13 10:49:06.419243+08

(1 row)