Troubleshooting Amazon Athena Queries with Date Extraction

Answer Correct answer: B — Change WHERE year = 2023 to WHERE extract(year FROM sales_data) = 2023.

A data engineer is using Amazon Athena to analyze sales data that is in Amazon S3. The data engineer writes a query to retrieve sales amounts for 2023 for several products from a table named sales_data. However, the query does not return results for all of the products that are in the sales_data table. The data engineer needs to troubleshoot the query to resolve the issue. The data engineer's original query is as follows: SELECT product_name, sum(sales_amount) FROM sales_data - WHERE year = 2023 - GROUP BY product_name - How should the data engineer modify the Athena query to meet these requirements?

  1. Replace sum(sales_amount) with count(*) for the aggregation.
  2. Change WHERE year = 2023 to WHERE extract(year FROM sales_data) = 2023. Correct Answer
  3. Add HAVING sum(sales_amount) > 0 after the GROUP BY clause.
  4. Remove the GROUP BY clause.

Community Votes

B
61%
C
39%

61% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

Tests understanding of SQL data types in Athena, specifically that filtering on a year value requires extracting it from a DATE/TIMESTAMP column rather than comparing directly to an integer.

This question addresses a common Athena query error where a numeric year filter fails because the source column is a date or timestamp type. The correct solution involves using the EXTRACT function to properly parse the year from the date column.

Learners often choose option C (HAVING sum > 0) assuming the goal is to filter out zero-value rows, but this ignores the fundamental syntax error in the WHERE clause regarding the date field.

Community Discussion (17 comments)

GiorgioGss 👍 12 Selected: B
"SELECT product_name, sum(sales_amount) FROM sales_data WHERE extract(year FROM sales_date) = 2023 GROUP BY product_name;" A. This would change the query to count the number of rows instead of summing sales. C. This would filter out products with zero sales amounts. D. Removing the GROUP BY clause would result in a single sum of all sales amounts without grouping by product_name.
pikuantne 👍 7
None of these options make sense. I think the question is worded incorrectly. I understand that the problem is supposed to be: the products that did not have any sales in 2023 should also be visible in the report with sum of sales_amount = 0. So, the WHERE condition should be deleted and replaced with a CASE WHEN. That way all of the products in the table will be visible, but only sales for 2023 will be summed. Which is what I think this question is asking. None of the provided options do that.
YUICH 👍 2 Selected: B
hy Option (B) Works If the underlying table field is a date or timestamp (rather than a numeric year column), using WHERE year = 2023 filters out all rows that do not literally match year = 2023. By using extract(year FROM sales_data) = 2023, you are correctly filtering rows whose date (or timestamp) in the sales_data column corresponds to the year 2023. Hence, (B) resolves the problem by filtering on the correct year value from the actual date/timestamp column, ensuring all qualifying products are included in the results.
Udyan 👍 1 Selected: C
The issue might be that some products have sales amounts of 0 or NULL, and those records are being excluded from the results because Athena may not include them in the final output when performing aggregation. By using the HAVING clause, you can filter the groups based on the aggregated sales amount (sum). This ensures that only products with a non-zero sum of sales are returned in the results. The HAVING clause is used to filter results after the aggregation.
MLOPS_eng 👍 1 Selected: C
The HAVING clause filters the results to include only products with an aggregated sales amount greater than zero.
Assassin27 👍 1 Selected: C
SELECT product_name, sum(sales_amount) FROM sales_data WHERE year = 2023 GROUP BY product_name HAVING sum(sales_amount) > 0 Explanation: The HAVING clause ensures that only products with a non-zero aggregated sales amount are included in the results. This will address cases where products exist in the table but have no sales data for 2023.
kailu 👍 1 Selected: C
There is no issue with the WHERE clause from the original query, so B is not the right option IMO.
Shatheesh 👍 2
C, query in the question is correct you just need to get amounts grater than Zero
valuedate 👍 5 Selected: B
year should be the partition in s3 so its necessary to extract. its not a column
VerRi 👍 2 Selected: C
No need to extract the year again
Just_Ninja 👍 1 Selected: C
https://docs.aws.amazon.com/kinesisanalytics/latest/sqlref/sql-reference-having-clause.html
Snape 👍 1 Selected: C
Wrong answers A. Replace sum(sales_amount) with count(*) for the aggregation. This option will return the count of records for each product, not the sum of sales amounts, which is the desired result. B. Change WHERE year = 2023 to WHERE extract(year FROM sales_data) = 2023. The year column likely stores the year value directly, so there's no need to extract it from a date or timestamp column. D. Remove the GROUP BY clause. Removing the GROUP BY clause will cause an error because the sum(sales_amount) aggregation function requires a GROUP BY clause to specify the grouping column (product_name in this case).
khchan123 👍 3
B B. Change WHERE year = 2023 to WHERE extract(year FROM sales_data) = 2023. The issue with the original query is that it assumes there is a column named year in the sales_data table. However, it's more likely that the date or timestamp information is stored in a single column, for example, a column named sales_date. To extract the year from a date or timestamp column, you need to use the extract() function in Athena SQL.
chris_spencer 👍 2
None of the answer makes senses. Option C will exclude any amount that is 0. This option would be correct if it is: Add HAVING sum(sales_amount) >= 0 after the GROUP BY clause.
Christina666 👍 2 Selected: C
Gemini: C. Add HAVING sum(sales_amount) > 0 after the GROUP BY clause. Zero Sales Products: The original query is likely missing products that had zero sales amount in 2023. This modification filters the grouped results, ensuring only products with positive sales are displayed. Why Other Options Don't Address the Core Issue: A. Replace sum(sales_amount) with count(*) for the aggregation. This would show how many sales transactions a product had, but not if it generated any revenue. It wouldn't solve the issue of missing products. B. Change WHERE year = 2023 to WHERE extract(year FROM sales_data) = 2023. This is functionally equivalent to the original WHERE clause if the year column is already an integer type. It wouldn't fix missing products. D. Remove the GROUP BY clause. This would aggregate all sales for 2023 with no product breakdown, losing the granularity needed.
kj07 👍 4
Not A because the engineer wants a sum not the total count. Not C because it will filter out the data with sales_amount zero. Not D because it will return just one result and the engineer wants the sales for multiple products. B should be the right answer if the sales_data is a date field.
rralucard_ 👍 2 Selected: C
https://docs.aws.amazon.com/athena/latest/ug/select.html

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The original query uses WHERE year = 2023, which implies a column named 'year' exists and is numeric. In many S3-based datasets (like Parquet or CSV with timestamps), the date is stored as a single DATE or TIMESTAMP column (e.g., sales_date). Option B correctly modifies the query to WHERE extract(year FROM sales_data) = 2023 (assuming sales_data refers to the date column or there's a typo in the question's column name reference, but the logic holds). This extracts the year component from the date/timestamp, allowing the filter to work correctly. Without this, if the column is indeed a date, the query might fail entirely or return no results due to type mismatch.

Why the Other Options Are Wrong

Option A changes the aggregation to count rows, which does not meet the requirement to sum sales amounts. Option C adds a HAVING clause to filter for positive sums, which would exclude products with zero sales, but it doesn't fix the underlying issue of how the year is being filtered in the WHERE clause. Option D removes the GROUP BY clause, resulting in a single aggregated total instead of per-product breakdowns, failing the requirement to retrieve amounts for 'several products'.

Community Comment Notes

Several users pointed out that the question phrasing is ambiguous, particularly regarding whether sales_data is a table name or a column name. As user pikuantne noted, "None of these options make sense" without clarifying the schema, but most agreed that B is the intended technical fix for date extraction. User valuedate confirmed that "year should be the partition... its not a column," supporting the need for extraction functions. Some users argued for C, believing the issue was zero-value exclusion, but this is secondary to the primary query execution logic.

Official Reference

Exam Strategy

When troubleshooting Athena queries involving dates, always check if the column is a DATE/TIMESTAMP type. If you are filtering by year/month/day, use the EXTRACT() function rather than direct equality comparison unless the column is explicitly defined as an integer representing the year.

Frequently Asked Questions

Why can't I just use WHERE year = 2023?

If the column storing the date is a DATE or TIMESTAMP type, comparing it directly to an integer (2023) causes a type mismatch or returns no rows. You must extract the year component first.

Does the HAVING clause fix missing zero-sales products?

No, HAVING filters grouped results after aggregation. It cannot fix a WHERE clause that fails to retrieve rows due to incorrect date filtering logic.

More DEA-C01 FAQ →

Related Analysis

Practice All DEA-C01 Questions

Access 100 questions with complete answers and detailed explanations.

View Full DEA-C01 Practice Test →

← Back to DEA-C01 Study Guide