
Ultimate access to all questions.
A data engineer needs to apply custom logic to string column city in table stores for a specific use case. In order to apply this custom logic at scale, the data engineer wants to create a SQL user-defined function (UDF).
Which of the following code blocks creates this SQL UDF?
A
CREATE FUNCTION combine_nyc(city STRING)
RETURNS STRING
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
CREATE FUNCTION combine_nyc(city STRING)
RETURNS STRING
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
B
CREATE UDF combine_nyc(city STRING)
RETURNS STRING
CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
CREATE UDF combine_nyc(city STRING)
RETURNS STRING
CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
C
CREATE UDF combine_nyc(city STRING)
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
CREATE UDF combine_nyc(city STRING)
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
D
CREATE FUNCTION combine_nyc(city STRING)
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
CREATE FUNCTION combine_nyc(city STRING)
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
E
CREATE UDF combine_nyc(city STRING)
RETURNS STRING
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
CREATE UDF combine_nyc(city STRING)
RETURNS STRING
RETURN CASE
WHEN city = "brooklyn" THEN "new york"
ELSE city
END;
Explanation:
Option A is the correct syntax for creating a SQL user-defined function (UDF) in Databricks:
CREATE FUNCTION - This is the standard SQL syntax for creating functions in Databricks SQLRETURNS STRING - Properly declares the return type of the functionRETURN CASE ... END - Correctly uses the RETURN keyword followed by the CASE expressionCREATE UDF which is not standard SQL syntax in DatabricksCREATE UDF and missing RETURNS STRING declarationRETURNS STRING declaration after CREATE FUNCTIONCREATE UDF which is not the standard syntax in Databricks SQLIn Databricks SQL, the correct syntax follows ANSI SQL standards where you use CREATE FUNCTION to define scalar user-defined functions. The function body must return a value using the RETURN keyword, and the return type must be declared with RETURNS <type>.