Unleashing Data Insights: Harnessing the SQL Power of AWS Athena

Amazon Athena is an interactive query service offered by Amazon Web Services (AWS) that enables you to analyze data stored in Amazon S3 using standard SQL queries. It provides a serverless and pay-as-you-go platform for running ad hoc queries on large datasets without the need for managing infrastructure or setting up complex data warehouses. Athena is a part of the AWS Big Data ecosystem and is designed to simplify the process of querying data and gaining insights from it.

Athena is built upon Presto, a distributed SQL engine open-sourced by Facebook. It offers support for both ANSI SQL and a range of Presto SQL features. These encompass functions for geospatial operations and the incorporation of rough query extensions. These extensions enable the execution of approximate queries on extensive datasets, delivering results with statistically bounded errors in a significantly reduced timeframe.

Key Features and Benefits

  1. Serverless Architecture: Athena is serverless, meaning you don’t need to provision or manage any infrastructure. It automatically scales to handle your query workload, and you are only charged for the queries you run.
  2. Standard SQL Queries: Athena supports standard SQL syntax, making it accessible to users familiar with SQL. This enables data analysts, data scientists, and developers to quickly start querying and analyzing data without learning new query languages.
  3. Integration with Amazon S3: Athena is optimized to work with data stored in Amazon S3. This allows you to use data that is already stored in your S3 buckets for analysis, without the need to move or transform the data.
  4. Schema-on-Read: Athena employs a schema-on-read approach. This means that the structure and schema of the data are applied during query execution rather than upfront during data ingestion. This provides flexibility in working with different data formats and evolving schemas.
  5. Support for Various Data Formats: Athena supports various data formats, including JSON, Parquet, ORC, Avro, and more. This enables you to choose the format that best suits your data and query requirements.
  6. Partitioning and Performance: You can partition your data in Amazon S3 to improve query performance. Athena takes advantage of partition pruning, which reduces the amount of data scanned during queries, leading to faster results and lower costs.
  7. Data Encryption and Security: Athena encrypts data at rest and supports Amazon VPC (Virtual Private Cloud) for isolating and securing your query executions.
  8. Ad Hoc Analysis: Athena is particularly useful for ad hoc analysis and exploratory data analysis. It empowers users to quickly run queries without the need to set up and manage complex data processing pipelines.
  9. Integration with Visualization Tools: The query results from Athena can be easily integrated with various visualization tools, such as Amazon QuickSight or third-party Business Intelligence (BI) tools.

Use Cases

  1. Log Analysis: Athena can be used to analyze and gain insights from log files, making it valuable for monitoring and troubleshooting applications and systems.
  2. Data Exploration: Data analysts and scientists can use Athena to explore large datasets, identify trends, and uncover hidden patterns.
  3. Ad Hoc Reporting: Athena enables users to create custom reports on-demand without requiring pre-built dashboards, making it suitable for generating one-time or infrequent reports.
  4. Market Research: Marketers can analyze customer behavior, preferences, and trends by querying data stored in S3 using Athena.
  5. Security Analysis: Security teams can analyze access logs and detect potential security breaches by querying logs and events.
  6. Cost Optimization: By analyzing billing and usage data, organizations can optimize their AWS resources and reduce costs.
  7. Gaming Analytics: Gaming companies can analyze player behavior, game performance, and engagement metrics using Athena.


Before you begin working with AWS Athena, you need to ensure you have the following prerequisites in place:

  • AWS Account: You should have an active AWS account that will allow you to access and manage AWS services, including Athena.
  • Amazon S3 Bucket: Athena queries data stored in Amazon S3, so you need to have an S3 bucket where your data is stored. Make sure you have the necessary permissions to access this bucket.
  • Data Format: Your data should be stored in a supported format, such as Parquet, ORC, CSV, JSON, etc. Ensure that your data is properly organized in your S3 bucket.

Understanding tables, databases, and data catalogs

Within Athena, the fundamental units for organizing metadata definitions that establish a schema for the underlying source data are catalogs, databases, and tables. The hierarchies of these data objects are denoted using the subsequent terminology:

  • Data source: This encompasses a collection of databases.
  • Database: A database comprises a collection of tables.
  • Table: Tables are the repositories for data, organized as rows or columns.

At times, these entities might also be denoted by interchangeable but synonymous terms:

  • A data source can alternatively be called a catalog.
  • A database might be referred to as a schema.

For instance, the following query example in the Athena console utilizes the AwsDataCatalog as the data source, operates within the movie-db database, and interacts with the movies table.

Every dataset in Athena requires a corresponding table. The table’s metadata serves as a guide for Athena, indicating the data’s storage location in Amazon S3, and outlining the data’s structure, including particulars like column names, data formats, and the table’s identity. Databases, on the other hand, group tables logically, containing solely metadata and schema details for a given dataset.

In order to query a dataset through Athena, a table must exist to facilitate the retrieval and presentation of query outcomes. Consequently, before analyzing data, it’s essential to register a table within Athena. This registration process can be accomplished through either automated or manual table creation.

The use of an AWS Glue crawler can automate table creation. When such a crawler constructs a table, it automatically registers it in the AWS Glue Data Catalog. Athena relies on this catalog to store and retrieve metadata, utilizing it during query execution for dataset analysis.

Irrespective of the method used to create tables, the act of creating a table formally registers the dataset with Athena. This registration resides within the AWS Glue Data Catalog and empowers Athena to perform queries on the dataset. In the Athena query interface, this catalog is identified as the AwsDataCatalog.

Once a table is established, you can employ SQL SELECT statements to query its contents. The results of your queries are deposited in your specified Amazon S3 location for query results.

For manual creation of tables and databases, Athena employs HiveQL data definition language (DDL) statements, such as CREATE TABLE, CREATE DATABASE, and DROP TABLE. These statements work behind the scenes to establish tables and databases within the AWS Glue Data Catalog.

Data Ingestion and Preparation

Data ingestion and preparation are critical steps in making your data available for analysis in AWS Athena. This process involves uploading your data to Amazon S3, defining how Athena should access and interpret that data through tables, and optimizing the data layout for improved query performance.

1. Uploading Data to Amazon S3:

Amazon Simple Storage Service (Amazon S3) is a highly scalable and durable object storage service provided by AWS. Athena reads data directly from S3, so the first step is to upload your data files to an S3 bucket. Here’s how:

  • Create an S3 Bucket: If you don’t already have an S3 bucket, create one in your AWS account. I have created one with name “learncsdesign-movie”.
  • Upload Data Files: Use the AWS Management Console, AWS Command Line Interface (CLI), or SDKs to upload your data files (CSV, JSON, Parquet, etc.) to the bucket. For this tutorial I have uploaded JSON file which has movie data, each line has one JSON record. Sample JSON data is shown below:
{"title":"The Grudge","year":2020,"cast_members":["Andrea Riseborough","Demián Bichir","John Cho","Betty Gilpin","Lin Shaye","Jacki Weaver"],"genres":["Horror","Supernatural"],"href":"The_Grudge_(2020_film)","extract":"The Grudge is a 2020 American psychological supernatural horror film written and directed by Nicolas Pesce. Originally announced as a reboot of the 2004 American remake and the original 2002 Japanese horror film Ju-On: The Grudge, the film ended up taking place before and during the events of the 2004 film and its two direct sequels, and is the fourth installment in the American The Grudge film series. The film stars Andrea Riseborough, Demián Bichir, John Cho, Betty Gilpin, Lin Shaye, and Jacki Weaver, and follows a police officer who investigates several murders that are seemingly connected to a single house.","thumbnail":"","thumbnail_width":220,"thumbnail_height":326}
{"title":"Underwater","year":2020,"cast_members":["Kristen Stewart","Vincent Cassel","Jessica Henwick","John Gallagher Jr.","Mamoudou Athie","T.J. Miller"],"genres":["Action","Horror","Science Fiction"],"href":"Underwater_(film)","extract":"Underwater is a 2020 American science fiction action horror film directed by William Eubank. The film stars Kristen Stewart, Vincent Cassel, Jessica Henwick, John Gallagher Jr., Mamoudou Athie, and T.J. Miller.","thumbnail":"","thumbnail_width":250,"thumbnail_height":398}
{"title":"Like a Boss","year":2020,"cast_members":["Tiffany Haddish","Rose Byrne","Salma Hayek","Jennifer Coolidge","Billy Porter"],"genres":["Comedy"],"href":"Like_a_Boss_(film)","extract":"Like a Boss is a 2020 American comedy film directed by Miguel Arteta, written by Sam Pitman and Adam Cole-Kelly, and starring Tiffany Haddish, Rose Byrne, and Salma Hayek. The plot follows two friends who attempt to take back control of their cosmetics company from an industry titan.","thumbnail":"","thumbnail_width":259,"thumbnail_height":383}
  • Organize Data: Organize your data within the bucket by using logical folders and a consistent naming convention.

2. External vs. Managed Tables:

In Athena, you can define tables that reference the data in your S3 bucket. There are two main types of tables: external and managed.

  • External Tables: These tables are pointers to the data stored in S3. You define the schema of the table (column names, data types, etc.) and specify the S3 location of the data files. This is useful when you want to keep data in your control and use it with multiple services.
  • Managed Tables: Also known as “internal” tables, these tables have their data managed by Athena. When you create a managed table, Athena takes care of creating an S3 location to store the data and maintaining the data’s lifecycle. Managed tables are useful when you want a more hands-off approach to data management.

3. Partitioning Data for Performance:

Partitioning is a technique to improve query performance by dividing data into smaller, manageable chunks based on specific columns. This is particularly useful when dealing with large datasets. Here’s how partitioning works:

  • Column Choice: Choose a column that has discrete and frequently filtered values, such as dates, regions, or categories. This becomes the partition key.
  • Directory Structure: When data is partitioned, it’s stored in S3 directories that reflect the partition keys. For example, if you partition by date, data for each date will be stored in a separate S3 directory.
  • Improved Performance: Partitioning helps Athena skip over irrelevant data when querying, reducing the amount of data scanned. This leads to faster query performance and cost savings.

Creating Databases and Tables in AWS Athena

To create a database in AWS Athena:

  • Navigate to Athena Console: Go to the AWS Management Console, select Athena, and open the Athena Query Editor.
  • Run SQL Command: In the Query Editor, use the CREATE DATABASE statement followed by the database name. For example:
  • Execute Query: Click the “Run Query” button. The new database named “movie-db” will be created.

To define a table schema in AWS Athena:

  • Use the CREATE TABLE statement to define a new table.
  • Specify the table name and the columns along with their data types.
title STRING,
year INT,
cast_members ARRAY<STRING>,
href STRING,
extract STRING,
thumbnail STRING,
thumbnail_width INT,
thumbnail_height INT
'serialization.format' = '1'
LOCATION 's3://learncsdesign-movie/'

When defining tables in Athena, it’s important to choose the right data format based on your use case, data structure, and performance requirements. Additionally, partitioning your data can significantly improve query performance, especially for large datasets.

Supported Data Formats:

AWS Athena supports a variety of data formats for querying, including CSV, JSON, Parquet, ORC, and more. Each format has its own advantages in terms of query performance, storage efficiency, and compatibility with other tools.

  • CSV (Comma-Separated Values): A text-based format where each line represents a row and fields are separated by commas. Simple to use but less efficient compared to columnar formats.
  • JSON (JavaScript Object Notation): A format for structured data that’s widely used for semi-structured or nested data. Athena provides JSON functions to work with JSON data.
  • Parquet: A columnar storage format optimized for analytics. It offers compression and predicate pushdown, improving query performance and reducing data storage costs.
  • ORC (Optimized Row Columnar): Similar to Parquet, ORC is another columnar format designed for high performance and space efficiency. It’s well-suited for complex queries.
  • Avro: A compact binary format that supports schema evolution. It’s often used in data pipelines and when schema changes are expected.

Athena SQL Fundamentals

Athena SQL Fundamentals is a crucial section of this tutorial, as it introduces the core concepts and techniques for querying data stored in Amazon S3 using Athena. This section assumes you have a basic understanding of SQL, but it’s designed to accommodate those new to Athena and its querying capabilities.

1. Basic SQL Queries in Athena:

In Athena, you interact with your data using SQL queries, making it familiar to those with SQL experience. Basic queries start with the SELECT statement, which retrieves data from a specified table. For instance:

SELECT * FROM "movie-db"."movies" 

This query fetches all columns from the movies table.

2. SELECT, FROM, and WHERE Clauses:

  • SELECT specifies the columns you want to retrieve.
  • FROM indicates the source table.
  • WHERE is used to filter data based on conditions.
SELECT title, year FROM "movie-db"."movies" where year > 2020

This query selects specific columns from movies where the value in year is greater than 2020.

3. Aggregation Functions:

Aggregation functions perform calculations on a set of rows and return a single value. Common aggregation functions include COUNTSUMAVGMIN, and MAX. Example:

SELECT cast_member, COUNT(DISTINCT title) as movie_count
FROM movies
CROSS JOIN UNNEST(cast_members) AS t(cast_member)
GROUP BY cast_member
ORDER BY movie_count DESC;

In this query, we are counting the number of distinct movie titles that each cast member appeared in. The GROUP BY clause groups the results by cast member, and the COUNT aggregate function counts the number of distinct movie titles for each cast member. The results will be ordered in descending order based on the movie count.

AWS Athena Costing

When it comes to understanding the costing of AWS Athena, it’s important to consider several factors that contribute to the pricing structure. Athena’s pricing is based on the amount of data scanned during query execution, but there are additional nuances to consider. Let’s break down the details of AWS Athena costing:

1. Data Scanned: Athena charges you for the amount of data scanned during query execution. The more data your query needs to access and process, the higher the cost. Data is charged in gigabytes (GB). This means that if your query reads 5GB of data, you’ll be charged for 5GB of scanned data.

2. Query Complexity: Complex queries, such as those involving multiple joins, aggregations, or subqueries, can potentially scan more data than simpler queries. As a result, complex queries tend to incur higher costs.

3. Compression and Data Format: The way your data is stored and compressed in Amazon S3 can impact the amount of data scanned. Athena supports various compression and file formats, such as Parquet, ORC, JSON, and CSV. Using columnar storage formats like Parquet or ORC can significantly reduce the amount of data scanned, thus lowering costs.

4. Data Partitioning: If your data is organized into partitions, Athena can skip scanning irrelevant partitions based on query filters, reducing the amount of data scanned and lowering costs. Well-designed partitioning strategies can lead to more efficient queries and cost savings.

5. Result Data Size: Apart from the data scanned, the size of the result set also contributes to the cost. If your query returns a large amount of data, it might incur additional costs due to the data transfer from Athena to your output location.

6. Amazon S3 Data Transfer Costs: Data transfer costs may be incurred if you’re reading data from or writing results to Amazon S3 in a different region. This is important to consider if your data and Athena query results are stored in different regions.

7. Query Caching: Athena provides query result caching, which can reduce costs for recurring queries by reusing cached results instead of re-executing the query and scanning the data. However, not all queries are cacheable, and cache eviction policies should be considered.


To conclude, AWS Athena serves as a remarkable solution for leveraging SQL capabilities on vast datasets stored in Amazon S3.

This tutorial emphasized how Athena’s serverless architecture simplifies data analysis, allowing users to harness the power of familiar SQL queries to extract insights. From setting up databases and optimizing data formats to executing complex queries, Athena’s SQL-oriented approach empowers users to efficiently explore and derive value from their data without the hassle of managing infrastructure.

Leave a Reply

Your email address will not be published.