A variable in SQL is a named storage that holds a value temporarily during the execution of a batch or procedure.
Click to reveal answer
beginner
How do you declare a variable in SQL?
You declare a variable using the DECLARE statement, for example: DECLARE @myVar INT;
Click to reveal answer
beginner
What does the SET statement do in SQL?
The SET statement assigns a value to a variable, for example: SET @myVar = 10;
Click to reveal answer
intermediate
Can you assign a value to a variable during declaration in SQL?
No, in standard SQL you declare a variable first and then assign a value using SET. Some SQL dialects allow assignment during declaration, but it's not standard.
Click to reveal answer
intermediate
What is the difference between SET and SELECT for assigning variables?
SET assigns one variable at a time and is standard. SELECT can assign multiple variables at once but may behave differently if no rows are returned.
Click to reveal answer
Which SQL statement is used to declare a variable?
ADECLARE
BSET
CSELECT
DCREATE
✗ Incorrect
DECLARE is used to create a variable in SQL.
How do you assign the value 5 to a variable named @count?
ASET @count = 5;
BDECLARE @count = 5;
CSELECT @count = 5;
DCREATE @count = 5;
✗ Incorrect
SET assigns a value to an already declared variable.
Which statement is true about SET and SELECT for variable assignment?
ASET can assign multiple variables at once.
BBoth SET and SELECT cannot assign variables.
CDECLARE assigns values to variables.
DSELECT can assign multiple variables at once.
✗ Incorrect
SELECT can assign multiple variables in one statement.
What happens if you use SET to assign a variable but the value is missing?
AThe variable is set to NULL.
BThe variable keeps its previous value.
CAn error occurs.
DThe variable is deleted.
✗ Incorrect
SET requires a value; missing value causes an error.
Which of these is a valid variable name in SQL?
AtotalAmount
B@totalAmount
C#totalAmount
D$totalAmount
✗ Incorrect
Variables in SQL typically start with @.
Explain how to declare and assign a value to a variable in SQL using SET statements.
Think about the two-step process: first create the variable, then give it a value.
You got /4 concepts.
Describe the difference between SET and SELECT when assigning values to variables.
Consider how many variables each can assign and what happens if no data is returned.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of using variables in SQL with DECLARE and SET statements?
easy
A. To store temporary values that can be used later in the query
B. To permanently save data in the database
C. To create new tables automatically
D. To delete rows from a table
Solution
Step 1: Understand variable usage in SQL
Variables declared with DECLARE hold temporary values during query execution.
Step 2: Identify the purpose of SET
SET assigns values to these variables for use later in the query.
Final Answer:
To store temporary values that can be used later in the query -> Option A
Quick Check:
Variables = Temporary storage [OK]
Hint: Variables hold temporary data during query execution [OK]
Common Mistakes:
Thinking variables save data permanently
Confusing variables with table creation
Assuming variables delete data
2. Which of the following is the correct syntax to declare an integer variable named @count and set its value to 10 in SQL?
In SQL, DECLARE @variable_name TYPE; declares a variable.
Step 2: Assign value using SET
SET @variable_name = value; assigns the value.
Final Answer:
DECLARE @count INT; SET @count = 10; -> Option A
Quick Check:
DECLARE then SET = correct syntax [OK]
Hint: Declare first, then set value with SET [OK]
Common Mistakes:
Using SET before DECLARE
Mixing declaration and assignment in wrong order
Incorrect placement of data type
3. Consider the following SQL code:
DECLARE @x INT; SET @x = 5; SET @x = @x + 3; SELECT @x;
What will be the output of the SELECT statement?
medium
A. 5
B. 8
C. 3
D. NULL
Solution
Step 1: Initialize variable @x
@x is declared as integer and set to 5.
Step 2: Update @x by adding 3
@x = 5 + 3 = 8.
Final Answer:
8 -> Option B
Quick Check:
5 + 3 = 8 [OK]
Hint: Add values stepwise to variable [OK]
Common Mistakes:
Forgetting to update variable value
Assuming variable resets after SET
Confusing initial and updated values
4. Identify the error in the following SQL code:
DECLARE @total INT SET total = 100; SELECT @total;
medium
A. Missing semicolon after DECLARE statement
B. SELECT statement syntax is incorrect
C. Variable name missing '@' in SET statement
D. Variable @total not declared
Solution
Step 1: Check variable declaration
@total is declared correctly with DECLARE @total INT.
Step 2: Check SET statement
SET statement uses total without '@', which is incorrect; it should be @total.
Final Answer:
Variable name missing '@' in SET statement -> Option C
Quick Check:
Variable names need '@' prefix [OK]
Hint: Always use '@' before variable names [OK]
Common Mistakes:
Omitting '@' in variable usage
Forgetting semicolons (usually optional)
Misreading SELECT syntax
5. You want to calculate the total price for 3 items each costing 15, 20, and 25 using variables. Which SQL code correctly declares variables, sets their values, and calculates the total?
hard
A. DECLARE @item1 INT = 15, @item2 INT = 20, @item3 INT = 25; SET @total = @item1 + @item2 + @item3; SELECT @total;
B. DECLARE @total INT; SET total = 15 + 20 + 25; SELECT @total;
C. SET @item1 = 15; SET @item2 = 20; SET @item3 = 25; SET @total = @item1 + @item2 + @item3; SELECT @total;
D. DECLARE @item1 INT; DECLARE @item2 INT; DECLARE @item3 INT; DECLARE @total INT; SET @item1 = 15; SET @item2 = 20; SET @item3 = 25; SET @total = @item1 + @item2 + @item3; SELECT @total;
Solution
Step 1: Declare all variables before use
Variables @item1, @item2, @item3, and @total must be declared first.
Step 2: Assign values and calculate total
Assign each item price with SET, then sum them into @total.
Step 3: Select the total value
Use SELECT to output the total price.
Final Answer:
DECLARE @item1 INT; DECLARE @item2 INT; DECLARE @item3 INT; DECLARE @total INT; SET @item1 = 15; SET @item2 = 20; SET @item3 = 25; SET @total = @item1 + @item2 + @item3; SELECT @total; -> Option D
Quick Check:
Declare then set all variables before calculation [OK]
Hint: Declare all variables first, then assign and calculate [OK]
Common Mistakes:
Assigning values without declaring variables
Trying to declare and assign in one line incorrectly
Missing variable declarations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.