Exam Certified Data Analyst Associate All QuestionsBrowse all questions from this exam
Question 26

A data analyst has created a user-defined function using the following line of code:

CREATE FUNCTION price(spend DOUBLE, units DOUBLE)

RETURNS DOUBLE -

RETURN spend / units;

Which of the following code blocks can be used to apply this function to the customer_spend and customer_units columns of the table customer_summary to create column customer_price?

    Correct Answer: E

    To apply a user-defined function (UDF) in SQL, you typically call the function by its name and pass the appropriate columns as arguments within a SELECT statement. You can then alias the result to give it a descriptive name. In this case, the UDF 'price' takes two parameters, 'spend' and 'units', and returns their division. To create a new column 'customer_price' in the 'customer_summary' table, you can call the 'price' function with 'customer_spend' and 'customer_units' as arguments and alias the result as 'customer_price'. The correct code block that does this is: SELECT price(customer_spend, customer_units) AS customer_price FROM customer_summary.

Discussion
MrWood47Option: E

To apply a user-defined function (UDF) in SQL, you generally call the function by its name and pass the appropriate columns as arguments within a SELECT statement. The result is then aliased to give it a descriptive name, which in this case is customer_price. The UDF price created takes two arguments spend and units and returns their division as a DOUBLE. To apply this function to the customer_spend and customer_units columns of the customer_summary table and create a new column customer_price, you'd simply call the price function with these columns as arguments. Answer E statement selects the result of the price function applied to the customer_spend and customer_units columns and labels the result as customer_price for each row in the customer_summary table.