Databricks Data Engineer Associate Question 196
Single answerYou are working with a Databricks SQL environment and need to define a user-defined function (UDF) that calculates the square of a given number. Which of the following SQL commands correctly creates this UDF?
- A
CREATE FUNCTION square(num DOUBLE) RETURNS DOUBLE RETURN num * num;
- B
CREATE SQL FUNCTION square AS (num DOUBLE) RETURNS DOUBLE BEGIN RETURN num * num; END;
- C
CREATE FUNCTION square AS 'num DOUBLE -> num * num';
- D
CREATE FUNCTION square AS (x DOUBLE) RETURNS DOUBLE RETURN x * x;
Show answer and explanation
Correct answer: A
Explanation
In Databricks, SQL UDFs are defined using the CREATE FUNCTION statement, specifying the function name, input parameters with their data types, the RETURN data type, and the specific formula or logic to execute. Only the first option adheres to this standard.
- A. Correct.
This is the correct syntax for defining a SQL UDF in Databricks. It uses the CREATE FUNCTION statement with the correct RETURN clause and formula.
- B. Incorrect.
This syntax is incorrect because the 'AS' keyword is not used in SQL UDF creation in Databricks.
- C. Incorrect.
This syntax is invalid because it mixes a function definition style that does not adhere to SQL UDF standards in Databricks.
- D. Incorrect.
This syntax is incorrect as it uses a malformed RETURN clause and incomplete function structure.