Introduction
The shop's CSV file is stored in S3 and described by a Glue catalog table. Use Amazon Athena to answer questions about those records with SQL, a language for selecting, filtering and grouping tabular data. Read all sales, select the east region and calculate a total for each region.
Complete Catalog Sales Files with Glue first. This lab starts independently with its own prepared CSV object, Glue database/table and AWS CLI identity. You do not need a personal AWS account or resources from the earlier VM. Use Terminal to submit ordinary SQL files and AWS View to inspect the input, catalog and actual query results. S3 stores the records, Glue describes them and Athena executes your query and stores its result in a separate S3 location.
Certification Relevance
| Certification | Exam task | Practice |
|---|---|---|
| Cloud Practitioner (CLF-C02) | Task 3.7 | Identify Athena as a service for SQL analytics and explain how it uses S3 files and Glue catalog metadata. |

Submit and Inspect a SELECT Query
In this step, submit SQL and follow its execution ID to the actual result. A SQL table presents named columns and rows. The sales table is external: its Glue definition points to the S3 CSV file rather than copying it into a database server.
Enter the project directory and confirm the current identity:
cd /home/labex/project
aws sts \
get-caller-identity
The ARN ends in labex-an02-operator. Read and display the database name and query result location. jq -r extracts a plain JSON value; $(...) saves command output in a shell variable:
DATABASE=$(jq -r .database resources.json)
echo "$DATABASE"
RESULT_LOCATION=$(jq -r .output_location resources.json)
echo "$RESULT_LOCATION"
The database is labex_sales. The result location is this fresh environment's separate bucket with a queries/ prefix. It holds query output, while the input bucket holds sales records.
Inspect the prepared table's columns and input location:
aws glue \
get-table \
--database-name "$DATABASE" \
--name sales \
--query 'Table.{Columns:StorageDescriptor.Columns,Location:StorageDescriptor.Location}'
The columns are order_id and region strings plus integer total_cents. The file contains three September sales: S01/east/1000, S02/west/2000 and S03/east/500. Open AWS View and expand the input CSV if you want to compare the raw records.
SELECT chooses columns, FROM chooses the catalog table and ORDER BY makes the displayed row order predictable. Write an ordinary SQL file. cat > all-sales.sql <<'EOF' writes the enclosed text to a file; the quoted delimiter preserves the SQL exactly:
cat > all-sales.sql <<'EOF'
SELECT order_id, region, total_cents
FROM sales
ORDER BY order_id;
EOF
Submit the file. --query-execution-context chooses the Glue database, and --result-configuration supplies the S3 output prefix. The CLI's file:// syntax reads the SQL file. --query selects the returned ID from the JSON response; it is a CLI output filter, separate from the SQL query:
QUERY_ID=$(aws athena \
start-query-execution \
--query-string file://all-sales.sql \
--query-execution-context "Database=$DATABASE" \
--result-configuration "OutputLocation=$RESULT_LOCATION" \
--query QueryExecutionId \
--output text)
echo "$QUERY_ID"
The ID identifies this specific execution. Submitting a query does not by itself prove success. Read its state and result object location:
aws athena \
get-query-execution \
--query-execution-id "$QUERY_ID" \
--query 'QueryExecution.{State:Status.State,Reason:Status.StateChangeReason,Output:ResultConfiguration.OutputLocation}'
Proceed when State is SUCCEEDED. If you see QUEUED or RUNNING, repeat this status command until it completes. FAILED means you should inspect Reason and your SQL, catalog or file location rather than treating an execution ID as a successful result.
Read the result rows. Athena represents cell values as VarCharValue; this CLI output filter displays the rows as a compact table:
aws athena \
get-query-results \
--query-execution-id "$QUERY_ID" \
--query 'ResultSet.Rows[*].Data[*].VarCharValue' \
--output table
The first row names the columns. The remaining three rows contain S01/east/1000, S02/west/2000 and S03/east/500. Integer source values appear as text in the result API; the table's column metadata still describes their types. AWS View now shows a successful execution, its SQL and its actual result rows. Its S3 output is a CSV object named with this query ID.
Filter Sales with WHERE
In this step, return only records from the east region. WHERE applies a condition to each input row. The expression region = 'east' compares the string column with a quoted SQL string value. Selecting fewer columns changes the result shape; it does not change the source file.
Write a new query with the filter:
cat > east-sales.sql <<'EOF'
SELECT order_id, total_cents
FROM sales
WHERE region = 'east'
ORDER BY order_id;
EOF
Submit it as a separate execution. Save and display its ID so later commands refer to this filtered result:
EAST_QUERY_ID=$(aws athena \
start-query-execution \
--query-string file://east-sales.sql \
--query-execution-context "Database=$DATABASE" \
--result-configuration "OutputLocation=$RESULT_LOCATION" \
--query QueryExecutionId \
--output text)
echo "$EAST_QUERY_ID"
Inspect this execution's state. As before, repeat the status command for QUEUED or RUNNING, and proceed only when it succeeds:
aws athena \
get-query-execution \
--query-execution-id "$EAST_QUERY_ID" \
--query 'QueryExecution.{State:Status.State,Reason:Status.StateChangeReason}'
Read its rows:
aws athena \
get-query-results \
--query-execution-id "$EAST_QUERY_ID" \
--query 'ResultSet.Rows[*].Data[*].VarCharValue' \
--output table
After the header, only S01/1000 and S03/500 remain. The west-region S02 row is absent from this result but still exists in S3. AWS View displays the new execution alongside the earlier one; each has its own query ID and S3 output object. Do not delete or modify the input records to obtain a filtered answer.
Group Sales into Regional Totals
In this step, answer a different question: how much did each region sell? Aggregation combines multiple rows into a smaller result. SUM(total_cents) adds the amounts, and GROUP BY region creates one total per region. AS revenue_cents gives the computed column a readable name.
Write the grouped query:
cat > regional-summary.sql <<'EOF'
SELECT region, SUM(total_cents) AS revenue_cents
FROM sales
GROUP BY region
ORDER BY region;
EOF
region appears both as an output column and a grouping key. Each result row now represents a region, rather than an individual order. Submit the query and display its execution ID:
SUMMARY_QUERY_ID=$(aws athena \
start-query-execution \
--query-string file://regional-summary.sql \
--query-execution-context "Database=$DATABASE" \
--result-configuration "OutputLocation=$RESULT_LOCATION" \
--query QueryExecutionId \
--output text)
echo "$SUMMARY_QUERY_ID"
Confirm successful execution before reading results:
aws athena \
get-query-execution \
--query-execution-id "$SUMMARY_QUERY_ID" \
--query 'QueryExecution.{State:Status.State,Reason:Status.StateChangeReason,Output:ResultConfiguration.OutputLocation}'
Retrieve the grouped rows:
aws athena \
get-query-results \
--query-execution-id "$SUMMARY_QUERY_ID" \
--query 'ResultSet.Rows[*].Data[*].VarCharValue' \
--output table
The result contains east/1500 and west/2000. East combines 1000 + 500; west has one 2000-cent sale. The totals therefore add up to the original 3500 cents. Expand this execution's SQL and result rows in AWS View and compare them with the input CSV. 
Grouping computed a new answer from the real input; it did not update the source or catalog.
Summary
You submitted ordinary SQL files to Athena, followed real execution IDs through state and result APIs, filtered rows with WHERE and computed regional totals with GROUP BY and SUM. S3 kept the original CSV and separate query output objects, while Glue supplied the schema and input location. The next lab introduces monthly partitions so new files can be cataloged and queried correctly.
For further reading, see the AWS reference on SELECT filters, groups, and ordering; Locate output files by query execution.



