DA0-002 · Question #95
DA0-002 Question #95: Real Exam Question with Answer & Explanation
The correct answer is D: SELECT COUNT(DISTINCT EMPLOYEE_ID) FROM JOBS. To accurately count employees when some have multiple jobs, the COUNT(DISTINCT EMPLOYEE_ID) function is required to ensure each unique employee is counted only once.
Question
A data analyst receives a request for the current employee head count and runs the following SQL statement: SELECT COUNT(EMPLOYEE_ID) FROM JOBS The returned head count is higher than expected because employees can have multiple jobs. Which of the following should return an accurate employee head count?
Options
- ASELECT JOB_TYPE, COUNT DISTINCT(EMPLOYEE_ID) FROM JOBS
- BSELECT DISTINCT COUNT(EMPLOYEE_ID) FROM JOBS
- CSELECT JOB_TYPE, COUNT(DISTINCT EMPLOYEE_ID) FROM JOBS
- DSELECT COUNT(DISTINCT EMPLOYEE_ID) FROM JOBS
Explanation
To accurately count employees when some have multiple jobs, the COUNT(DISTINCT EMPLOYEE_ID) function is required to ensure each unique employee is counted only once.
Common mistakes.
- A.
COUNT DISTINCT(EMPLOYEE_ID)is syntactically incorrect;DISTINCTmust be inside theCOUNTfunction. Also, includingJOB_TYPEwould return a count per job type, not a total employee headcount. - B.
SELECT DISTINCT COUNT(EMPLOYEE_ID) FROM JOBSis syntactically incorrect;DISTINCTcannot be applied directly to the result of an aggregate functionCOUNTin this manner to achieve unique counts of individual rows before aggregation. - C. While
COUNT(DISTINCT EMPLOYEE_ID)is correct, includingJOB_TYPEwithout aGROUP BYclause is syntactically invalid for most SQL dialects, and ifGROUP BY JOB_TYPEwere added, it would return a distinct employee count per job type, not the total overall headcount.
Concept tested. SQL COUNT DISTINCT for unique records
Reference. https://learn.microsoft.com/en-us/sql/t-sql/functions/count-transact-sql?view=sql-server-ver16
Topics
Community Discussion
No community discussion yet for this question.