Challenge - 5 Problems
SQL Variable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this SQL variable assignment?
Consider the following SQL code snippet:
Assuming the Employees table has 5 employees in the Sales department, what will be the output of the SELECT statement?
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;
Attempts:
2 left
💡 Hint
The SET statement assigns the count of employees in Sales to the variable @count.
✗ Incorrect
The variable @count is assigned the number of employees in the Sales department using a subquery. Since there are 5 such employees, the SELECT returns 5.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
In SQL Server, DECLARE and SET are separate statements.
✗ Incorrect
Option B correctly declares the variable first, then sets its value. Option B is invalid in older SQL Server versions. Option B uses SET incorrectly. Option B uses MySQL syntax which is invalid in SQL Server.
❓ optimization
advanced2:30remaining
Optimizing variable assignment with multiple SET statements
You have the following SQL code:
Which option optimizes the variable assignments to reduce the number of queries?
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');
Attempts:
2 left
💡 Hint
Try to use a single query to assign both variables.
✗ Incorrect
Option A uses a single SELECT with conditional aggregation to assign both variables in one query, reducing database calls and improving performance.
🔧 Debug
advanced2:00remaining
Identify the error in this variable assignment
Examine the following SQL code:
What error will this code produce?
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;
Attempts:
2 left
💡 Hint
Check the syntax of the SET statement when assigning from a SELECT.
✗ Incorrect
The SET statement requires parentheses around the SELECT query or the SELECT must be used alone to assign variables. Missing parentheses cause a syntax error.
🧠 Conceptual
expert2:30remaining
Understanding variable scope and lifetime in SQL scripts
In a SQL Server script, you declare a variable inside a BEGIN...END block as:
What will happen when the last SELECT statement runs?
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;
Attempts:
2 left
💡 Hint
Consider the scope of variables declared inside BEGIN...END blocks.
✗ Incorrect
Variables declared inside a BEGIN...END block are scoped to that block and do not exist outside it. The SELECT outside the block cannot access @temp, causing a runtime error.