CodingNic

SQL Basics

Comments, Literals, and Expressions

SQL Basics 10 min read

Comments, Literals, and Expressions

Objectives

By the end of this lesson, you should be able to:

  • Write single-line and multi-line SQL comments
  • Recognize text, numeric, boolean, and NULL literals
  • Write a simple arithmetic expression inside a query

💡 Why this matters: Comments explain your SQL to the next person reading it (often future you), literals are the actual values you write into a query, and expressions let a query calculate something instead of just fetching it as-is.

⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database.

Comments

A comment is text SQL ignores entirely, meant for humans reading the query, not the database. PostgreSQL supports two styles:

sql
-- this is a single-line comment, everything after -- on this line is ignored

SELECT product_name
FROM products /* this is an inline block comment */
WHERE id = 1;

-- comments out everything to the end of that line. /* ... */ comments out everything between the markers, even across multiple lines, useful for temporarily disabling a whole chunk of a query while testing.

Literals

A literal is a fixed value written directly into a query, exactly as-is, rather than read from a table.

sql
SELECT 'hello' AS text_literal, 42 AS number_literal, true AS bool_literal, NULL AS null_literal;
text
 text_literal | number_literal | bool_literal | null_literal
--------------+-----------------+--------------+---------------
 hello        |              42 | t            |
  • Text literals use single quotes: 'hello', 'Notebook'. Double quotes are for identifiers (last lesson), not text, mixing these up is one of the most common early SQL mistakes.
  • Numeric literals are written plainly: 42, 19.99, no quotes.
  • Boolean literals are true and false, unquoted.
  • NULL represents “no value,” it isn’t a literal in quite the same sense as the others (it’s not text, a number, or a boolean), but it can be written directly into a query the same way.

Expressions

An expression is something SQL calculates, rather than a plain literal or column value. Ordinary arithmetic works directly inside a query:

sql
SELECT product_name, price, price * 1.1 AS price_with_tax
FROM products
WHERE product_name = 'Wireless Mouse';
text
  product_name  | price | price_with_tax
-----------------+-------+-----------------
 Wireless Mouse  | 24.99 |         27.489

price * 1.1 isn’t a column that exists in the products table, it’s calculated fresh for every row, using that row’s actual price. +, -, *, / all work as expected between numeric columns and numeric literals, directly inside a SELECT.

Try It

  1. Write a single-line comment above a SELECT statement explaining what it does.
  2. Write a query that selects the literals 'apple', 7, and false as three separate columns, with your own choice of aliases.
  3. Using the products table, write an expression that calculates each product’s price with a 20% discount applied (multiply by 0.8).

Recap

  • -- comments out the rest of a line, /* ... */ comments out everything between the markers.
  • Text literals use single quotes, numeric literals are unquoted, boolean literals are true/false, NULL represents no value.
  • An expression, like price * 1.1, calculates a value fresh for each row, rather than reading it directly from a column.

Next lesson: SELECT itself, put together in full, reading real data back from a real table.