Sub topic 4
Literals, Arithmetic and String Concatenation
This chapter assumes you understand tables, rows, columns, basic numbers, strings, and dates. You also need access to an Oracle Database session through SQL*Plus, SQL Developer, or SQL Worksheet.
A literal is a value written directly in a SQL statement. Numeric literals do not need quotation marks. Character literals use single quotation marks. Oracle also supports date literals in the form DATE 'YYYY-MM-DD'.
SELECT 42 AS number_value,
3.14 AS decimal_value,
'Oracle SQL' AS text_value,
DATE '2026-11-28' AS date_value
FROM dual;The FROM dual clause supplies the one row needed for this query. The result contains one row with four calculated values. The aliases after AS provide readable column headings.
Arithmetic expressions
An expression combines values and operators to produce a result. Oracle supports the usual arithmetic operators:
+adds values.-subtracts values.*multiplies values./divides values.
Arithmetic follows operator precedence. Multiplication and division are evaluated before addition and subtraction. Use parentheses when you want the order to be obvious or different.
SELECT 10 + 5 * 2 AS standard_precedence,
(10 + 5) * 2 AS parenthesized_result,
100 / 4 AS division_result,
17 - 3 AS subtraction_result
FROM dual;standard_precedence is 20, because multiplication happens first. parenthesized_result is 30, because the parentheses force addition to happen first.
Expressions can use columns as well as literals. For example, this query calculates an annual salary from a monthly salary:
SELECT employee_id,
salary,
salary * 12 AS annual_salary
FROM employees;The expression does not change the stored salary value. It calculates a value for the query result only.
Combining text with `||`
The || operator concatenates, or joins, character values. It does not add numbers.
SELECT 'Hello, ' || 'Oracle' || '!' AS message,
'Total: ' || (2 + 3) AS formatted_total
FROM dual;The arithmetic expression is evaluated before its result is concatenated with the text. Oracle converts the numeric result to character text for the concatenation.
Column values can be joined in the same way:
SELECT first_name || ' ' || last_name AS full_name,
'Employee #' || employee_id AS employee_label
FROM employees;The literal ' ' inserts a space between the names. Without it, first_name || last_name would produce text such as StevenKing.
To include a single quotation mark inside a character literal, write two consecutive single quotation marks:
SELECT 'It''s an Oracle query' AS quoted_text
FROM dual;The two quotation marks represent one quotation mark in the result.
Expressions and NULL
A NULL value means that a value is missing, unknown, or not applicable. Arithmetic involving NULL produces NULL, and concatenating a nullable column can produce unexpected output. Use NVL when you need a replacement value.
SELECT first_name,
salary + NVL(commission_pct, 0) AS salary_plus_commission_rate
FROM employees;Here, a missing commission_pct is treated as zero for the calculation. This expression adds a commission rate to a salary, so it is only an example of expression syntax. In a real payroll calculation, you would use the correct business formula and units.
Visualizing expression evaluation
The parentheses in a larger expression make its intended evaluation order easier to read, even when Oracle's normal precedence would produce the same result.
Exercise
Write a query against DUAL that returns the text Order total: 42, where 42 is calculated as 40 + 2.
Show answer
SELECT 'Order total: ' || (40 + 2) AS order_total FROM dual;