0
0
SQLquery~10 mins

Variables and SET statements in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variables and SET statements
Declare Variable
Assign Value with SET
Use Variable in Query or Logic
Optionally Reassign with SET
End or Use Variable Result
This flow shows how you declare a variable, assign it a value using SET, use it in queries or logic, and optionally change its value again.
Execution Sample
SQL
DECLARE @count INT;
SET @count = 5;
SET @count = @count + 3;
SELECT @count AS Result;
This code declares a variable, sets it to 5, adds 3, then selects the final value.
Execution Table
StepActionVariableValueOutput/Result
1Declare variable @count@countNULLNo output
2SET @count = 5@count5No output
3SET @count = @count + 3@count8No output
4SELECT @count AS Result@count8Result = 8
5End of script--Execution stops
💡 Script ends after SELECT statement returns the variable value.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
@countNULL588
Key Moments - 3 Insights
Why is the variable @count NULL right after declaration?
When declared, variables have no value until SET assigns one, as shown in step 1 and 2 of the execution_table.
What happens if you try to use @count before SET assigns a value?
It will be NULL or cause an error depending on context, because variables start without a value until SET assigns one (see step 1).
Can you change the value of a variable after it is set?
Yes, you can reassign it with SET as shown in step 3 where @count changes from 5 to 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of @count after step 2?
ANULL
B8
C5
D0
💡 Hint
Check the 'Value' column in row for step 2 in execution_table.
At which step does @count get its first assigned value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the first SET statement assigning a value in execution_table.
If you remove step 3, what would be the output at step 4?
A5
BNULL
C8
DError
💡 Hint
Without step 3, @count remains as assigned in step 2; check variable_tracker for values.
Concept Snapshot
DECLARE variable_name datatype;  -- creates a variable
SET variable_name = value;        -- assigns or changes value
Variables start NULL until SET assigns a value
Use variables in queries or logic by referencing their name
You can reassign variables multiple times with SET
Full Transcript
This lesson shows how to use variables in SQL. First, you declare a variable with DECLARE, which creates it but does not give it a value yet. Then you assign a value using SET. You can change the value later by using SET again. Finally, you can use the variable in queries or logic. The example code declares @count, sets it to 5, adds 3 to make 8, and then selects the result. The execution table shows each step and how the variable changes. Beginners often wonder why the variable is NULL at first or if they can change it later. The answer is yes, you must assign it before use, and you can reassign it anytime. The quiz questions help check understanding of when and how the variable changes.