0
0
SQLquery~20 mins

Variables and SET statements in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SQL Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this SQL variable assignment?
Consider the following SQL code snippet:

DECLARE @count INT;
SET @count = (SELECT COUNT(*) FROM Employees WHERE Department = 'Sales');
SELECT @count AS SalesCount;

Assuming the Employees table has 5 employees in the Sales department, what will be the output of the SELECT statement?
SQL
DECLARE @count INT;
SET @count = (SELECT COUNT(*) FROM Employees WHERE Department = 'Sales');
SELECT @count AS SalesCount;
ARuntime error: variable not declared
B
SalesCount
0
CSyntax error due to missing semicolon
D
SalesCount
5
Attempts:
2 left
💡 Hint
The SET statement assigns the count of employees in Sales to the variable @count.
📝 Syntax
intermediate
2:00remaining
Which option correctly declares and sets a variable in SQL?
You want to declare an integer variable named @total and set it to 100. Which of the following options is syntactically correct in standard SQL Server syntax?
ADECLARE @total INT = 100;
BDECLARE @total INT; SET @total = 100;
CSET @total INT = 100;
DDECLARE INT @total; SET @total := 100;
Attempts:
2 left
💡 Hint
In SQL Server, DECLARE and SET are separate statements.
optimization
advanced
2:30remaining
Optimizing variable assignment with multiple SET statements
You have the following SQL code:

DECLARE @a INT;
DECLARE @b INT;
SET @a = (SELECT COUNT(*) FROM Orders WHERE Status = 'Pending');
SET @b = (SELECT COUNT(*) FROM Orders WHERE Status = 'Shipped');

Which option optimizes the variable assignments to reduce the number of queries?
SQL
DECLARE @a INT;
DECLARE @b INT;
SET @a = (SELECT COUNT(*) FROM Orders WHERE Status = 'Pending');
SET @b = (SELECT COUNT(*) FROM Orders WHERE Status = 'Shipped');
A
DECLARE @a INT, @b INT;
SELECT @a = SUM(CASE WHEN Status = 'Pending' THEN 1 ELSE 0 END), @b = SUM(CASE WHEN Status = 'Shipped' THEN 1 ELSE 0 END) FROM Orders;
B
DECLARE @a INT, @b INT;
SET @a = (SELECT COUNT(*) FROM Orders WHERE Status IN ('Pending', 'Shipped'));
SET @b = @a;
C
DECLARE @a INT, @b INT;
SELECT @a = COUNT(*), @b = COUNT(*) FROM Orders WHERE Status = 'Pending' OR Status = 'Shipped';
D
DECLARE @a INT, @b INT;
SET @a = (SELECT COUNT(*) FROM Orders WHERE Status = 'Pending');
SET @b = (SELECT COUNT(*) FROM Orders WHERE Status = 'Shipped');
Attempts:
2 left
💡 Hint
Try to use a single query to assign both variables.
🔧 Debug
advanced
2:00remaining
Identify the error in this variable assignment
Examine the following SQL code:

DECLARE @name VARCHAR(50);
SET @name = SELECT FirstName FROM Employees WHERE EmployeeID = 10;

What error will this code produce?
SQL
DECLARE @name VARCHAR(50);
SET @name = SELECT FirstName FROM Employees WHERE EmployeeID = 10;
ASyntax error near 'SELECT'
BNo error, variable assigned correctly
CRuntime error: variable not declared
DType mismatch error
Attempts:
2 left
💡 Hint
Check the syntax of the SET statement when assigning from a SELECT.
🧠 Conceptual
expert
2:30remaining
Understanding variable scope and lifetime in SQL scripts
In a SQL Server script, you declare a variable inside a BEGIN...END block as:

BEGIN
DECLARE @temp INT = 5;
END
SELECT @temp;

What will happen when the last SELECT statement runs?
SQL
BEGIN
  DECLARE @temp INT = 5;
END
SELECT @temp;
AOutput: 5
BSyntax error due to variable declaration inside BEGIN...END
CRuntime error: Must declare the scalar variable "@temp".
DOutput: NULL
Attempts:
2 left
💡 Hint
Consider the scope of variables declared inside BEGIN...END blocks.