Databricks Data Engineer Associate Question 314
Single answerA data engineer is working on a Delta table in Databricks containing transaction data. The table includes the columns transaction_id, amount, and timestamp. The engineer wants to create a new column, year_month, that extracts the year and month from the timestamp column in the format 'YYYY-MM'. This column should be automatically updated whenever new data is added to the table. Which of the following SQL commands should the engineer use to achieve this?
- A
ALTER TABLE transactions ADD COLUMN year_month STRING GENERATED ALWAYS AS (date_format(timestamp, 'yyyy-MM'))
- B
ALTER TABLE transactions ADD COLUMN year_month STRING GENERATED BY (date_format(timestamp, 'yyyy-MM'))
- C
ALTER TABLE transactions ADD COLUMN year_month STRING GENERATED ALWAYS AS (concat(year(timestamp), '-', month(timestamp)))
- D
ALTER TABLE transactions ADD COLUMN year_month STRING AS (date_format(timestamp, 'yyyy-MM'))
Show answer and explanation
Correct answer: A
Explanation
The correct syntax for creating a generated column in Databricks is to use the GENERATED ALWAYS AS clause followed by a valid expression. This ensures the column is derived from the specified expression and automatically updated whenever new data is added to the table. In this scenario, the correct expression to extract the year and month in 'YYYY-MM' format is date_format(timestamp, 'yyyy-MM').
- A. Correct.
This is the correct syntax for adding a generated column in Databricks. The
GENERATED ALWAYS ASclause ensures the column is derived from the specified expression and automatically updated. - B. Incorrect.
This is incorrect because
GENERATED BYis not valid syntax for creating generated columns in Databricks. - C. Incorrect.
This is incorrect because while
GENERATED ALWAYS ASis valid syntax, the expressionconcat(year(timestamp), '-', month(timestamp))does not produce the desired 'YYYY-MM' format. - D. Incorrect.
This is incorrect because the
ASkeyword alone does not define a generated column. It would not ensure the column is automatically updated.