AWS Builder Center
Amazon Athena for Beginners: Querying Data in Amazon S3 with Standard SQL

Amazon Athena for Beginners: Querying Data in Amazon S3 with Standard SQL

Learn how Amazon Athena lets you analyze CSV, JSON and Parquet files stored in S3 using plain SQL, without servers or data loading, plus a simple example and tips to keep query costs low.

Introduction

Data analysis usually starts with a simple question: "what is in this data?" In many projects, though, answering it means setting up a database, loading the files and managing infrastructure before running a single query.
Amazon Athena removes that step. It is a serverless, interactive query service that lets you analyze data stored directly in Amazon S3 using standard SQL. There are no servers to manage, and you pay for the queries you run.

What is Amazon Athena?

Amazon Athena is an AWS analytics service that runs SQL queries on files stored in Amazon S3. Instead of moving data into a database, you describe the structure of your files and query them where they already are.
Athena's SQL engine is based on Trino, so anyone familiar with SQL can start writing queries quickly. It supports common data formats such as CSV, JSON, Apache Parquet, Apache ORC and Apache Avro.

Key Concepts of Athena

1. Data in Amazon S3

Your data stays in S3 buckets. Athena reads the files directly when a query runs, so there is no loading step.

2. Databases and Tables

In Athena, a table is a definition of how to read a set of files: column names, data types, file format and the S3 location. The table does not store the data itself.

3. AWS Glue Data Catalog

The metadata about your databases and tables is stored in the AWS Glue Data Catalog. AWS Glue crawlers can also scan your files and infer schemas automatically.

4. Schema-on-Read

Unlike traditional databases, which validate data when it is written, Athena applies the schema when the data is read. This makes it flexible for exploring raw or semi-structured data.

5. Query Results

Athena saves the results of each query to an S3 location you choose, so you can download or reuse them later.

How Athena Works

  1. You store your files in an S3 bucket.
  1. You create a table that tells Athena how to read those files.
  1. You run a SQL query in the Athena console, the AWS CLI, an SDK, or a BI tool.
  1. Athena scans the data in parallel and returns the results, usually in seconds.

A Simple Example

Imagine a CSV file with sales data stored in  s3://my-example-bucket/sales/ . First, create a database and a table:
CREATE DATABASE IF NOT EXISTS sales_demo;

CREATE EXTERNAL TABLE IF NOT EXISTS sales_demo.sales (
order_id string,
order_date date,
product string,
category string,
amount double
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
LOCATION 's3://my-example-bucket/sales/'
TBLPROPERTIES ('skip.header.line.count'='1');
Then query it like any other SQL table:
SELECT category,
COUNT(*) AS orders,
SUM(amount) AS total_sales
FROM sales_demo.sales
GROUP BY category
ORDER BY total_sales DESC;

Benefits of Amazon Athena

  • Serverless: no infrastructure to set up or manage
  • Uses standard SQL
  • Queries data directly in Amazon S3, with no loading step
  • Pay only for the queries you run
  • Scales automatically by running queries in parallel
  • Supports CSV, JSON, Parquet, ORC and Avro
  • Integrates with Amazon QuickSight and with BI tools through JDBC/ODBC drivers

Athena Use Cases

  • Ad-hoc exploration of datasets stored in S3
  • Analyzing application and service logs
  • Querying a data lake built on S3
  • Preparing data for dashboards and reports
  • Quick data quality checks before machine learning projects

Tips to Keep Costs Low

Athena charges based on the amount of data each query scans, so reducing scanned data reduces cost and speeds up queries:
  • Use columnar formats such as Parquet or ORC, so Athena reads only the columns your query needs.
  • Compress your data to reduce the bytes scanned.
  • Partition your data (for example, by year or month) so queries read only the relevant folders.
  • Select only the columns you need instead of using  SELECT * .

Getting Started

  1. Create an S3 bucket and upload a small CSV file.
  1. Open the Athena console and choose an S3 location for query results if prompted.
  1. Create a database and a table that points to your file's S3 location.
  1. Run your first  SELECT  query and explore the results.

Conclusion

Amazon Athena is a simple and powerful way to start analyzing data on AWS. By understanding how tables map to files in S3, how the Glue Data Catalog stores metadata, and how scanned data affects cost, beginners can go from raw files to useful insights using only SQL.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article