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.