
Answer-first summary for fast verification
Answer: CREATE FUNCTION combine_california (city STRING)RETURNS STRINGRETURN CASE WHEN city = “San Francisco“ THEN “California“ ELSE cityEND;
The correct syntax for creating a user-defined function (UDF) in SQL involves using the `CREATE FUNCTION` statement, followed by the function name and parameters. It's essential to specify the return type with `RETURNS STRING` and to use the `RETURN` keyword to define the function's output based on the input conditions. The correct code snippet includes all these elements and properly uses the `CASE` statement for conditional logic. Other options either incorrectly use `CREATE UDF` instead of `CREATE FUNCTION`, omit necessary syntax like `RETURNS STRING`, or misuse the `RETURN` keyword.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
To efficiently apply custom logic on a string column named 'city' in the 'location' table, a data engineer decides to create a SQL user-defined function (UDF). Which of the following code snippets correctly defines this SQL UDF?
A
CREATE UDF combine_california (city STRING)RETURN CASE WHEN city = “San Francisco“ THEN “California“ ELSE cityEND;
B
CREATE FUNCTION combine_california (city STRING)RETURNS STRINGCASE WHEN city = “San Francisco“ THEN “California“ ELSE cityEND;
C
CREATE FUNCTION combine_california (city STRING)RETURNS STRINGRETURN CASE WHEN city = “San Francisco“ THEN “California“ ELSE cityEND;
D
CREATE FUNCTION combine_california (city STRING)RETURN CASE WHEN city = “San Francisco“ THEN “California“ ELSE cityEND;
E
CREATE UDF combine_california (city STRING)RETURN CASE WHEN city = “San Francisco“ THEN “California“ ELSE cityEND;