MODULE 2 OF 8: FILTERING AND TRANSFORMING • LESSON 1 OF 8~16 min read

Expressions, Calculated Columns and DISTINCT

Move beyond selecting stored columns: calculate values, label them clearly, and control duplicate rows in the result. This lesson uses a small order-item example to show what expressions and DISTINCT actually do.

•

Move beyond selecting stored columns: calculate values, label them clearly, and control duplicate rows in the result. This lesson uses a small order-item example to show what expressions and DISTINCT actually do.

Key Takeaways at a Glance:

  • •Arithmetic expressions (+, -, *, /) compute dynamic scalar values for each row without modifying underlying disk data.
  • •Expressions in SELECT should be given meaningful aliases using 'AS alias_name' for clean output headers.
  • •DISTINCT applies to the entire projected row tuple, not just the first column following the DISTINCT keyword.
  • •DISTINCT forces a sorting or hashing step in the execution plan, which incurs performance overhead on large tables.

In Simple Words (The Mental Model)

In SQL, you can use columns as variables in math formulas (like quantity * unit_price). The database calculates the answer for every single row on the fly as it reads it from disk. If you add DISTINCT, the database acts like a bouncer at a club, discarding any row whose combination of values has already entered.

Real-World Analogy

Think of an invoice calculator. The warehouse records how many boxes you bought (quantity) and the box price. The cashier doesn't need a separate column pre-saved on paper for the total—the cash register calculates 'quantity * unit_price' right at checkout.

Foundational Principles

Core Concept Breakdown

Essential database invariants, relational theory, and execution behavior.

1A SELECT list can contain expressions

A SELECT list may contain stored columns, literals, arithmetic expressions, function calls and aliases. For an order_items table with quantity and unit_price, quantity * unit_price calculates a line total for each row. This value exists in the query result; it does not update the stored table unless you run a separate data-changing statement.

Free Preview Limit Reached (2/2 Lessons Used)

Sign In to Unlock Full Lesson Content

Visitors are limited to 2 free preview lessons. Create a free StudyVex account to unlock all 38 comprehensive theory chapters, runnable query walkthroughs, visual input & result tables, and interactive placement quizzes.