TExcelTAD
Home › PostgreSQL › PostgreSQL Syntax

PostgreSQL Syntax

SQL reads almost like English, but it has a handful of rules that trip up beginners. This lesson covers how statements are built, how PostgreSQL treats upper and lower case, how to write text and comments, and the difference between single and double quotes.

Anatomy of a statement

text
SELECT title, price          -- which columns
FROM books                   -- which table
WHERE price < 10             -- which rows
ORDER BY price;              -- in which order; the semicolon ends the statement

A statement is made of clauses (SELECT …, FROM …, WHERE …) and always ends with a semicolon. Line breaks and extra spaces don't matter, so the same query can be written on one line or spread out for readability:

pgsql · try it yourself
SELECT title, price FROM books WHERE price < 10 ORDER BY price;

select   title,
         price
from     books
where    price < 10
order by price;
      title       | price
------------------+-------
 Moon Socks       |  7.50
 Ten Tiny Dragons |  8.99
(2 rows)

      title       | price
------------------+-------
 Moon Socks       |  7.50
 Ten Tiny Dragons |  8.99
(2 rows)

Keywords and identifiers

Keywords (SELECT, FROM, WHERE) are case-insensitive. A common convention, used in this course, is to write keywords in UPPER CASE and names in lower case.

Identifiers are the names of tables, columns and other objects. PostgreSQL folds unquoted identifiers to lower case: Books, BOOKS and books all mean the same table. If you put a name in double quotes, it keeps its exact spelling and may contain spaces, but then you must always quote it:

pgsql · try it yourself
CREATE TABLE "Shelf Labels" ("Label" text);
INSERT INTO "Shelf Labels" VALUES ('New arrivals');
SELECT "Label" FROM "Shelf Labels";
SELECT Label FROM "Shelf Labels";
CREATE TABLE
INSERT 0 1
    Label
--------------
 New arrivals
(1 row)

ERROR:  column "label" does not exist
LINE 1: SELECT Label FROM "Shelf Labels";
               ^
HINT:  Perhaps you meant to reference the column "Shelf Labels.Label".

The last query fails: unquoted Label became label, and no column has that name. Avoid quoted mixed-case names in your own designs; use snake_case such as shelf_labels and label.

Strings use single quotes

Text values (string literals) go in single quotes. To put a single quote inside a string, write it twice. Alternatively, dollar quoting ($…$) needs no escaping at all, which is handy for long text and function bodies:

pgsql · try it yourself
SELECT 'Hello' AS plain,
       'O''Brien' AS doubled_quote,
       $It's "easy"$ AS dollar_quoted,
       'Lorem' || ' ' || 'ipsum' AS joined;
 plain | doubled_quote | dollar_quoted |   joined
-------+---------------+---------------+-------------
 Hello | O'Brien       | It's "easy"   | Lorem ipsum
(1 row)
You write It means
'text' a string value
"name" an identifier (table or column name) with exact spelling
$text$ or $tag$text$tag$ a string value, no escaping needed
42, 3.14, 1e3 numbers
TRUE, FALSE, NULL boolean values and "unknown"

Comments

pgsql · try it yourself
-- A single-line comment runs to the end of the line.
SELECT 1 + 1 AS two;   /* A block comment
                          can span several lines. */
 two
-----
   2
(1 row)

Type casts

PostgreSQL is strict about types. You convert a value with the standard CAST(value AS type) or with the PostgreSQL shorthand value::type:

pgsql · try it yourself
SELECT CAST('2024-02-29' AS date) AS leap_day,
       '42'::integer + 1 AS number,
       3.7::integer AS rounded,
       10 / 4 AS integer_division,
       10 / 4.0 AS decimal_division;
  leap_day  | number | rounded | integer_division |  decimal_division
------------+--------+---------+------------------+--------------------
 2024-02-29 |     43 |       4 |                2 | 2.5000000000000000
(1 row)

Note: Dividing two integers gives an integer (10 / 4 is 2). Make one side a decimal (4.0 or ::numeric) to keep the fraction.

Test yourself

What does PostgreSQL do with the unquoted identifier CustomerName?

  1. Keeps it exactly
  2. Folds it to customername
  3. Converts it to CUSTOMERNAME
  4. Rejects it

How do you write the text It's here as a standard string literal?

  1. "It's here"
  2. 'It''s here'
  3. 'It\'s here'
  4. It's here

What is the result of SELECT 7 / 2;?

  1. 3.5
  2. 3
  3. 4
  4. An error