Courseiva

DA0-002 Data Acquisition and Preparation Practice Question

A data analyst wants to find the top 5 products by total sales amount, but only for products that have been sold more than 50 times. Which SQL query accomplishes this?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

SELECT product_id, SUM(sales_amount) FROM sales GROUP BY product_id HAVING COUNT(*) > 50 ORDER BY SUM(sales_amount) DESC LIMIT 5

HAVING filters after aggregation, then ORDER BY and LIMIT give the top 5.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    SELECT product_id, SUM(sales_amount) FROM sales GROUP BY product_id HAVING COUNT(*) > 50 ORDER BY SUM(sales_amount) DESC LIMIT 5

    Why this is correct

    HAVING filters grouped rows after aggregation, so COUNT(*) > 50 removes products with 50 or fewer sales before ranking. ORDER BY SUM(sales_amount) DESC with LIMIT 5 then returns the five highest-revenue products. WHERE cannot reference aggregates, making HAVING the only clause satisfying both the sales-count threshold and the top-5 constraint.

  • ✗

    SELECT product_id, SUM(sales_amount) FROM sales WHERE COUNT(*) > 50 GROUP BY product_id ORDER BY SUM(sales_amount) DESC LIMIT 5

    Why it's wrong here

    WHERE COUNT(*) > 50 is invalid because aggregates cannot appear in WHERE, and filtering before grouping would exclude rows before totals are computed. HAVING applies the condition after GROUP BY. HAVING is correct when the requirement is filtering groups by an aggregate count.

  • ✗

    SELECT product_id, SUM(sales_amount) FROM sales GROUP BY product_id HAVING COUNT(*) > 50 ORDER BY SUM(sales_amount) ASC LIMIT 5

    Why it's wrong here

    ORDER BY SUM(sales_amount) ASC returns the five lowest-selling products, the inverse of the top five requested. The HAVING COUNT(*) > 50 filter is correctly placed, so only the sort direction fails. DESC would be correct when the analyst wants the highest totals first.

  • ✗

    SELECT product_id, SUM(sales_amount) FROM sales GROUP BY product_id WHERE COUNT(*) > 50 ORDER BY SUM(sales_amount) DESC LIMIT 5

    Why it's wrong here

    WHERE cannot contain aggregate functions; COUNT(*) is only valid in HAVING after GROUP BY. This query errors rather than returning results. HAVING is correct when filtering grouped rows by an aggregate such as COUNT(*) > 50, which is exactly the requirement here.

About these practice questions

Courseiva writes every DA0-002 question from scratch — 1,004 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This DA0-002 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DA0-002 exam.