Querying Data in Amazon S3 with Amazon Athena
This lesson explains how Amazon Athena runs standard SQL directly against data in Amazon S3, with no servers to manage and no data to load, and how to keep the cost of each query low.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how a table definition tells Athena where data is stored, in what format and with which columns.
- Identify how columnar formats, compression and partitioning reduce the data a query scans and its cost.
- Describe common uses of Athena, such as investigating AWS CloudTrail and load balancer logs.
- Explain how workgroups control query results, encryption and cost.
Defining tables
Amazon Athena queries files where they already are in Amazon S3, without loading them into a database. To do this, it needs a table definition that says where the files are, what format they are in and which columns they contain. These definitions are stored in the AWS Glue Data Catalog, a central metadata store that other services, such as Amazon EMR and Amazon Redshift Spectrum, can also use. The data itself never moves: dropping an Athena table removes only the definition.
Tables can be defined in several ways. A CREATE EXTERNAL TABLE statement describes the columns, format and S3 location explicitly. An AWS Glue crawler scans an S3 location, infers the schema and creates or updates the table automatically, which suits data whose structure changes. A CREATE TABLE AS SELECT (CTAS) query creates a new table from the results of a query and writes those results to S3, often converting them to a more efficient format along the way, as the next section shows.
Large datasets are usually partitioned: the files are stored in folders by the value of a column, such as dt=2026-10-01/. Athena must know which partitions exist, either from the catalog, which is updated with MSCK REPAIR TABLE or by a crawler, or through partition projection, which calculates the partitions from a pattern, such as a date range, and so needs no catalog updates when new folders appear.
Cost and performance
Athena reads CSV, JSON, Parquet, ORC and other formats, and you pay for the data each query scans. Three techniques cut both cost and query time:
- Columnar formats such as Parquet, so queries read only the columns they use.
- Compression.
- Partitioning, for example by date, so queries read only matching folders.
Example. This CTAS statement converts raw JSON logs into Parquet partitioned by date. Partition columns come last in the SELECT.
CREATE TABLE logs_parquet
WITH (format = 'PARQUET',
external_location = 's3://analytics-bucket/logs-parquet/',
partitioned_by = ARRAY['dt'])
AS SELECT request_id, status, latency_ms, dt
FROM logs_raw;A query such as SELECT count(*) FROM logs_parquet WHERE dt = '2026-10-01' AND status = 500 now reads one day's Parquet files and only two columns, instead of every JSON file.
Common uses
Athena suits questions that need SQL over data already stored in S3, particularly when the questions are occasional and do not justify running a database or data warehouse. A common developer use is investigating logs: AWS CloudTrail, Application Load Balancer, Amazon CloudFront and VPC Flow Logs all write to S3, and Athena can answer questions such as which IAM principal deleted a resource or which client caused a rise in 5xx errors without any data being moved.
Athena also queries data lakes and the output of Amazon Data Firehose, which commonly delivers streaming data to S3 in Parquet format, partitioned by time. With federated queries, Lambda-based connectors let Athena query other sources, such as Amazon DynamoDB, relational databases and on-premises systems, and join them with data in S3 in a single SQL statement.
Workgroups and the API
Workgroups organise queries and control their cost. Each workgroup can set the S3 location where query results are written, require those results to be encrypted, and enforce data usage controls, which cancel any query that would scan more than a set amount of data and can alert when a workgroup's total usage exceeds a threshold. Separate workgroups for different teams or applications also keep their query histories and costs apart.
Applications run queries through the Athena API, which is asynchronous because a query can take seconds or minutes. The application calls StartQueryExecution, which returns a query execution ID at once. It then polls GetQueryExecution until the state is SUCCEEDED or FAILED, and finally reads the rows with GetQueryResults, which pages through large result sets, or reads the result file directly from S3.
Cutting a $5 query to a fraction of a cent
A query over a year of JSON application logs (1 TB) scans everything each time, costing about $5 per query at $5 per TB scanned. The developer reorganises the data:
| Change | Data scanned for one day's errors |
|---|---|
| Original JSON, no partitions | 1 TB |
Partition by date (dt=2026-10-01) | About 3 GB (one day) |
| Convert to Parquet with CTAS | About 300 MB (columnar, compressed) |
| Select only needed columns | About 10 MB |
The same answer now costs a fraction of a cent. A workgroup with a per-query data limit prevents anyone accidentally running the original full scan again.
Which two changes most reduce the data an Athena query scans over CSV logs in S3?
Show answer
Converting the data to a compressed columnar format such as Parquet, and partitioning it, for example by date, so queries read only the partitions they need. Athena charges by the amount of data scanned.
Exam Tips
Remember for the exam
- Ad hoc SQL over files in S3, without loading them anywhere, points to Athena.
- To reduce Athena cost, use Parquet, compression and partitioning.
- Athena API calls are asynchronous: start the query, poll its status, then fetch the results.
- To limit how much data a query can scan, use workgroup data usage controls.