Why Computed Values Add Flexibility
📖 Scenario: You are managing a small online store database. You want to calculate the total price for each order item by multiplying the quantity by the unit price. Instead of storing the total price in the database, you will compute it on the fly using SQL queries. This approach keeps your data flexible and always accurate if prices or quantities change.
🎯 Goal: Build a simple SQL query that computes the total price for each order item by multiplying quantity and unit_price columns. This shows how computed values add flexibility by calculating results dynamically.
📋 What You'll Learn
Create a table called
order_items with columns item_id (integer), quantity (integer), and unit_price (decimal).Insert three rows with exact values: (1, 2, 10.00), (2, 5, 7.50), (3, 1, 20.00).
Write a SELECT query that returns
item_id, quantity, unit_price, and a computed column total_price which is quantity * unit_price.Use an alias
total_price for the computed column in the SELECT statement.💡 Why This Matters
🌍 Real World
Computing values on the fly in queries helps keep data consistent and reduces storage needs. For example, online stores calculate totals dynamically to reflect current prices and quantities.
💼 Career
Database developers and analysts often write queries with computed columns to generate reports and insights without changing the stored data.
Progress0 / 4 steps