Databricks Data Engineer Associate Question 194
Single answerYou are working on a Databricks SQL notebook and need to create a SQL User-Defined Function (UDF) to calculate the square of a given number. Which of the following is the correct syntax to define and use a SQL UDF in Databricks?
- A
CREATE FUNCTION square(x INT) RETURNS INT RETURN x * x;
- B
CREATE FUNCTION square AS (x INT) RETURNS INT -> x * x;
- C
CREATE OR REPLACE FUNCTION square(x INT) RETURNS INT RETURN x * x;
- D
CREATE OR REPLACE FUNCTION square AS (x INT) RETURNS INT -> x * x;
Show answer and explanation
Correct answer: C
Explanation
In Databricks SQL, to define a SQL UDF, you must use the CREATE OR REPLACE FUNCTION statement followed by the function name, input parameters with their types, the return type, and a valid RETURN statement specifying the logic of the function. Option 3 follows this syntax correctly, making it the right choice.
- A. Incorrect.
This option is incorrect because it does not use the
CREATE OR REPLACE FUNCTIONsyntax, which is required in Databricks SQL for defining a UDF. - B. Incorrect.
This option is incorrect because the syntax used for defining the function body with
ASand->is not valid in Databricks SQL. - C. Correct.
This is the correct option because it uses the correct syntax for defining a SQL UDF in Databricks SQL, including the
CREATE OR REPLACE FUNCTIONclause and a valid RETURN statement. - D. Incorrect.
This option is incorrect because, while it uses
CREATE OR REPLACE FUNCTION, the use ofASand->to define the function body is not valid in Databricks SQL.