Challenge - 5 Problems
User-defined Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a scalar user-defined function
Consider the following SQL user-defined function and query:
What is the output of the SELECT query?
CREATE FUNCTION dbo.MultiplyByTwo(@num INT) RETURNS INT AS BEGIN RETURN @num * 2; END;
SELECT dbo.MultiplyByTwo(5) AS Result;
What is the output of the SELECT query?
SQL
CREATE FUNCTION dbo.MultiplyByTwo(@num INT) RETURNS INT AS BEGIN RETURN @num * 2; END; SELECT dbo.MultiplyByTwo(5) AS Result;
Attempts:
2 left
💡 Hint
The function multiplies the input by 2.
✗ Incorrect
The function takes an integer input and returns its value multiplied by 2. So 5 * 2 = 10.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this table-valued function
Examine the following SQL code for a table-valued function:
Which option correctly identifies the syntax error?
CREATE FUNCTION dbo.GetEvenNumbers() RETURNS TABLE AS RETURN (SELECT Number FROM Numbers WHERE Number % 2 = 0);
Which option correctly identifies the syntax error?
SQL
CREATE FUNCTION dbo.GetEvenNumbers() RETURNS TABLE AS RETURN (SELECT Number FROM Numbers WHERE Number % 2 = 0);
Attempts:
2 left
💡 Hint
Inline table-valued functions can omit BEGIN and END.
✗ Incorrect
This is a valid inline table-valued function syntax in SQL Server. It returns a table directly from the SELECT statement.
❓ optimization
advanced2:00remaining
Optimizing a scalar function for performance
You have a scalar function that calculates the square of a number:
Which option is the best way to optimize this function for performance in SQL Server?
CREATE FUNCTION dbo.Square(@x INT) RETURNS INT AS BEGIN RETURN @x * @x; END;
Which option is the best way to optimize this function for performance in SQL Server?
SQL
CREATE FUNCTION dbo.Square(@x INT) RETURNS INT AS BEGIN RETURN @x * @x; END;
Attempts:
2 left
💡 Hint
Inline scalar functions can improve performance by avoiding context switches.
✗ Incorrect
Using an inline scalar function (RETURNS expression without BEGIN...END) allows SQL Server to inline the function logic, improving performance.
🔧 Debug
advanced2:00remaining
Debugging a user-defined function with NULL handling
Given this function:
What error or issue will occur when calling dbo.SafeDivide(10, 0)?
CREATE FUNCTION dbo.SafeDivide(@a INT, @b INT) RETURNS FLOAT AS BEGIN RETURN @a / @b; END;
What error or issue will occur when calling dbo.SafeDivide(10, 0)?
SQL
CREATE FUNCTION dbo.SafeDivide(@a INT, @b INT) RETURNS FLOAT AS BEGIN RETURN @a / @b; END;
Attempts:
2 left
💡 Hint
Dividing by zero is not allowed in SQL arithmetic.
✗ Incorrect
Dividing by zero causes a runtime error in SQL Server, so calling the function with 0 as denominator will fail.
🧠 Conceptual
expert2:00remaining
Understanding side effects in user-defined functions
Which statement about user-defined functions (UDFs) in SQL Server is TRUE?
Attempts:
2 left
💡 Hint
Think about what UDFs are allowed to do and how they behave in queries.
✗ Incorrect
Table-valued functions return tables and can be joined like tables or views. UDFs cannot modify data or call stored procedures that do.