Postgresql - List of Commands: August 20, 2019
Postgresql - List of Commands: August 20, 2019
Contents
1 About This Document 1
5 List of Keywords 8
5.1 ALTER TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2 AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.3 AS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.4 ASC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.5 ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.6 AVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.7 BETWEEN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.8 CALLED ON NULL INPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.9 CASCADE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.10 CASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.11 CAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.12 CHR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.13 COALESCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.14 CONCAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.15 COUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.16 CREATE FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.17 CREATE OR REPLACE FUNCTION . . . . . . . . . . . . . . . . . . . . . 15
5.18 CREATE SCHEMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.19 CREATE SCHEMA IF NOT EXISTS . . . . . . . . . . . . . . . . . . . . . 16
5.20 CREATE TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.21 CREATE TABLE ... AS SELECT ... FROM ... . . . . . . . . . . . . . . . . 16
5.22 CREATE TABLE ... AS TABLE ... . . . . . . . . . . . . . . . . . . . . . . . 17
5.23 CREATE TABLE ... AS TABLE ... WITH NO DATA . . . . . . . . . . . . 17
5.24 CROSS JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.25 DELETE FROM ... WHERE ... . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.26 DESC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.27 DROP SCHEMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.28 DROP SCHEMA IF NOT EXISTS . . . . . . . . . . . . . . . . . . . . . . . 20
5.29 DROP TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.30 DROP TABLE IF NOT EXISTS . . . . . . . . . . . . . . . . . . . . . . . . 20
5.31 EXCEPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.32 EXCEPT ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.33 FILTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.34 FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.35 FULL JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.36 FULL OUTER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.37 GROUP ... BY ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.38 GROUP ... BY ... HAVING ... . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.39 IF EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.40 ILIKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.41 IMMUTABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.42 INNER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.43 INTERSECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.44 INTERSECT ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.45 IN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.46 INITCAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.47 INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.48 INSERT INTO ... VALUES(...) . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.49 INSERT INTO ... SELECT ... FROM ... . . . . . . . . . . . . . . . . . . . . 28
5.50 INSERT INTO ... TABLE ... . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.51 JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.52 LEFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.53 LEFT JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.54 LEFT OUTER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.55 LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.56 LIKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.57 LIMIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.58 LOWER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.59 MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.60 MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.61 NOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.62 NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.63 NULLIF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.64 OFFSET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.65 ON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.66 OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.67 ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.68 OUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.69 OVERLAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.70 pg tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.71 PLACING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.72 POSITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.73 POSIX Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.74 REGEXP REPLACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.75 REPLACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.76 RESTRICT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.77 RETURNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.78 RETURNS NULL ON NULL INPUT . . . . . . . . . . . . . . . . . . . . . . 40
5.79 REVERSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.80 RIGHT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.81 RIGHT JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.82 RIGHT OUTER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.83 SELECT ... FROM ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.84 SELECT DISTINCT ... FROM ... . . . . . . . . . . . . . . . . . . . . . . . . 42
5.85 SELECT INTO ... FROM ... . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.86 SETOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.87 SIMILAR TO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.88 STABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.89 STRICT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.90 SUBSTRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.91 SUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.92 TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.93 TRUNCATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.94 UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.95 UNION ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.96 UPPER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.97 USING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.98 VALUES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.99 VOLATILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.100 WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.101 WHERE ... BETWEEN ... AND ... . . . . . . . . . . . . . . . . . . . . . . 49
5.102 WHERE ... IN ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.103 WHERE ... LIKE ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.104 WHERE ... ILIKE ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
1 About This Document
This document explains the most widely used PostgreSQL data types, symbols and key-
words, and gives examples of their use. The examples can be copied and pasted to the
PostgreSQL app in NCLab, and run. The document is meant as a reference rather than a
learning material. To learn PostgreSQL, take the self-paced courses PostgreSQL Fundamen-
tals and Advanced PostgreSQL in NCLab. More information can be found on the courses’
web page
http://nclab.com/learn-sql/
This document is by no means complete. If you do not find the keyword you were looking
for, visit the official PostgreSQL documentation at
https://www.postgresql.org/docs/current/sql-keywords-appendix.html
1
3.2 VARCHAR(n)
VARCHAR(n) is the variable-length character string. With VARCHAR(n), you can store
up to n characters. PostgreSQL does not pad spaces when the stored string is shorter than
the length of the column.
3.3 TEXT
TEXT is the variable-length character string. Theoretically, text data is a character string
with unlimited length.
3.4 SMALLINT
Small integer (SMALLINT) is 2-byte signed integer that has a range from -32,768 to 32,767.
3.5 INT
Integer (INT) is a 4-byte integer that has a range from -2,147,483,648 to 2,147,483,647.
3.6 SERIAL
Serial is the same as integer except that PostgreSQL will automatically generate and populate
values into the SERIAL column.
3.7 FLOAT(n)
FLOAT(n) is a floating-point number whose precision, at least, n, up to a maximum of 8
bytes.
3.11 DATE
DATE stores the date.
2
3.12 TIME
TIME stores the time of day values.
3.13 TIMESTAMP
TIMESTAMP stores both date and time values.
3.14 TIMESTAMPZ
TIMESTAMPTZ is a timezone-aware timestamp data type. It is the abbreviation for times-
tamp with the time zone.
3.15 INTERVAL
INTERVAL stores periods of time.
Output:
3
4.2 %
The symbol % can be used as a wildcard with the WHERE ... LIKE ... clause. It represents
none, one, or multiple unknown text characters, and it can be placed anywhere in the search
text string. Note that the search text string is enclosed in single quotes (not double quotes).
Example:
Output:
4.3
The symbol (underscore) can be used as a wildcard with the WHERE ... LIKE ... clause.
It represents exactly one unknown text character, and it can be placed anywhere in the
search text string. Note that the search text string is enclosed in single quotes (not double
quotes). Example:
Output:
4
4.4 \
The backslash \ is an escape character which can be used to escape (”take the superpowers
away from”) wildcards and various other special characters. For instance, \% in a search
text string is just the percent symbol and no longer a wildcard. Similarly, \ in a search text
string is just the underscore and no longer a wildcard. Example:
Output:
id | title
-------+--------------------------------------------------------------------------------
5569 | Zeichnet 5 1/2% Dritte Kriegs-Anleihe
6214 | In the Forward Capitalist Countries the Share of Exploiting Classes in the Nati
109440 | 60% Off, New York
183530 | Warhead I (Regan 55%, Brezhnev 45%, Thatcher less than 1%, Mitterrand less than
193145 | My%Desktop
284019 | Jacket Cover for Upton Sinclair's 100%
4.5 ||
The Postgre-specific symbol || can be used to concatenate text strings. It is equivalent to
the function CONCAT. Example:
Output:
result
------
ABC
4.6 ::
The Postgre-specific symbol :: is equivalent to the function CAST. It can be used to convert
values to a different data type, such as a number to a text string, a text string to a number,
decimal number to a binary number, etc. Example:
5
-- Cast the text string '123' to an integer,
-- add 1, and display the result as 'result':
SELECT '123'::INT + 1 AS result;
Output:
result
------
124
4.7 ∼∼
The Postgre-specific operator ∼∼ is equivalent to LIKE (case-sensitive search). Example:
Output:
count
-----
23
Note: Searches using the keyword LIKE are case-sensitive. For case-insensitive search
(which is preferable in most situations) see the clause WHERE ... ILIKE ... (Subsection
5.104).
4.8 ∼∼*
The Postgre-specific operator ∼∼* is the same as ILIKE (case-insensitive LIKE). Example:
Output:
6
count
-----
194
Note: Searches using the keyword ILIKE are case-insensitive. For case-sensitive search see
the clause WHERE ... LIKE ... (Subsection 5.103).
4.9 !∼∼
The Postgre-specific operator !∼∼ is equivalent to NOT LIKE (case-sensitive search). Ex-
ample:
Output:
name
--------------
Albania
Algeria
American Samoa
Andorra
Angola
...
Note: Searches using the keyword LIKE are case-sensitive. For case-insensitive search
(which is preferable in most situations) see the clause WHERE ... ILIKE ... (Subsection
5.104).
4.10 !∼∼*
The Postgre-specific operator !∼∼* is equivalent to NOT ILIKE (case-insensitive NOT
LIKE). Example:
7
Output:
name
--------------
Albania
Algeria
American Samoa
Andorra
Angola
...
Note: Searches using the keyword ILIKE are case-insensitive. For case-sensitive search see
the clause WHERE ... LIKE ... (Subsection 5.103).
5 List of Keywords
5.1 ALTER TABLE
This powerful statement can be used to rename tables, add / rename / drop (= delete)
columns, change data types of columns, etc. To begin with, a table can be renamed by
typing
8
Columns can be renamed by typing
5.2 AND
The keyword AND is used to make sure that two or more conditions are fulfilled at the same
time. It can also be used as part of the WHERE ... BETWEEN ... AND ... clause (see
Subsection 5.101). Example:
name | population
----------------------------+-----------
Falkland Islands | 2000
Faroe Islands | 43000
Fiji Islands | 817000
French Guiana | 181000
French Polynesia | 235000
French Southern territories | 0
5.3 AS
The keyword AS is used for aliasing. For example, it can be used with the SELECT ...
FROM ... statement to rename columns in the result for better clarity. But it can also be
used in joins and other contexts, usually to shorten long names. Example:
SELECT
name AS country_name,
9
continent,
population
FROM world.country
ORDER BY name
LIMIT 5;
Output:
Note that the keyword AS can be left out. The following query is identical to the one above:
SELECT
name country_name,
continent,
population
FROM world.country
ORDER BY name
LIMIT 5;
5.4 ASC
The keyword ASC can be used with the ORDER BY clause to order rows in the result
ascendingly. However, this keyword is not used very often because ORDER BY will order
rows ascendingly by default. Example:
10
ORDER BY name
LIMIT 5;
Output:
5.5 ASCII
The function ASCII returns the ASCII code of any given text character. Example:
-- Display the ASCII code of the letter 'A'. Name the result 'result':
SELECT ASCII('A') AS result;
Output:
result
------
65
5.6 AVG
AVG is one of the five aggregate functions (COUNT, SUM, AVG, MIN, MAX). It applies to
an entire column and calculates the average value. Example:
Output:
avg
---------------------
15871186.956521739130
11
5.7 BETWEEN
See WHERE ... BETWEEN ... AND ... (Subsection 5.101).
5.9 CASCADE
This is an option which can be used while removing a table from the database via DELETE
or TRUNCATE. It tells to automatically drop objects that depend on the table (such as
views).
5.10 CASE
The CASE clause can be used to create conditional expressions (”insert if-then-else logic”)
into SQL queries. The typical CASE clause has the following structure:
CASE
WHEN ... THEN ...
WHEN ... THEN ...
...
ELSE ...
END
Example:
Output:
12
Fuller | Andrew | American
Leverling | Janet | American
Peacock | Margaret | American
Buchanan | Steven | British
...
5.11 CAST
The function CAST can be used to convert values to a different data type, such as a number
to a text string, a text string to a number, decimal number to a binary number, etc. Example:
Output:
result
------
124
5.12 CHR
The function CHR is inverse to the function ASCII. It returns the text character which
corresponds to a given ASCII code. Example:
Output:
result
------
A
5.13 COALESCE
The function COALESCE can be used to replace NULLs with selected values. Often it
is used in combination with NULLIF to replace values in tables without using CASE. For
example, the query
13
SELECT * FROM northwind.orders
ORDER BY
CASE WHEN ship_region IS NOT NULL THEN ship_region
ELSE ship_country
END;
Output:
...
5.14 CONCAT
The function CONCAT can be used to concatenate text strings. It is equivalent to the
symbol ||. Example:
Output:
concat
----------------
Nancy Davolio
Andrew Fuller
Janet Leverling
...
14
5.15 COUNT
COUNT is one of the five aggregate functions (COUNT, SUM, AVG, MIN, MAX). It applies
to an entire column and counts all values in the column. Example:
Output:
count
-----
46
-- Function definition:
CREATE OR REPLACE FUNCTION add(i INT, j INT) RETURNS INT
AS $$ SELECT i + j; $$
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
-- Function call:
SELECT add(2, 3);
15
5.19 CREATE SCHEMA IF NOT EXISTS
The clause IF NOT EXISTS is not part of the SQL standard. It makes sure that the new
schema is created only if a schema with the same name does not exist. Example:
16
5.22 CREATE TABLE ... AS TABLE ...
This statement can be used to create an identical copy of a table:
Another way (which we prefer) is to use the SELECT INTO statement with some additional
WHERE clause which is never satisfied. It can be as simple as WHERE 1 = 0:
Output:
17
color
------
yellow
pink
black
The T-Shirts come in four different sizes S, M, L and XL:
Output:
size
-----
S
M
L
XL
The CROSS JOIN creates a table of all combinations of each row from the first table with
each row from the second table. In math, this is called the Cartesian product:
Output:
color | size
-------+-----
yellow | S
yellow | M
yellow | L
yellow | XL
pink | S
pink | M
pink | L
pink | XL
black | S
black | M
black | L
black | XL
18
5.25 DELETE FROM ... WHERE ...
The statement DELETE FROM ... WHERE ... can be used to delete selected or all rows
from a table. Example:
5.26 DESC
The keyword DESC can be used with the ORDER BY clause to order rows in the result
descendingly. Example:
Output:
19
5.28 DROP SCHEMA IF NOT EXISTS
The statement DROP SCHEMA IF NOT EXISTS makes sure that the schema exists before
its deletion is attempted: For example, to drop a schema named ’ProjectX’ if not exists,
type:
5.31 EXCEPT
The EXCEPT clause is a set theory clause (set difference). Recall that we are talking
about sets of rows (tables). The EXCEPT clause can be used to select rows from one table
which are not present in another table. The following example finds the country codes of all
Arabic-speaking countries which do not lie in the Middle East:
Output:
20
countrycode
-----------
SOM
EGY
MAR
ESH
DZA
...
This is because EXCEPT first reduces the multiplicity of all items to 1 while EXCEPT ALL
does not.
5.33 FILTER
The FILTER clause is designed to filter values in aggregate functions. Compare a query
which uses CASE expressions in the aggregate function SUM to count employees in various
nationality groups:
SELECT
SUM(CASE
WHEN country = 'USA' THEN 1 ELSE 0
END) AS num_american,
SUM(CASE
WHEN country = 'UK' THEN 1 ELSE 0
END) AS num_british,
SUM(CASE
WHEN country NOT IN ('USA', 'UK') THEN 1 ELSE 0
21
END) AS num_other
FROM northwind.employees;
SELECT
COUNT(*) FILTER(WHERE country = 'USA') AS num_american,
COUNT(*) FILTER(WHERE country = 'UK') AS num_british,
COUNT(*) FILTER(WHERE country NOT IN ('USA', 'UK')) AS num_other
FROM northwind.employees;
The FILTER clause is followed by the keyword WHERE and a condition in parentheses.
Notice that the new query uses COUNT(*) instead of SUM - that’s because there is no
longer need for a CASE expression returning 1 or 0.
5.34 FROM
See SELECT ... FROM ... (Subsection 5.83). Also see OVERLAY(... PLACING ... FROM
... FOR ...) in Subsection 5.69.
Output:
22
The NULLs in row 4 correspond to missing data from the left table, the NULLs in row 5
correspond to missing data from the right table.
SELECT
product_id,
SUM(unit_price * quantity * (1 - discount)) AS revenue
FROM northwind.order_details
GROUP BY product_id
ORDER BY product_id;
Output:
product_id | revenue
-----------+--------------
1 | 12788.0999197
2 | 16355.9599054
3 | 3043.99999946
4 | 8567.90003265
5 | 5347.20005666
...
23
SELECT
product_id,
SUM(unit_price * quantity * (1 - discount)) AS revenue
FROM northwind.order_details
GROUP BY product_id
HAVING SUM(unit_price * quantity * (1 - discount)) >= 50000
ORDER BY product_id;
Output:
product_id | revenue
-----------+----------------
29 | 80368.6724385
38 | 141396.735627
59 | 71155.6999094
5.39 IF EXISTS
This is an option which can be used while removing a schema/table from the database via
DELETE or TRUNCATE. It tells the system to first check if the schema/table exists before
attempting to delete it.
5.40 ILIKE
See the clause WHERE ... ILIKE ... (Subsection 5.104).
5.41 IMMUTABLE
This is a flag which is used when defining a new function (other flags include STABLE and
VOLATILE). The flag IMMUTABLE indicates that the function does not look up or modify
the database, and always returns the same result when given the same argument values.
This tells the query optimizer that such a function can be highly optimized, or made more
efficient.
24
SELECT rental_id, make, model FROM car_rentals.rentals
INNER JOIN car_rentals.cars
USING(car_id);
Output:
5.43 INTERSECT
The INTERSECT clause performs set intersection of two sets of rows (obtained, typically,
with two different SELECT statements). For example, let’s find out whether some of the
Northwind company suppliers reside in the same cities as its customers:
SELECT city
FROM northwind.customers
INTERSECT
SELECT city
FROM northwind.suppliers;
Output:
city
------
Paris
London
Berlin
...
25
SELECT contact_title, COUNT(contact_title) FROM (
SELECT contact_title FROM northwind.suppliers
INTERSECT
SELECT contact_title FROM northwind.customers
) AS result
GROUP BY contact_title
ORDER BY contact_title;
Output:
contact_title | count
---------------------+------
Accounting Manager | 1
Marketing Manager | 1
Order Administrator | 1
Owner | 1
Sales Agent | 1
Sales Manager | 1
Sales Representative | 1
Output:
contact_title | count
---------------------+------
Accounting Manager | 2
Marketing Manager | 5
Order Administrator | 2
Owner | 1
Sales Agent | 1
Sales Manager | 4
Sales Representative | 6
26
5.45 IN
For WHERE ... IN ... see Subsection 5.102. For IN in POSITION see Subsection 5.72. For
IN in functions see Subsection 5.16.
5.46 INITCAP
The function INITCAP will reformat a text string so that the first letter of every word is
capitalized (and the rest lowercased). Here is an example:
Output:
initcap
--------------------------
Monday Is My Favorite Day.
5.47 INSERT
See Subsection 5.48 for INSERT INTO ... VALUES(...), Subsection 5.49 for INSERT INTO
... SELECT ... FROM ..., and Subsection 5.50 for INSERT INTO ... TABLE ...
Output:
27
name | phone | email
-------------+----------------+---------------
Debbie Dobbs | (777) 123-4567 | [email protected]
Output:
28
INSERT INTO friends TABLE new_customers;
5.51 JOIN
See INNER JOIN (Subsection 5.42). It is recommended to use INNER JOIN instead of
JOIN for clarity.
5.52 LEFT
The function LEFT(text value, N) returns the first N characters of the text string ’text value’.
As an example, let’s extract the first three characters from the text string ’Sunday’:
Output:
left
----
Sun
Output:
29
5.54 LEFT OUTER JOIN
See LEFT JOIN (Subsection 5.53).
5.55 LENGTH
The PostgreSQL LENGTH function is used to find the length of a string, i.e., the number
of characters in the given string. Example:
SELECT LENGTH('Hello!');
Output:
length
------
6
5.56 LIKE
See the clause WHERE ... LIKE ... (Subsection 5.103).
5.57 LIMIT
The keyword LIMIT can be used with the SELECT ... FROM ... statement to limit the
number of rows in the result. Usually it is meaningful to combine it with the ORDER BY
clause. Example:
Output:
30
Antarctica | Antarctica | 0
British Indian Ocean Territory | Africa | 0
Bouvet Island | Antarctica | 0
Heard Island and McDonald Islands | Antarctica | 0
5.58 LOWER
The function LOWER can be used to lowercase text strings. Example:
Output:
city | population
-------------------+-----------
adamstown | 42
west island | 167
fakaofo | 300
città del vaticano | 455
bantam | 503
By combining the function LOWER with the case-sensitive search clause LIKE, one can
achieve case-insensitive search equivalent to the clause ILIKE.
5.59 MAX
MAX is one of the five aggregate functions (COUNT, SUM, AVG, MIN, MAX). It applies
to an entire column and calculates the maximum value. Example:
Output:
31
max
---------
146934000
5.60 MIN
MIN is one of the five aggregate functions (COUNT, SUM, AVG, MIN, MAX). It applies to
an entire column and calculates the minimum value. Example:
Output:
min
----
1000
5.61 NOT
The keyword NOT is used in conjunction with the WHERE clause, to negate conditions.
For example, the following query finds all countries whose name begins with ’Y’:
Output:
name
----------
Yemen
Yugoslavia
And this query finds all countries whose name does not begin with an ’Y’:
Output:
32
name
--------------------------------------------
Afghanistan
United Arab Republic
Netherlands Antilles
Albania
Algeria
...
5.62 NULL
The keyword NULL in SQL tables represents unknown or missing data.
5.63 NULLIF
The function NULLIF can be used to replace selected values with NULLs without using
CASE. The function NULLIF(val1, val2) (”NULL if val1 = val2”) returns NULL if the
two values are the same, otherwise it returns val1. It is equivalent to the following CASE
expression:
CASE
WHEN val1 = val2 THEN NULL
ELSE val1
END
Example: The table northwind.products contains a column ’discontinued’ with 0s and 1s.
Let’s say that we want to replace all the 0s with NULLs:
SELECT
product_name,
NULLIF(discontinued, 0) AS discontinued
FROM northwind.products;
Output:
product_name | discontinued
---------------------------------+-------------
Chai | 1
Chang | 1
Aniseed Syrup | NULL
...
33
5.64 OFFSET
The keyword OFFSET can be used to skip a given number of rows at the beginning of the
output of a SELECT statement. Note that this only makes sense when the output is ordered
using ORDER BY. Often, OFFSET is used to skip damaged data or data which for some
reason one does not want to show. For example, let’s list all countries sorted according to
their population:
Output:
name | population
---------------------------------------------+-----------
British Indian Ocean Territory | 0
United States Minor Outlying Islands | 0
Bouvet Island | 0
French Southern territories | 0
Heard Island and McDonald Islands | 0
Antarctica | 0
South Georgia and the South Sandwich Islands | 0
Pitcairn | 50
Cocos (Keeling) Islands | 600
...
Here, we might not want to show the first seven to avoid annoying questions why their
population is zero. This can be done using OFFSET as follows:
Output:
name | population
--------------------------------------+-----------
Pitcairn | 50
Cocos (Keeling) Islands | 600
Holy See (Vatican City State) | 1000
Tokelau | 2000
Falkland Islands | 2000
34
Niue | 2000
...
5.65 ON
The keyword ON is part of INNER JOIN (and other joins). Joins are used when the infor-
mation we need is not available in a single table - part of it is in one table, another part in
another table. The keyword ON is used when the two tables have a shared column which
has a different name in each table. Otherwise one uses the keyword USING instead.
The shared column typically is an ID, country code, etc. For example, let’s find the capital
cities of the countries from the table world.country. The catch is, though, that this table does
not contain any names of cities. It only contains IDs in the column ’capital’. This column
matches the column ’id’ in the table world.city which contains the names. Therefore, these
two tables need to be joined ON the two matching columns:
Output:
name | name
--------------------------------------+----------------------------------
Afghanistan | Kabul
United Arab Republic | Cairo
Netherlands Antilles | Willemstad
Albania | Tirana
...
5.66 OR
The keyword OR is used to make sure that at least one of two or more conditions is fulfilled.
Example:
Output:
35
name | population
-----------+-----------
Yemen | 18112000
Yugoslavia | 10640000
Zaire | 2943000
Zambia | 9169000
Zimbabwe | 11669000
5.67 ORDER BY
The ORDER BY clause can be used with the SELECT ... FROM ... statement to order
rows in the result according to one or more columns. Example:
Output:
5.68 OUT
This keyword is used to declare output parameters in SQL functions. The sample function
’inc3’ below accepts one integer and returns three integers $1 + 1, $1 + 2 and $1 + 3:
CREATE OR REPLACE FUNCTION inc3(IN INT, OUT INT, OUT INT, OUT INT)
AS $$ SELECT $1 + 1, $1 + 2, $1 + 3; $$
LANGUAGE SQL
36
IMMUTABLE
RETURNS NULL ON NULL INPUT
5.69 OVERLAY
The function OVERLAY(text value PLACING new FROM pos FOR num) uses the text
string ’new’ to replace ’num’ characters in the text string ’text value’ starting at position
’pos’:
Output:
overlay
---------------
Ben plays music
5.70 pg tables
This is not an SQL keyword, but let’s mention pg tables because of its importance. This
is a view which provides essential information about schemata and tables in a PostgreSQL
database. Example:
Output:
schemaname | tablename
-----------+----------------
world | countrylanguage
world | temp
world | city
world | country
37
5.71 PLACING
See OVERLAY(... PLACING ... FROM ... FOR ...) in Subsection 5.69.
5.72 POSITION
The function POSITION(str IN text value) returns the position of the substring ’str’ in the
text string ’text value’. For example, let’s obtain the position of the substring ’day’ in the
text string ’Monday’:
Output:
position
--------
4
Output:
result
------
False
To get a match, the SQL regular expression would have to be ’%Alva%’. On the other hand,
in POSIX it is OK to only match a substring:
Output:
38
result
------
True
Notice the operator ∼ which means ”matches POSIX regular expression” (case sensitive).
It is different from ∼∼ which is a shortcut for LIKE.
Output:
regexp_replace
----------------------
Sunshine is a nice day
5.75 REPLACE
The case-sensitive function REPLACE(text value, str1, str2) is an advanced version of the
function OVERLAY. It replaces in the text string ’text value’ all occurrences of the substring
’str1’ with the substring ’str2’:
Output:
replace
------------------------
Sunshine is a nice shine
When the substring ’str1’ is not found, the original text string ’text value’ is returned. The
usefulness of this function is still rather limited because it does not accept any wildcards.
5.76 RESTRICT
This is an option which can be used while removing a table from the database via DELETE
or TRUNCATE. It refuses to drop the table if any objects depend on it. This is the default.
39
5.77 RETURNS
The keyword RETURNS is used when defining new SQL functions. It specifies the returned
data type.
5.79 REVERSE
The function REVERSE does what you would expect - it reverses text strings! Let’s reverse
the famous palindrome ”Was it a car or a cat I saw?”:
Output:
reverse
----------------------------
?was I tac a ro rac a ti saW
5.80 RIGHT
The function RIGHT(text value, N) returns the last N characters from the text string
’text value’. As an example, let’s extract the last three characters from the text string
’Sunday’:
Output:
right
-----
day
40
SELECT * FROM car_rentals.customers
RIGHT JOIN car_rentals.rentals
USING(cust_id);
Output:
Output:
Output:
41
make | model
-------+-----------
Toyota | Highlander
Ford | Explorer
Honda | Pilot
Nissan | Altima
Output:
countrycode
-----------
AFG
AFG
AFG
AFG
NLD
...
with
Output:
countrycode
-----------
MDG
PLW
BMU
PSE
COG
...
42
5.85 SELECT INTO ... FROM ...
This statement can be used to create an identical copy of a table:
5.86 SETOF
This keyword is used when defining a new function. It is used to return a column of values,
or a table.
5.87 SIMILAR TO
The clause SIMILAR TO is a powerful alternative to LIKE (case-sensitive search). For
illustration, let’s use LIKE to search for all nationalities in the table moma.nationality which
begin with B, G, P or R:
The same query is much simpler when written using SIMILAR TO and a regular expression:
5.88 STABLE
This is a flag which is used when defining a new function (other flags include IMMUTABLE
and VOLATILE). The flag STABLE means that the function may look up the database but
does not change it. This lets the query optimizer know that two calls to such a function may
give different results: for example, if the database changes in the meantime, if system date
and/or time changes, etc.
43
5.89 STRICT
This keyword is used where definign new functions. It is the same as RETURNS NULL ON
NULL INPUT (Subsection 5.78). It means that the function aborts and just returns NULL
whenever any of the arguments are NULL.
5.90 SUBSTRING
The function SUBSTRING can be used to extract parts of text strings. Its syntax is
where ’text value’ is a text string, ’pos’ the initial position, and ’num’ the number of char-
acters. The function can also be used with a simpler syntax:
Example:
Output:
substring
---------
Edison
5.91 SUM
SUM is one of the five aggregate functions (COUNT, SUM, AVG, MIN, MAX). It applies
to an entire column and adds up all values. Example:
Output:
sum
---------
730074600
44
5.92 TABLE
This keyword is used in many contexts: CREATE TABLE (Subsections 5.20 - 5.23), ALTER
TABLE (Subsection 5.1), DROP TABLE (Subsection 5.29), DROP TABLE IF NOT EXISTS
(Subsection 5.30), INSERT INTO ... TABLE ... (Subsection 5.50) etc.
5.93 TRUNCATE
This is an alternative way of removing all data from a table:
TRUNCATE travels;
5.94 UNION
The UNION operation performs the set union of two sets of rows (two tables). Note that
the result of the SELECT statement is such a set of rows (a table). UNION can be placed
between two SELECT statements which, moreover, can be combined with WHERE clauses
etc. Also note that set union automatically removes duplicates. For example, let’s display
contact name, contact title and company name for all people who are in the table north-
wind.customers or in the table northwind.suppliers:
Output:
45
UNION
SELECT code FROM world.country
WHERE region LIKE '%Africa%';
Output:
countrycode
-----------
COG
IOT
CAF
SOM
ERI
...
Output:
contact_title
---------------------
Purchasing Manager
Order Administrator
Sales Representative
...
5.96 UPPER
The function UPPER can be used to uppercase text strings. Example:
46
-- limit the number of rows to 5:
SELECT UPPER(name) AS city, population FROM world.city
ORDER BY population
LIMIT 5;
Output:
city | population
-------------------+-----------
ADAMSTOWN | 42
WEST ISLAND | 167
FAKAOFO | 300
CITTÀ DEL VATICANO | 455
BANTAM | 503
By combining the function UPPER with the case-sensitive search clause LIKE, one can
achieve case-insensitive search equivalent to the clause ILIKE.
5.97 USING
The keyword USING is part of INNER JOIN (and other joins). Joins are used if the infor-
mation we need is not available in a single table - part of it is in one table, another part
in another table. The keyword USING connects the two tables when they have a shared
column of the same name. Otherwise the keyword ON is used instead. The shared col-
umn typically is an ID, country code, etc. For example, let’s find the make and model of
the car which was involved in each rental transaction. This requires data from two tables:
car rentals.rentals and car rentals.cars which both have a column named ’car id’:
Output:
5.98 VALUES
See INSERT INTO ... VALUES(...) (Subsection 5.48).
47
5.99 VOLATILE
This is a flag which is used when defining a new function (other flags include IMMUTABLE
and STABLE). The flag VOLATILE tells the query optimizer that such a function cannot
be optimized at all.
5.100 WHERE
The WHERE clause can be used to narrow down the results of the SELECT ... FROM ...
statement (this is called ”filtering”). The WHERE clause is extremely versatile. It can be
combined with arithmetic comparison operators =, != (not equal), <, <=, >, >= as well as
with the Boolean operators AND, OR, NOT as well as with the keywords BETWEEN, IN,
LIKE, ILIKE. First we will show examples of its use with the arithmetic and Boolean oper-
ators. Its use with the keywords BETWEEN, IN, LIKE, ILIKE will be discussed separately
in the following Subsections 5.101, 5.102, 5.103 and 5.104.
The sample query below will display all countries from the table world.country with
population of at least 200,000,000:
Output:
name | population
--------------+-----------
Indonesia | 212107000
United States | 278357000
India | 1013662000
China | 1277558000
And this query will display all countries from the table world.country with population of at
least 200,000,000 which do not lie in North America:
Output:
name | population
----------+-----------
Indonesia | 212107000
48
India | 1013662000
China | 1277558000
Output:
Output:
49
5.103 WHERE ... LIKE ...
The WHERE ... LIKE ... clause can be used to narrow down (filter) the results of the
SELECT ... FROM ... statement. First, take a few minutes to understand the wildcards %
(Subsection 4.2) and (Subsection 4.3). Note that the search text string is enclosed in single
quotes (not double quotes). Example:
Output:
50