Lesson 1 of 8 Amazon Athena and Data Analysis

Querying the Lake with Athena SQL

This lesson explains how Amazon Athena, a serverless interactive SQL query service for data in S3, runs queries with nothing to provision, and how cost and speed depend on the data scanned.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how Athena separates storage from compute.
  • Use SQL features such as CTAS, INSERT INTO and MERGE.
  • Reduce cost and improve performance by scanning less data.
  • Use workgroups to separate teams.

What Athena queries

Amazon Athena separates storage from compute: the data stays in Amazon S3, and Athena brings a query engine to it only while a query runs. Athena uses a distributed SQL engine based on Trino, which splits each query across many workers that read different parts of the data in parallel, so large datasets are scanned quickly without any cluster to manage.

Athena learns where data is and how it is structured from table definitions in the AWS Glue Data Catalog, which crawlers, ETL jobs or CREATE TABLE statements populate. It queries files in CSV, JSON, Parquet, ORC and Avro formats, Apache Iceberg tables, and the logs that AWS services deliver to S3, such as AWS CloudTrail, Application Load Balancer and VPC Flow Logs, which makes it a common tool for investigating operational and security events as well as for analytics.

SQL features

Athena's SQL goes well beyond simple queries. Standard SELECT statements with joins and window functions handle everyday analytics. CTAS (CREATE TABLE AS SELECT) writes a query's results as a new table, which can be partitioned and stored as Parquet, so it is a simple way to transform and convert data. INSERT INTO appends results to an existing table, and views save queries for reuse.

For Iceberg tables, Athena supports data manipulation: MERGE, UPDATE and DELETE change rows, and OPTIMIZE and VACUUM maintain the table. Iceberg time travel queries a table as it was earlier, and prepared statements run parameterised SQL safely. The following table summarises the features.

FeatureUse
SELECT with joins and window functionsStandard analytics
CTAS (CREATE TABLE AS SELECT)Write results as new, partitioned Parquet tables
INSERT INTO, viewsAppend data; reuse queries
Iceberg DML: MERGE, UPDATE, DELETE, OPTIMIZE, VACUUMChange and maintain lake tables
Time travel, prepared statementsHistorical queries; parameterised SQL
Match the pairs

Athena SQL features

Exam skills 3.2.3 · 3.1.7

Match each requirement with the Athena SQL feature.

Log in to try this activity Interactive activities are free with an account.

Cost and performance

With on-demand pricing, Athena charges about $5 per terabyte of data scanned, with a minimum of 10 MB per query. Cost and query time therefore both depend mainly on how much data a query has to read, and the same techniques reduce both. Teams with steady, heavy workloads can instead buy provisioned capacity, measured in data processing units (DPUs), and pay for the capacity rather than for data scanned.

The most effective technique is storing data in columnar formats, such as Parquet and ORC, with compression. A columnar file stores each column separately, so a query that uses three columns of a fifty-column table reads only those three, and compression shrinks what is read further. Partitioning stores data in folders by a column such as date, so a query filtered on that column reads only the matching folders; partition projection calculates the partitions from a pattern instead of looking them up, which speeds up queries on tables with very many partitions.

Other techniques help further. Compacting many small files into fewer large ones reduces the overhead of opening each file. Selecting only the columns needed, instead of SELECT *, lets columnar formats skip the rest. Query result reuse returns the stored result of an identical query run within a set time window, which suits dashboards that repeat the same queries. EXPLAIN and the query statistics in the console show where a slow query spends its time.

The following diagram shows a one-time CTAS conversion of raw JSON logs and the Athena workgroup that protects analysts from expensive queries.

Converting raw JSON logs once with CTAS. The statement reads the raw table from September onwards and writes Snappy-compressed Parquet partitioned by dt to the curated location, so every later query scans far less. The analysts' workgroup enforces a results location and a per-query scan limit, so an accidental SELECT * over years of raw data is cancelled.
The scenario is illustrative.
Fill in the value

Athena pricing

Exam skills 1.2.4 · 3.1.7

Work out Athena on-demand costs at about $5 per terabyte scanned.

Log in to try this activity Interactive activities are free with an account.

Workgroups

Athena organises queries into workgroups, which separate teams, applications or environments. Each workgroup sets where query results are stored and how they are encrypted, which engine version runs its queries, and data usage limits that cancel queries scanning too much data or alert when the workgroup's total usage passes a threshold. Each workgroup also publishes its own CloudWatch metrics, which supports chargeback of query costs to the teams that incur them. The lesson Controlling Athena Costs with Workgroups and Data Layout covers these controls in detail.

Worked example

A CTAS that pays for itself

Analysts repeatedly query raw JSON logs. A one-off CREATE TABLE AS SELECT converts the data to partitioned Parquet, and every later query is cheaper:

CREATE TABLE logs_curated
WITH (format = 'PARQUET', write_compression = 'SNAPPY',
      external_location = 's3://lake/curated/logs/', partitioned_by = ARRAY['dt'])
AS SELECT request_id, status, latency_ms, path, dt
   FROM raw_logs
   WHERE dt >= '2026-09-01';

A workgroup for the analysts enforces a per-query scan limit and a results location, so an accidental SELECT * over years of raw data is cancelled before it costs much. Partition columns go last in the SELECT list, as Athena requires.

Check yourself

How can the data scanned by each team's Athena queries be capped?

Show answer

With workgroup data usage limits.

Exam Tips

Remember for the exam

  • To lower Athena costs, use Parquet, compression and partitions, and select only the columns you need.
  • To cap the data scanned per query or per team, set workgroup data usage limits.
  • To convert CSV into partitioned Parquet with SQL, use CTAS.
  • To separate teams' settings and costs, use workgroups.
  • For repeated identical queries, enable query result reuse.