Databricks Data Engineer Associate Question 195
Single answerYou are working on a Databricks SQL workflow and need to create a SQL User-Defined Function (UDF) that calculates the square of a given integer. Which of the following SQL commands correctly defines this UDF?
- A
CREATE FUNCTION square(x INT) RETURNS INT RETURN x * x;
- B
CREATE OR REPLACE TEMPORARY FUNCTION square(x INT) RETURNS INT RETURN x * x;
- C
CREATE FUNCTION square(x INT) AS 'x * x';
- D
CREATE FUNCTION square AS (x INT) RETURNS INT RETURN x * x;
Show answer and explanation
Correct answer: B
Explanation
In Databricks SQL, User-Defined Functions (UDFs) are typically created as temporary functions using the CREATE OR REPLACE TEMPORARY FUNCTION syntax. This ensures the function is scoped to the current session and not permanently stored. The correct syntax includes specifying the function name, input parameters, return type, and the logic after the RETURN keyword.
- A. Incorrect.
This option is incorrect because Databricks SQL does not support creating permanent UDFs using the
CREATE FUNCTIONsyntax without specifyingOR REPLACE TEMPORARY. - B. Correct.
This is the correct option. In Databricks SQL, UDFs are typically defined as temporary functions using the
CREATE OR REPLACE TEMPORARY FUNCTIONsyntax, and the function logic is specified after theRETURNkeyword. - C. Incorrect.
This option is incorrect because the syntax
AS 'x * x'is not valid for defining functions in Databricks SQL. - D. Incorrect.
This option is incorrect because the function definition syntax is invalid for SQL UDFs in Databricks. The keyword
ASis misplaced and the overall structure is incorrect.