Postgresql - List of Commands: August 20, 2019

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

PostgreSQL - List of Commands

August 20, 20191

Contents
1 About This Document 1

2 The PostgreSQL App in NCLab 1

3 Basic Data Types 1


3.1 CHAR(n) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3.2 VARCHAR(n) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.3 TEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.4 SMALLINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.5 INT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.6 SERIAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.7 FLOAT(n) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.8 REAL or FLOAT8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.9 NUMERIC or NUMERIC(p,s) . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.10 BOOLEAN or BOOL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.11 DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.12 TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.13 TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.14 TIMESTAMPZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.15 INTERVAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

4 Frequently Used Symbols 3


4.1 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.2 % . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.4 \ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.5 || . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.6 :: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1 This document was prepared using the LATEX module in NCLab
4.7 ∼∼ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.8 ∼∼* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.9 !∼∼ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.10 !∼∼* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.11 POSIX Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

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

2 The PostgreSQL App in NCLab


NCLab’s Creative Suite provides a PostgreSQL app where you can run all the codes from
this document - just copy and paste the desired code sample to the code cell, and press the
Play button. The app provides several widely used databases including
• CarRentals,
• Northwind,
• MoMA,
• World.
The app also provides your own schema (whose name matches your username). In this
schema, you are free to create your own tables, modify them, and/or delete them as you
like. In other words, NCLab allows you to create your own database and work with it using
everything you learned in the self-paced courses.

3 Basic Data Types


3.1 CHAR(n)
CHAR(n) is the fixed-length character with space padded. If you insert a string that is
shorter than the length of the column, PostgreSQL pads spaces. If you insert a string that
is longer than the length of the column, PostgreSQL will issue an error.

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.8 REAL or FLOAT8


REAL or FLOAT8 is a 4-byte floating-point number.

3.9 NUMERIC or NUMERIC(p,s)


NUMERIC or NUMERIC(p,s) is a real number with p digits with s number after the decimal
point. The NUMERIC(p,s) is the exact number.

3.10 BOOLEAN or BOOL


Use keywords BOOLEAN or BOOL to declare a column with the Boolean data type. A
Boolean data type can hold one of three possible values: TRUE, FALSE or NULL.

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.

4 Frequently Used Symbols


4.1 *
When used in a calculation, the symbol * (asterisk) means multiplication. When used with
the SELECT ... FROM ... statement, it means ”all columns”. Example:

-- Display all columns from table world.city. Order rows by name,


-- and limit their number to 5:
SELECT * FROM world.city
ORDER BY name
LIMIT 5;

Output:

id | name | countrycode | district | population


-----+------------+-------------+---------------------+-----------
3097 | Aachen | DEU | Nordrhein-Westfalen | 243825
3318 | Aalborg | DNK | Nordjylland | 161161
2760 | Aba | NGA | Imo & Abia | 298900
1404 | Abadan | IRN | Khuzestan | 206073
395 | Abaetetuba | BRA | Pará | 111258

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:

-- Display all columns from table world.city where


-- the name of the city begins with 'Pra':
SELECT * FROM world.city
WHERE name LIKE 'Pra%'
ORDER BY name;

Output:

id | name | countrycode | district | population


-----+--------------+-------------+--------------------+-----------
3339 | Praha | CZE | Hlavnı́ mesto Praha | 1181126
1859 | Praia | CPV | S~
ao Tiago | 94800
335 | Praia Grande | BRA | S~
ao Paulo | 168434
1483 | Prato | ITA | Toscana | 172473

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:

-- Display all columns from table world.city where


-- the name of the city begins with 'Pra', ends
-- with 'a' and is 5 characters long:
SELECT * FROM world.city
WHERE name LIKE 'Pra_a'
ORDER BY name;

Output:

id | name | countrycode | district | population


-----+-------+-------------+--------------------+-----------
3339 | Praha | CZE | Hlavnı́ mesto Praha | 1181126
1859 | Praia | CPV | S~
ao Tiago | 94800

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:

-- Display all rows in table moma.artworks where


-- the title contains the percent symbol:
SELECT * FROM moma.artworks
WHERE title LIKE '%\%%';

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:

SELECT 'A' || 'BC' AS result;

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:

-- In the table moma.artworks count all titles which


-- contain the substring 'lamp' (case-sensitive):
SELECT COUNT(*) FROM moma.artworks
WHERE title ~~ '%lamp%';

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:

-- In the table moma.artworks count all titles which


-- contain the substring 'lamp' (case-insensitive):
SELECT COUNT(*) FROM moma.artworks
WHERE title ~~* '%lamp%';

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:

-- Exclude from the search 'Afghanistan',


-- but 'Ghana' will not be excluded:
SELECT name FROM world.country
WHERE name !~~* '%gha%'
ORDER BY name;

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:

-- Exclude from the search 'Afghanistan', 'Ghana', etc.


SELECT name FROM world.country
WHERE name !~~* '%gha%'
ORDER BY name;

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).

4.11 POSIX Symbols


In POSIX,
• ∼ means match (case-sensitive),
• ∼* means match (case-insensitive),
• !∼ means no match (case-sensitive),
• !∼* means no match (case-insensitive).

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

ALTER TABLE table_name RENAME TO new_table_name;

To rename a table ’songs’ to ’music’, one would type:

ALTER TABLE songs RENAME TO music;

New columns can be added to an existing table by typing

ALTER TABLE table_name ADD COLUMN column_name column_type;

8
Columns can be renamed by typing

ALTER TABLE table_name RENAME COLUMN col_name TO new_col_name;

Columns can be dropped by typing

ALTER TABLE table_name DROP COLUMN column_name;

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:

-- Display all countries whose name begins with 'F'


-- and whose population is less than 1,000,000:
SELECT name, population FROM world.country
WHERE name LIKE 'F%' AND population < 1000000
ORDER BY name;

Output (note that France and Finland are missing):

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:

country_name | continent | population


---------------+-----------+-----------
Afghanistan | Asia | 22720000
Albania | Europe | 3401200
Algeria | Africa | 31471000
American Samoa | Oceania | 68000
Andorra | Europe | 78000

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:

-- Display the name, continent and population of countries. Order rows


-- alphabetically by name, and limit their number to 5:
SELECT
name,
continent,
population
FROM world.country

10
ORDER BY name
LIMIT 5;

Output:

name | continent | population


---------------+-----------+-----------
Afghanistan | Asia | 22720000
Albania | Europe | 3401200
Algeria | Africa | 31471000
American Samoa | Oceania | 68000
Andorra | Europe | 78000

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:

-- Calculate the average population of European countries:


SELECT AVG(population) FROM world.country
WHERE continent = 'Europe';

Output:
avg
---------------------
15871186.956521739130

11
5.7 BETWEEN
See WHERE ... BETWEEN ... AND ... (Subsection 5.101).

5.8 CALLED ON NULL INPUT


This clause is used when definign new functions. It means that the function body will be
called normally even when some of the arguments are NULL. Compare to RETURNS NULL
ON NULL INPUT (Subsection 5.78) which is the same as STRICT (Subsection 5.89).

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:

SELECT last_name, first_name,


CASE
WHEN country = 'USA' THEN 'American'
WHEN country = 'UK' THEN 'British'
ELSE 'Other'
END AS nationality
FROM northwind.employees;

Output:

last_name | first_name | nationality


----------+------------+------------
Davolio | Nancy | American

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:

-- Cast the text string '123' to an integer,


-- add 1, and display the result as 'result':
SELECT CAST('123' AS INT) + 1 AS result;

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:

-- Display the text character which corresponds to the ASCII


-- code 65. Name the result 'result':
SELECT CHR(65) AS result;

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;

can be simplified with COALESCE to just

SELECT * FROM northwind.orders


ORDER BY COALESCE(ship_region, ship_country);

Output:

order_id | customer_id | employee_id | order_date | required_date | shipped_date | ship_


---------+-------------+-------------+------------+---------------+--------------+------
10338 | OLDWO | 4 | 1996-10-25 | 1996-11-22 | 1996-10-29 | 3
11034 | OLDWO | 8 | 1998-04-20 | 1998-06-01 | 1998-04-27 | 1
10594 | OLDWO | 3 | 1997-07-09 | 1997-08-06 | 1997-07-16 | 2

...

5.14 CONCAT
The function CONCAT can be used to concatenate text strings. It is equivalent to the
symbol ||. Example:

SELECT CONCAT(first_name, ' ', last_name)


FROM northwind.employees;

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:

-- Counts all European countries:


SELECT COUNT(name) FROM world.country
WHERE continent = 'Europe';

Output:
count
-----
46

5.16 CREATE FUNCTION


This is a way to create a function. However, CREATE OR REPLACE FUNCTION is
preferred (see Subsection 5.17).

5.17 CREATE OR REPLACE FUNCTION


This is a way to create a new function if it does not exist. As an example, let’s create a new
function ’add’ to add two numbers:

-- 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);

5.18 CREATE SCHEMA


The statement CREATE TABLE is used to create new schemata. Example:

CREATE SCHEMA ProjectX;

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:

CREATE SCHEMA IF NOT EXISTS ProjectX;

5.20 CREATE TABLE


The statement CREATE TABLE is used to create new tables. The keywords CREATE
TABLE are followed by the table name and the list of columns in parentheses. For every
column one needs to specify its name and data type. For example, let’s create a table
friends with three columns named ’name’, ’phone’, ’email’. The column ’phone’ will be
VARCHAR(15) and the other two VARCHAR(30):

CREATE TABLE friends(


name VARCHAR(30),
phone VARCHAR(15),
email VARCHAR(30)
);

5.21 CREATE TABLE ... AS SELECT ... FROM ...


This statement can be used to create a new table which only contains selected columns of
an existing table:

CREATE TABLE friends5 AS


SELECT name, email FROM friends;

The SELECT statement can be enhanced with a WHERE clause:

CREATE TABLE friends6 AS


SELECT * FROM friends
WHERE (name LIKE 'F%') OR (name LIKE 'G%');

16
5.22 CREATE TABLE ... AS TABLE ...
This statement can be used to create an identical copy of a table:

CREATE TABLE friends2 AS TABLE friends;

Another way of doing the same is:

SELECT * INTO friends2 FROM friends;

5.23 CREATE TABLE ... AS TABLE ... WITH NO DATA


Sometimes one needs to create a new empty table with the same structure as an existing
table. For this, one can use the CREATE TABLE statement with an additional line WITH
NO DATA:

CREATE TABLE friends3 AS TABLE friends


WITH NO DATA;

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:

SELECT * INTO friends3 FROM friends


WHERE 1 = 0;

5.24 CROSS JOIN


CROSS JOIN is the Cartesian product of the rows of two tables (understand: “combination
of each row from the first table with each row from the other table”). Imagine that a company
is selling T-Shirts of three different colors: yellow, pink and black:

CREATE TABLE colors(color TEXT);


INSERT INTO colors VALUES('yellow'), ('pink'), ('black');
SELECT * FROM colors;

Output:

17
color
------
yellow
pink
black
The T-Shirts come in four different sizes S, M, L and XL:

CREATE TABLE sizes(size TEXT);


INSERT INTO sizes VALUES('S'), ('M'), ('L'), ('XL');
SELECT * FROM sizes;

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:

SELECT * FROM colors


CROSS JOIN sizes;

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:

DELETE FROM travels4


WHERE city = 'Linz' OR city = 'Graz';

5.26 DESC
The keyword DESC can be used with the ORDER BY clause to order rows in the result
descendingly. Example:

-- Display the name, continent and population of countries. Order rows


-- by population descendingly, and limit their number to 5:
SELECT
name,
continent,
population
FROM world.country
ORDER BY population DESC
LIMIT 5;

Output:

name | continent | population


--------------+---------------+-----------
China | Asia | 1277558000
India | Asia | 1013662000
United States | North America | 278357000
Indonesia | Asia | 212107000
Brazil | South America | 170115000

5.27 DROP SCHEMA


The keyword DROP SCHEMA can be used to drop schemata. For example, to drop a
schema named ’ProjectX’, type:

DROP SCHEMA ProjectX;

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:

DROP SCHEMA IF NOT EXISTS ProjectX;

5.29 DROP TABLE


The keyword DROP TABLE can be used to drop tables. For example, to drop a table named
’SampleT’, type:

DROP TABLE SampleT;

5.30 DROP TABLE IF NOT EXISTS


The statement DROP TABLE IF NOT EXISTS makes sure that the table exists before its
deletion is attempted: For example, to drop a table named ’SampleT’ if not exists, type:

DROP TABLE IF NOT EXISTS SampleT;

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:

SELECT countrycode FROM world.countrylanguage


WHERE language = 'Arabic' AND isofficial = True
EXCEPT
SELECT code FROM world.country
WHERE region = 'Middle East';

Output:

20
countrycode
-----------
SOM
EGY
MAR
ESH
DZA
...

5.32 EXCEPT ALL


The EXCEPT ALL clause is similar to EXCEPT but it takes into account the multiplicity
of items in both bags. Recall that a ”bag” is set with possibly duplicate items (a set does
not allow duplicity). SQL tables are bags. When there are no repeated items, EXCEPT
ALL is the same as EXCEPT. For illustration of the concept:
• {1, 2, 3} EXCEPT {2, 3, 4} is {1},
• {1, 2, 3} EXCEPT ALL {2, 3, 4} is {1}.
However, the two clauses will perform differently with repeated items. Compare:
• {1, 1, 2, 2, 2, 3} EXCEPT {2, 2, 3, 4} is {1},
• {1, 1, 2, 2, 2, 3} EXCEPT ALL {2, 2, 3, 4} is {1, 1, 2}.

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;

with a query which employs FILTER to do the same:

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.

5.35 FULL JOIN


The FULL (OUTER) JOIN or just FULL JOIN is analogous to the LEFT JOIN and RIGHT
JOIN. It keeps all rows from the left table, as well as all rows from the right table, and
inserts NULLs for missing data from the other table. For illustration, let’s full-join the
tables Customers and Rentals:

SELECT * FROM car_rentals.customers


FULL JOIN car_rentals.rentals
USING(cust_id);

Output:

cust_id | name | phone | email | rental_id | car_id | date_out


--------+--------------+----------------+----------------+-----------+--------+---------
384 | Ben Bowman | (475) 927-8732 | bb@this.com | 1198 | 97 | 2018-05-
133 | Walter White | (123) 456-6789 | ww@example.com | 2847 | 152 | 2018-05-
8 | Carol Clark | (902) 111-3859 | cc@that.com | 2385 | 59 | 2018-05-
405 | NULL | NULL | NULL | 3181 | 291 | 2018-06-
73 | Aaron Atkins | (555) 666-7777 | aa@other.com | NULL | NULL | NULL

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.

5.36 FULL OUTER JOIN


See FULL JOIN (Subsection 5.35).

5.37 GROUP ... BY ...


The GROUP ... BY ... clause is used in conjunction with aggregate functions (COUNT,
SUM, AVG, MIN, MAX), to group results. For example, the following query calculates the
cumulative revenue for each product. It displays two columns ’product id’ and ’revenue’. At
the end, it orders the results by ’product id’:

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
...

5.38 GROUP ... BY ... HAVING ...


The keyword HAVING is used to filter the results of the GROUP BY clause. Note that one
cannot use the keyword WHERE because:
• The WHERE clause applies to every row in the table.
• Aggregate functions such as COUNT, MIN, MAX, SUM, AVG are not allowed in the
WHERE clause.
For example, let’s display only products with revenue of $50,000 or more.

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.

5.42 INNER JOIN


INNER JOIN (and other joins) are used if the information 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 if they have a shared column of the same name. Otherwise the keyword
ON is used instead. The shared column 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’:

24
SELECT rental_id, make, model FROM car_rentals.rentals
INNER JOIN car_rentals.cars
USING(car_id);

Output:

rental_id | make | model


----------+--------+-----------
1198 | Ford | Explorer
2847 | Honda | Pilot
2385 | Toyota | Highlander

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
...

5.44 INTERSECT ALL


The INTERSECT ALL clause is a rarely used functionality and although it is part of the
official SQL standard, it is not implemented in all SQL flavors (for example MS SQL Server
does not support it). It is similar to INTERSECT, but it takes into account repetitions. Let’s
look at the tables ’northwind.suppliers’ and ’northwind.customers’. Both of them contain a
column named ’contact title’. First let’s display the INTERSECT of these values:

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

Then let’s change the INTERSECT clause to INTERSECT ALL:

SELECT contact_title, COUNT(contact_title) FROM (


SELECT contact_title FROM northwind.suppliers
INTERSECT ALL
SELECT contact_title FROM northwind.customers
) AS result
GROUP BY contact_title
ORDER BY contact_title;

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:

SELECT INITCAP('MONDAY IS MY FAVORITE DAY.');

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 ...

5.48 INSERT INTO ... VALUES(...)


A complete row can be inserted into a table ”manually” using the statement INSERT INTO
... VALUES(...) where the first ellipsis is replaced with the table name, and the second one
with a comma-separated list of column values. Let’s say that a table ’friends’ already was
created using the CREATE TABLE statement (Subsection 5.20). Then a fictitious friend
Debbie Dobbs can be inserted there by typing

DROP TABLE IF EXISTS friends;


CREATE TABLE friends(
name VARCHAR(30),
phone VARCHAR(15),
email VARCHAR(30)
);
INSERT INTO friends
VALUES('Debbie Dobbs', '(777) 123-4567', 'ddobbs@abc.com');
SELECT * FROM friends;

Output:

27
name | phone | email
-------------+----------------+---------------
Debbie Dobbs | (777) 123-4567 | ddobbs@abc.com

It is also possible to insert two or more rows at the same time:

INSERT INTO friends


VALUES
('Gina Gibbs', '(123) 123-1234', 'ggibbs@abc.com'),
('Howard Hughes', '(321) 432-5432', 'hhughes@abc.com');

5.49 INSERT INTO ... SELECT ... FROM ...


This statement is similar to INSERT INTO ... VALUES(...) (Subsection 5.48) but instead
of typing the values manually, they are taken from another table. Example:

DROP TABLE IF EXISTS friends;


CREATE TABLE friends(
name VARCHAR(30),
phone VARCHAR(15),
email VARCHAR(30)
);
INSERT INTO friends
SELECT name, phone, email FROM car_rentals.customers;
SELECT * FROM friends;

Output:

name | phone | email


-------------+----------------+---------------
Walter White | (123) 456-6789 | ww@example.com
Ben Bowman | (475) 927-8732 | bb@this.com
Carol Clark | (902) 111-3859 | cc@that.com
Aaron Atkins | (555) 666-7777 | aa@other.com

5.50 INSERT INTO ... TABLE ...


This statement is similar to INSERT INTO ... VALUES(...) (Subsection 5.48) and INSERT
INTO ... SELECT ... (Subsection 5.49) but it inserts an entire table. Example:

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’:

SELECT LEFT('Sunday', 3);

Output:
left
----
Sun

5.53 LEFT JOIN


Recall that INNER JOIN (Subsection 5.42) is used to connect two tables. The connection
is made on matching rows only. All non-matching rows are omitted. This is where LEFT
JOIN is different. It joins two tables (say A and B), and includes all rows from the (left)
table A - even if they do not have a matching row in the right table. Example:

SELECT * FROM car_rentals.customers


LEFT JOIN car_rentals.rentals
USING(cust_id);

Output:

cust_id | name | phone | email | rental_id | car_id | date_out


--------+--------------+----------------+----------------+-----------+--------+---------
384 | Ben Bowman | (475) 927-8732 | bb@this.com | 1198 | 97 | 2018-05-
133 | Walter White | (123) 456-6789 | ww@example.com | 2847 | 152 | 2018-05-
8 | Carol Clark | (902) 111-3859 | cc@that.com | 2385 | 59 | 2018-05-
73 | Aaron Atkins | (555) 666-7777 | aa@other.com | NULL | NULL | NULL
If we performed an INNER JOIN, Aaron Atkins would not be in the resulting joined table.

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:

-- Display the name, continent and population of countries. Order rows


-- by population (ascendingly), and limit their number to 5:
SELECT
name,
continent,
population
FROM world.country
ORDER BY population
LIMIT 5;

Output:

name | continent | population


---------------------------------------------+------------+-----------
South Georgia and the South Sandwich Islands | Antarctica | 0

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:

-- Display name and population of cities from table world.city.


-- Display names in lower case, order them by population, and
-- limit the number of rows to 5:
SELECT LOWER(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 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:

-- Find out the maximum of the populations of European countries:


SELECT MAX(population) FROM world.country
WHERE continent = 'Europe';

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:

-- Find out the minimum of the populations of European countries:


SELECT MIN(population) FROM world.country
WHERE continent = 'Europe';

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’:

SELECT name FROM world.country


WHERE name LIKE 'Y%';

Output:
name
----------
Yemen
Yugoslavia

And this query finds all countries whose name does not begin with an ’Y’:

SELECT name FROM world.country


WHERE name NOT LIKE '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:

SELECT name, population FROM world.country


ORDER BY 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:

SELECT name, population FROM world.country


ORDER BY population
OFFSET 7;

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:

SELECT country.name, city.name


FROM world.country
INNER JOIN world.city
ON city.id = country.capital;

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:

-- Display all countries whose name begins with 'Y' or 'Z':


SELECT name, population FROM world.country
WHERE name LIKE 'Y%' OR name LIKE 'Z%'
ORDER BY name;

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:

-- Display the name, continent and population of countries. Order rows


-- alphabetically by name, and limit their number to 5:
SELECT
name,
continent,
population
FROM world.country
ORDER BY name
LIMIT 5;

Output:

name | continent | population


---------------+-----------+-----------
Afghanistan | Asia | 22720000
Albania | Europe | 3401200
Algeria | Africa | 31471000
American Samoa | Oceania | 68000
Andorra | Europe | 78000

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

Recall that $1 means the first input parameter.

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’:

SELECT OVERLAY('Ben listens to music' PLACING 'plays' FROM 5 FOR 10);

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:

-- Display all tables in the schema 'world':


SELECT
schemaname,
tablename
FROM pg_tables
WHERE schemaname = 'world';

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’:

SELECT POSITION('day' IN 'Monday');

Output:
position
--------
4

5.73 POSIX Regular Expressions


POSIX means ”Portable Operating System Interface”. This is a set of international stan-
dards designed by the Institute of Electrical and Electronics Engineers (IEEE) for maintain-
ing compatibility between operating systems. POSIX regular expressions apply across all
operating systems including Linux, MAC and Windows, and being able to use them is an
extremely valuable skill. The big difference between POSIX and SQL regular expressions is
that in POSIX, the regular expression does not have to match the whole text string - it can
match a substring. Here is a sample SQL regular expression:

SELECT 'Thomas Alva Edison' SIMILAR TO 'Alva' AS result;

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:

SELECT 'Thomas Alva Edison' ~ 'Alva' AS result;

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.

5.74 REGEXP REPLACE


SQL has the function REGEXP REPLACE(text value, regexp, newstr, [, flags]) to use
POSIX regular expressions in search-and-replace operations. Here, ’text value’ is the origi-
nal text string, ’regexp’ a POSIX regular expression identifying the substring to be replaced,
’newstr’ the replacement text string, and ’flags’ some optional flags. Example:

SELECT REGEXP_REPLACE('Sunday is a nice day', 'day', 'shine');

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’:

SELECT REPLACE('Sunday is a nice day', 'day', 'shine');

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.78 RETURNS NULL ON NULL INPUT


This keyword is used where defining new SQL functions. It is the same as STRICT (Sub-
section 5.89). It means that the function aborts and just returns NULL whenever any of the
arguments are NULL. Compare to CALLED ON NULL INPUT (Subsection 5.8).

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?”:

SELECT REVERSE('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’:

SELECT RIGHT('Sunday', 3);

Output:
right
-----
day

5.81 RIGHT JOIN


The RIGHT (OUTER) JOIN or just RIGHT JOIN is the counterpart of the LEFT JOIN.
It keeps all rows from the right table and inserts NULLs for missing data from the left one.
For illustration, let’s right-join the tables Customers and Rentals:

40
SELECT * FROM car_rentals.customers
RIGHT JOIN car_rentals.rentals
USING(cust_id);

Output:

cust_id | name | phone | email | rental_id | car_id | date_out


--------+--------------+----------------+----------------+-----------+--------+---------
384 | Ben Bowman | (475) 927-8732 | bb@this.com | 1198 | 97 | 2018-05-
133 | Walter White | (123) 456-6789 | ww@example.com | 2847 | 152 | 2018-05-
8 | Carol Clark | (902) 111-3859 | cc@that.com | 2385 | 59 | 2018-05-
405 | NULL | NULL | NULL | 3181 | 291 | 2018-06-
The NULLs correspond to missing data from the left table. An INNER JOIN would not
include this line.

5.82 RIGHT OUTER JOIN


See RIGHT JOIN (Subsection 5.81).

5.83 SELECT ... FROM ...


This is the basic SQL statement which selects some (or all) columns from a table. All
columns are selected using the asterisk symbol *:

SELECT * FROM car_rentals.cars;

Output:

car_id | make | model | color | year | mileage


-------+--------+------------+-------+------+--------
59 | Toyota | Highlander | Blue | 2012 | 93758
97 | Ford | Explorer | Red | 2018 | 8495
152 | Honda | Pilot | Grey | 2016 | 46483
187 | Nissan | Altima | Black | 2017 | 25412
When we do not want all columns, we can just list the columns of interest:

SELECT make, model FROM car_rentals.cars;

Output:

41
make | model
-------+-----------
Toyota | Highlander
Ford | Explorer
Honda | Pilot
Nissan | Altima

5.84 SELECT DISTINCT ... FROM ...


This statement is similar to SELECT ... FROM ... (Subsection 5.83) except that it removes
duplicates. Compare:

SELECT countrycode FROM world.city;

Output:

countrycode
-----------
AFG
AFG
AFG
AFG
NLD
...
with

SELECT DISTINCT countrycode FROM world.city;

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:

SELECT * INTO friends2 FROM friends;

Another way of doing the same is:

CREATE TABLE friends2 AS TABLE friends;

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:

SELECT nationality FROM moma.nationality


WHERE (nationality LIKE 'B%') OR (nationality LIKE 'G%')
OR (nationality LIKE 'P%') OR (nationality LIKE 'R%');

The same query is much simpler when written using SIMILAR TO and a regular expression:

SELECT nationality FROM moma.nationality


WHERE nationality SIMILAR TO '(B|G|P|R)%';

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

SUBSTRING(text_value FROM pos FOR num)

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:

SUBSTRING(text_value, pos, num)

Example:

SELECT SUBSTRING('Thomas Edison had 1,093 patents.', 8, 6);

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:

-- Adds the populations of all European countries:


SELECT SUM(population) FROM world.country
WHERE continent = 'Europe';

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:

SELECT contact_name, contact_title, company_name


FROM northwind.customers
UNION
SELECT contact_name, contact_title, company_name
FROM northwind.suppliers;

Output:

contact_name | contact_title | company_name


-------------------+------------------------------+-------------------------------------
Christina Berglund | Order Administrator | Berglunds snabbköp
Martin Bein | International Marketing Mgr. | Plutzer Lebensmittelgroßmärkte AG
Philip Cramer | Sales Associate | Königlich Essen
...
As another example, the following query displays the country codes of all cities whose pop-
ulation is over 5,000,000 along with the country codes of all African countries:

SELECT countrycode FROM world.city


WHERE population >= 5000000

45
UNION
SELECT code FROM world.country
WHERE region LIKE '%Africa%';

Output:
countrycode
-----------
COG
IOT
CAF
SOM
ERI
...

5.95 UNION ALL


The clause UNION ALL is similar to UNION, but it keeps repeated items. The result of
UNION ALL is a bag, not a set (because sets do not allow duplicate items). As an example,
let’s display all values of contact title from the table northwind.suppliers along with all values
of contact title from the table northwind.customers. In both cases include repetitions:

SELECT contact_title FROM northwind.suppliers


UNION ALL
SELECT contact_title FROM northwind.customers;

Output:
contact_title
---------------------
Purchasing Manager
Order Administrator
Sales Representative
...

5.96 UPPER
The function UPPER can be used to uppercase text strings. Example:

-- Display name and population of cities from table world.city.


-- Display names in upper case, order them by population, and

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’:

SELECT rental_id, make, model FROM car_rentals.rentals


INNER JOIN car_rentals.cars
USING(car_id);

Output:

rental_id | make | model


----------+--------+-----------
1198 | Ford | Explorer
2847 | Honda | Pilot
2385 | Toyota | Highlander

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:

SELECT name, population FROM world.country


WHERE population >= 200000000
ORDER BY population;

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:

SELECT name, population FROM world.country


WHERE population >= 200000000 AND continent != 'North America'
ORDER BY population;

Output:

name | population
----------+-----------
Indonesia | 212107000

48
India | 1013662000
China | 1277558000

5.101 WHERE ... BETWEEN ... AND ...


The BETWEEN clause can be used to replace two conditions defining a range. For example,
WHERE population BETWEEN 100 and 200 is the same as WHERE population >= 100
AND population <= 200. This also works for text strings, time strings, etc. Example:

SELECT name, population, countrycode FROM world.city


WHERE population BETWEEN 1000000 AND 2000000;

Output:

name | population | countrycode


------------------------+------------+------------
Kabul | 1780000 | AFG
La Matanza | 1266461 | ARG
Córdoba | 1157507 | ARG
Yerevan | 1248700 | ARM
Brisbane | 1291117 | AUS
...

5.102 WHERE ... IN ...


This clause can be used to specify a set of values for the filter. The set can be numerical,
text strings, time strings, etc. Example:

SELECT name, population, countrycode FROM world.city


WHERE name IN ('Barcelona', 'Kabul', 'Manaus');

Output:

name | population | countrycode


----------+------------+------------
Kabul | 1780000 | AFG
Manaus | 1255049 | BRA
Barcelona | 1503451 | ESP
Barcelona | 322267 | VEN

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:

-- Display all columns from table world.city where


-- the name of the city begins with 'Pra':
SELECT * FROM world.city
WHERE name LIKE 'Pra%'
ORDER BY name;

Output:

id | name | countrycode | district | population


-----+--------------+-------------+--------------------+-----------
3339 | Praha | CZE | Hlavnı́ mesto Praha | 1181126
1859 | Praia | CPV | S~
ao Tiago | 94800
335 | Praia Grande | BRA | S~
ao Paulo | 168434
1483 | Prato | ITA | Toscana | 172473
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).

5.104 WHERE ... ILIKE ...


The WHERE ... ILIKE ... clause is similar to WHERE ... LIKE ..., only case-insensitive.
So, the query

SELECT name FROM world.country


WHERE name ILIKE '%gha%';

will include both ’Afghanistan’ and ’Ghana’. As opposed to

SELECT name FROM world.country


WHERE name LIKE '%gha%';

which would include ’Afghanistan’ but not ’Ghana’.

50

You might also like