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

PostgreSQL Operators

PostgreSQL Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation.

A PostgreSQL operator is a reserved keyword or character, generally used in a WHERE statement as a filtering condition.

Common operators include:


Arithmetic operators

Assume variable a is 2 and variable b is 3, then:

operator Description Example
+ Addition a + b gives 5
- Subtraction a - b gives -1
* Multiplication a * b gives 6
/ Division b / a gives 1
% Modulo (remainder) b % a gives 1
^ Exponent a ^ b gives 8
|/ Square root |/ 25.0 gives 5
||/ the cube root ||/ 27.0 gives 3
! Factorial 5 ! gives 120
!! Factorial (prefix operator) !! 5 gives 120

Example



runoobdb=# select 2+3;

 ?column?

----------

        5

(1 row)





runoobdb=# select 2*3;

 ?column?

----------

        6

(1 row)





runoobdb=# select 10/5;

 ?column?

----------

        2

(1 row)





runoobdb=# select 12%5;

 ?column?

----------

        2

(1 row)





runoobdb=# select 2^3;

 ?column?

----------

        8

(1 row)





runoobdb=# select |/ 25.0;

 ?column?

----------

        5

(1 row)





runoobdb=# select ||/ 27.0;

 ?column?

----------

        3

(1 row)





runoobdb=# select 5 !;

 ?column?

----------

      120

(1 row)





runoobdb=# select !!5;

 ?column?

----------

      120

(1 row)


Comparison operators

Assume variable a is 10 and variable b is 20, then:

operator Description Example
= Equal (a = b) is false.
!= Not equal (a != b) is true.
<> Not equal (a <> b) is true.
> Greater than (a > b) is false.
< Less than (a < b) is true.
>= Greater than or equal to (a >= b) is false.
<= Less than or equal to (a <= b) is true.

Example

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)

Read data where the SALARY field is greater than 50000:

runoobdb=# SELECT * FROM COMPANY WHERE SALARY > 50000;

 id | name  | age |address    | salary

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

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

(2 rows)

Read data where the SALARY field equals 20000:

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

 id | name  | age |  address    | salary

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

   1 | Paul  |  32 | California  |  20000

   3 | Teddy |  23 | Norway      |  20000

(2 rows)

Read data where the SALARY field is not equal to 20000:

runoobdb=#  SELECT * FROM COMPANY WHERE SALARY != 20000;

 id | name  | age |  address    | salary

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

  2 | Allen |  25 | Texas       |  15000

  4 | Mark  |  25 | Rich-Mond   |  65000

  5 | David |  27 | Texas       |  85000

  6 | Kim   |  22 | South-Hall  |  45000

  7 | James |  24 | Houston     |  10000

(5 rows)



runoobdb=# SELECT * FROM COMPANY WHERE SALARY <> 20000;

 id | name  | age | address    | salary

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

  2 | Allen |  25 | Texas      |  15000

  4 | Mark  |  25 | Rich-Mond  |  65000

  5 | David |  27 | Texas      |  85000

  6 | Kim   |  22 | South-Hall |  45000

  7 | James |  24 | Houston    |  10000

(5 rows)

Read data where the SALARY field is greater than or equal to 65000:

runoobdb=# SELECT * FROM COMPANY WHERE SALARY >= 65000;

 id | name  | age |  address  | salary

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

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

(2 rows)


Logical operators

PostgreSQL has the following logical operators:

No. Operator & Description
1

AND

Logical AND operator. If both operands are nonzero, the condition is true.

The WHERE clause in PostgresSQL can use AND to include multiple filter conditions.

2

NOT

Logical NOT operator. Used to reverse the logical state of an operand. If the condition is true, the logical NOT operator makes it false.

PostgresSQL has operators such as NOT EXISTS, NOT BETWEEN, NOT IN.

3

OR

Logical OR operator. If either operand is nonzero, the condition is true.

The WHERE clause in PostgresSQL can use OR to include multiple filter conditions.

SQL uses a three-valued logic system, including true, false, and null, where null means "unknown".

a b a AND b a OR b
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
TRUE NULL NULL TRUE
FALSE FALSE FALSE FALSE
FALSE NULL FALSE NULL
NULL NULL NULL NULL
a NOT a
TRUE FALSE
FALSE TRUE
NULL NULL

Example

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)

Read data where the AGE field is greater than or equal to 25 and the SALARY field is greater than or equal to 6500:

runoobdb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 6500;

 id | name  | age |                      address                  | salary

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

  1 | Paul  |  32 | California                                    |  20000

  2 | Allen |  25 | Texas                                         |  15000

  4 | Mark  |  25 | Rich-Mond                                     |  65000

  5 | David |  27 | Texas                                         |  85000

(4 rows)

Read data where the AGE field is greater than or equal to 25 or the SALARY field is greater than 6500:

runoobdb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 6500;

 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

  8 | Paul  |  24 | Houston     |  20000

  9 | James |  44 | Norway      |   5000

 10 | James |  45 | Texas       |   5000

(10 rows)

Read data where the SALARY field is not NULL:

runoobdb=#  SELECT * FROM COMPANY WHERE SALARY IS NOT NULL;

 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

  8 | Paul  |  24 | Houston     |  20000

  9 | James |  44 | Norway      |   5000

 10 | James |  45 | Texas       |   5000

(10 rows)


Bitwise operators

Bitwise operators act on bits and perform operations bit by bit. The truth tables for &, |, and ^ are shown below:

p q p & q p | q
0 0 0 0
0 1 0 1
1 1 1 1
1 0 0 1

Assume A = 60 and B = 13; represented in binary format, they are as follows:

A = 0011 1100

B = 0000 1101


A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

The following table shows the bitwise operators supported by PostgreSQL. Assume variables A has a value of 60, variable B has a value of 13, then:

operator Description Example
&

Bitwise AND operation, performs the "AND" operation bit by bit. The rule is:

0&0=0;

0&1=0;
1&0=0;
1&1=1; | (A & B) will give 12, that is 0000 1100 | | | |

Bitwise OR operator, performs the "OR" operation bit by bit. The rule is:

0|0=0;
0|1=1;
1|0=1;
1|1=1;
| (A | B) gives 61, which is 0011 1101 | | # |

XOR operator, performs the "XOR" operation bit by bit. The rule is:

0#0=0;
0#1=1;
1#0=1;
1#1=0;
| (A # B) gives 49, which is 0011 0001 | | ~ |

Bitwise NOT operator, performs the "NOT" operation bit by bit. The rule is:

~1=0;
~0=1;
| (~A ) will give -61, that is 1100 0011, the two's complement form of a signed binary number. | | << | Binary left shift operator. Shifts all bits of an operand to the left by a number of positions (the bits on the left are discarded, zeros are filled on the right). | A << 2 will give 240, that is 1111 0000 | | >> | Binary right shift operator. Shifts all bits of a number to the right by a number of positions; for positive numbers zeros are filled on the left, for negative numbers ones are filled on the left, and the bits on the right are discarded. | A >> 2 will give 15, that is 0000 1111 |

Example

runoobdb=# select 60 | 13;

 ?column?

----------

       61

(1 row)





runoobdb=# select 60 & 13;

 ?column?

----------

       12

(1 row)





runoobdb=#  select  (~60);

 ?column?

----------

      -61

(1 row)





runoobdb=# select  (60 << 2);

 ?column?

----------

      240

(1 row)





runoobdb=# select  (60 >> 2);

 ?column?

----------

       15

(1 row)





runoobdb=#  select 60 # 13;

 ?column?

----------

       49

(1 row)