Computed Columns and Expression Precedence
Arithmetic, concatenation, and operator precedence inside SELECT lists.
Computed Columns and Expression Precedence is a free SQL Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Computed Columns in SELECT
A computed column is an expression in SELECT that derives a value instead of reading one directly — like price * quantity. Always give it a clean alias.
SELECT product,
price,
quantity,
price * quantity AS line_total
FROM order_items;Arithmetic Operators
SQL has the math operators you'd expect: +, -, *, /, plus a modulo. The result's type follows the operands — and that's where the next gotcha hides.
SELECT 7 + 3 AS sum_val,
7 - 3 AS diff_val,
7 * 3 AS prod_val,
7 % 3 AS remainder;Integer Division Trap
Watch out: dividing two integers does integer division, so 5 / 2 gives 2, not 2.5. Fix it by casting one side to decimal or multiplying by 1.0.
SELECT 5 / 2 AS int_div, -- 2
5 * 1.0 / 2 AS real_div, -- 2.5
CAST(5 AS DECIMAL) / 2 AS cast_div; -- 2.5Operator Precedence
SQL follows normal math precedence: * and / run before + and -. So 2 + 3 * 4 is 14, not 20. Parentheses override everything.
SELECT 2 + 3 * 4 AS no_parens, -- 14
(2 + 3) * 4 AS with_parens; -- 20Precedence in a Real Discount Query
Precedence bugs love business logic. For a discounted price, price - price * rate is correct because * runs first. Always sanity-check the grouping the engine actually uses.
SELECT price,
discount_rate,
price - price * discount_rate / 100 AS net_price
FROM products;String Concatenation
Joining text depends on the database: || in Postgres and standard SQL, + in SQL Server, and CONCAT() in MySQL. CONCAT() is the safe, portable choice.
-- Postgres / standard
SELECT first_name || ' ' || last_name AS full_name
FROM employees;
-- Portable
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;NULL Poisons Arithmetic
Big one: any math with NULL gives NULL. So base_salary + bonus is NULL for anyone missing a bonus. Wrap risky columns in COALESCE to supply a default.
SELECT base_salary,
bonus,
base_salary + bonus AS unsafe_total,
base_salary + COALESCE(bonus,0) AS safe_total
FROM employees;Concatenation and NULL by Dialect
NULL hits text too, and dialects differ: Postgres makes the whole string vanish, while SQL Server's CONCAT treats NULL as empty. Wrap each piece in COALESCE for predictable output.
SELECT COALESCE(first_name,'') || ' ' || COALESCE(last_name,'') AS full_name
FROM employees;Functions Inside Expressions
Computed columns happily wrap functions like ROUND, ABS, or UPPER. A function call is just another expression, so you can nest it right inside your math.
SELECT product,
ROUND(price * quantity * 1.0, 2) AS line_total,
UPPER(product) AS product_caps
FROM order_items;Type Coercion and CAST
When operand types differ, SQL does implicit coercion that can surprise you. Be explicit with CAST(expr AS type) — interviewers read that as careful, defensive code.
SELECT 'Order #' || CAST(order_id AS VARCHAR) AS label
FROM orders;Boolean Expressions as Columns
A comparison like price > 100 is itself an expression. Postgres can select it as true/false, but the portable answer wraps it in a CASE to emit 1/0 or a label.
-- Portable
SELECT product,
CASE WHEN price > 100 THEN 1 ELSE 0 END AS is_pricey
FROM products;Quick Check
Evaluate the expression.
Recap
Recap: alias your computed columns. Cast for decimal division, mind precedence, use CONCAT() for portable text — and remember NULL poisons math, so guard it with COALESCE.
Frequently asked questions
Is the “Computed Columns and Expression Precedence” lesson free?
Yes — the full text of “Computed Columns and Expression Precedence” is free to read here on the web, and the SQL Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Computed Columns and Expression Precedence”?
Arithmetic, concatenation, and operator precedence inside SELECT lists. You practise SQL Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SQL Interview Prep?
No prior experience is required. SQL Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Computed Columns and Expression Precedence” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SQL Interview Prep lesson?
Yes. Every SQL Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Projecting Columns and Aliasing Pitfalls
- Computed Columns and Expression Precedence
- DISTINCT vs GROUP BY for Uniqueness
- CASE Expressions in SELECT