0
0
SQLquery~20 mins

User-defined functions in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
User-defined Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a scalar user-defined function
Consider the following SQL user-defined function and 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;
AResult: 10
BResult: 7
CResult: 25
DSyntax error in function definition
Attempts:
2 left
💡 Hint
The function multiplies the input by 2.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this table-valued function
Examine the following SQL code for a table-valued function:

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);
AMissing BEGIN and END block around the RETURN statement
BIncorrect use of RETURNS TABLE without specifying TABLE type
CNo syntax error; the function is valid
DMissing parentheses after RETURNS TABLE
Attempts:
2 left
💡 Hint
Inline table-valued functions can omit BEGIN and END.
optimization
advanced
2:00remaining
Optimizing a scalar function for performance
You have a scalar function that calculates the square of a number:

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;
AKeep it as a scalar function but add WITH SCHEMABINDING
BRewrite it as an inline scalar function using RETURNS without BEGIN...END
CRewrite it as an inline table-valued function
DConvert it to a stored procedure
Attempts:
2 left
💡 Hint
Inline scalar functions can improve performance by avoiding context switches.
🔧 Debug
advanced
2:00remaining
Debugging a user-defined function with NULL handling
Given this function:

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;
ADivision by zero error at runtime
BReturns NULL without error
CSyntax error in function definition
DReturns 0 without error
Attempts:
2 left
💡 Hint
Dividing by zero is not allowed in SQL arithmetic.
🧠 Conceptual
expert
2:00remaining
Understanding side effects in user-defined functions
Which statement about user-defined functions (UDFs) in SQL Server is TRUE?
AUDFs can modify database state by performing INSERT, UPDATE, or DELETE operations.
BUDFs always execute faster than equivalent inline SQL queries.
CScalar UDFs can call stored procedures that modify data.
DTable-valued UDFs can be used in JOINs like regular tables or views.
Attempts:
2 left
💡 Hint
Think about what UDFs are allowed to do and how they behave in queries.