Sub topic 2
Literals and Operators
This chapter assumes you can write basic SELECT ... FROM ... WHERE queries, recognize common column types, and use GROUP BY, COUNT, and SUM.
A SQL expression is a piece of SQL that produces one value. An expression can combine column values, literal values, operators, and functions. You can use expressions in the SELECT list to calculate values for each returned row.
Literals in SQL expressions
A literal is a value written directly in a query rather than read from a table column.
Common literal types include:
| Literal type | Example | Meaning |
|---|---|---|
| Number | `12`, `4.5` | An integer or decimal value |
| Text | `'Annual salary'` | A character string enclosed in single quotes |
| Boolean | `TRUE` | A true or false value, if supported by the database |
| NULL | `NULL` | An unknown or missing value |
Use single quotes for text. A number in quotes is text, not a number:
SELECT salary * 12 AS annual_salary,
'USD' AS currency
FROM employees;The expression salary * 12 uses a column and a numeric literal. The text literal 'USD' produces the same value for every returned row.
A text literal containing a single quote needs special handling. In standard SQL, write two single quotes:
SELECT ' manager''s report' AS label;This returns the text manager's report.
Arithmetic and comparison operators
Arithmetic operators calculate numeric values:
+adds-subtracts*multiplies/divides%returns a remainder in dialects that support it
For example:
SELECT first_name,
salary,
salary * 12 AS annual_salary,
salary / 12 AS monthly_salary
FROM employees;The database evaluates the expression separately for each row. Parentheses make the intended order clear:
SELECT salary + (salary * bonus_rate) AS pay_with_bonus
FROM employees;Without parentheses, multiplication and division normally happen before addition and subtraction. This follows the usual precedence rules. Use parentheses when a calculation could be misunderstood.
Comparison operators produce a condition:
=equal to<>or!=not equal to, depending on the dialect>greater than<less than>=greater than or equal to<=less than or equal to
You can combine conditions with logical operators:
SELECT employee_id,
first_name,
salary
FROM employees
WHERE salary >= 50000
AND department_id = 10;AND requires both conditions to be true. OR requires at least one condition to be true. Use parentheses when mixing them:
SELECT employee_id, salary, department_id
FROM employees
WHERE (department_id = 10 OR department_id = 20)
AND salary >= 50000;Expression evaluation
This diagram shows how a row becomes computed output.
The database resolves column values for the current row, applies operators, and returns the resulting value. A literal can provide a fixed multiplier, label, threshold, or fallback value.
Common errors
Do not use double quotes for ordinary text literals. In many databases, double quotes identify column or table names:
SELECT "salary"
FROM employees;Here, "salary" usually means an identifier. Use 'salary' when you want the literal text.
Also avoid assuming that division always returns a decimal. Some systems perform integer division when both operands are integers. Cast one operand to a decimal type when fractional results matter. The exact cast syntax depends on the SQL dialect.
Expressions can be combined with aliases to make computed results easier to read. For example, salary * 12 can be named annual_salary for the result set.
Exercise
What value does the expression (8 + 4) * 3 produce, and why are the parentheses important?
Show answer
It produces 36. The parentheses make addition happen before multiplication. Without them, multiplication would happen first, producing 20.