Databricks Data Engineer Associate Question 112
Select 2You are tasked with analyzing a table named 'sales_data' in Databricks that contains the columns 'order_id', 'customer_id', and 'order_amount'. You want to identify the number of rows where 'customer_id' is null and compare it to the total number of rows in the table. Which of the following approaches correctly achieves this?
- A
Use
count_if(customer_id IS NULL)to count rows where 'customer_id' is null. - B
Use
count(*) - count(customer_id)to derive the count of rows where 'customer_id' is null. - C
Use
count(customer_id)to count rows where 'customer_id' is null. - D
Use
count_if(customer_id IS NOT NULL)to count rows where 'customer_id' is not null. - E
Use
count(*)to count rows where 'customer_id' is null.
Show answer and explanation
Correct answers: A, B
Explanation
To count rows where 'customer_id' is null, you can either use the count_if function with the condition IS NULL or calculate the difference between count(*) (total rows) and count(customer_id) (non-null rows). Both approaches are valid and commonly used for this type of analysis in Databricks.
- A. Correct.
Correct. The
count_iffunction can be used with the conditionIS NULLto directly count rows where 'customer_id' is null. - B. Correct.
Correct. Subtracting
count(customer_id)(which counts non-null values) fromcount(*)(which counts all rows) gives the count of rows where 'customer_id' is null. - C. Incorrect.
Incorrect. The
count(customer_id)function counts only non-null values in the 'customer_id' column, not null values. - D. Incorrect.
Incorrect. The
count_if(customer_id IS NOT NULL)function counts rows where 'customer_id' is not null, which is the opposite of what is required. - E. Incorrect.
Incorrect. The
count(*)function counts all rows in the table, regardless of whether 'customer_id' is null or not.