Using Variables and SET Statements in SQL
📖 Scenario: You are managing a small store's sales data. You want to calculate the total sales for a specific product category and store this value in a variable for later use.
🎯 Goal: Create a SQL script that declares a variable, sets it to the total sales amount for the 'Electronics' category, and then updates the variable with a discount amount.
📋 What You'll Learn
Declare a variable named
@totalSales of type DECIMAL(10,2).Set
@totalSales to the sum of sales amounts from the Sales table where the category is 'Electronics'.Declare a variable named
@discount of type DECIMAL(10,2) and set it to 100.00.Update
@totalSales by subtracting @discount using a SET statement.💡 Why This Matters
🌍 Real World
Variables in SQL help store temporary values like totals or counters during data processing, making queries more flexible and readable.
💼 Career
Knowing how to use variables and SET statements is essential for writing dynamic SQL scripts, stored procedures, and performing calculations in database jobs.
Progress0 / 4 steps