> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Querying Data in S3 with Amazon Athena

> Hands-on lab · 45m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/querying-data-in-s3-with-amazon-athena" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab details

This lab allows users to query and analyse data stored in an S3 bucket using SQL statements. The lab aims to help users understand the process of creating a table in Amazon Athena, configuring the necessary settings, and executing SQL queries.
You will practice using Amazon Athena to create a table with a CSV file present in the S3 bucket and query using SQL statements.
Duration: **1 hour**
AWS Region: **us-east-1 (N. Virginia).**

### Introduction

#### What is Amazon Athena?

Amazon Athena is a serverless query service offered by Amazon Web Services (AWS), designed for the analysis of data stored in Amazon S3 using standard SQL queries. The underlying technology of Athena is Presto, an open-source distributed SQL query engine. Supporting various data formats such as CSV, ORC, JSON, Avro, and Apache Parquet, Athena enables users to access it through the AWS Management Console, ODBC/JDBC drivers, or API.

Operated on a pay-per-query model, Athena ensures that users are billed only for the executed queries. The service facilitates data analysis through an interactive query editor within the AWS Management Console, allowing users to create tables, define schemas, and execute SQL statements for data exploration and querying.

Athena further integrates with other AWS services, such as AWS Glue, for metadata management and data cataloguing. Its notable features include quick query execution and automatic scaling based on the volume and complexity of the data being analysed. This comprehensive functionality makes Amazon Athena a versatile and efficient tool for data analysis within the AWS ecosystem.

### Architecture diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/001.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=8fd16688b3612ca3f7c01653b82a7cbe" alt="" width="1100" height="640" data-path="images/labs/querying-data-in-s3-with-amazon-athena/001.png" />

### Task details

1. Sign in to AWS Management Console.
2. Setup workgroup
3. Create a database in Glue.
4. Query table in Athena
5. Validation of the lab
6. Deleting AWS Resources

### Launching the lab environment

1. To launch the lab environment, Click on the Launch lab button.
2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
3. Once the Lab is started, you will be provided with IAM username, Password, Access Key, and Secret Access Key.

<Note>
  You can only start one lab at any given time
</Note>

## Lab guide

### Lab steps

#### Task 1: Sign in to AWS Management Console

1. Click on the **Open console** button, and you will get redirected to AWS Console in a new browser tab.
2. On the AWS sign-in page,

* Leave the Account ID as default. Never edit/remove the 12-digit Account ID present in the AWS Console. Otherwise, you cannot proceed with the lab.
* Now copy your **Username** and **Password** in the Lab Console to the **IAM Username and Password** in AWS Console and click on the **Sign-in** button

3. Once Signed In to the AWS Management Console, make the default AWS Region as **US East (N. Virginia) us-east-1.**

#### Task 2: Setup workgroup

In this task, we are going to create a workgroup in Amazon Athena. The workgroup allows users to define specific configurations and settings for their query execution environment. By creating a workgroup, users can customize parameters such as query result location, encryption settings, and query execution options.

1. Make sure you are in the **N.Virginia** Region.
2. Navigate to the **Services** menu at the top, then click on **Athena** in the **Analytics** section.
3. In the left sidebar, under **Administration** select **Workgroup** and then click on **Create Workgroup** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/002.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=886e08122d84a955bd3039c9d01267a5" alt="" width="2342" height="536" data-path="images/labs/querying-data-in-s3-with-amazon-athena/002.png" />
4. Provide details to create a workgroup:

   * Workgroup Name: Enter **WhizWorkgroup**
   * Description: Enter **Workgroup for Athena lab**

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/003.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=5788bd9668b0936ed19e03232b2713a7" alt="" width="2494" height="596" data-path="images/labs/querying-data-in-s3-with-amazon-athena/003.png" />

* Analytics engine:

  * Choose the type of engine : Select **Athena SQL**
  * Upgrade Query engine: Select **Automatic**

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/004.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=cb950531c5b11afdb4b61f17ae3404cd" alt="" width="2776" height="690" data-path="images/labs/querying-data-in-s3-with-amazon-athena/004.png" />
* Under **Query result configuration:** Select **Customer Managed**. Then Click on **Browse S3** button to select the S3 bucket, whose name is starting with **whizlabs... .**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/005.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=52aeadb1d38ae544daab9764e7a3148e" alt="" width="2796" height="840" data-path="images/labs/querying-data-in-s3-with-amazon-athena/005.png" />
* Leave other settings as default
* Click on the **Create Workgroup** button.

5. Workgroup has been created successfully.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/006.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=9f3847232344e2630d80baa0dc511d29" alt="" width="2294" height="1126" data-path="images/labs/querying-data-in-s3-with-amazon-athena/006.png" />

#### Task 3: Create a database in Glue

In this task, we are going to create a database in AWS Glue, a fully managed extract, transform, and load (ETL) service. The database serves as a container for organizing and storing metadata related to the data tables used in Athena. It enables efficient data cataloging and data management for the subsequent tasks.

1. Make sure you are in the **N.Virginia** Region.

2. Navigate to the **Services** menu at the top, then click on **AWS Glue** in the **Analytics** section.

3. In the left sidebar, Under Data catalog, Click on **Databases**

4. Click on the **Add Database** button

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/007.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=4d4135fb1a96caae443afec39c3cf2d5" alt="" width="2368" height="620" data-path="images/labs/querying-data-in-s3-with-amazon-athena/007.png" />

5. In the pop-up menu, enter the database name as **whizgluedatabase** and click on the **Create database** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/008.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=9dd08bccce87855256c6fe9a6464cf34" alt="" width="1222" height="585" data-path="images/labs/querying-data-in-s3-with-amazon-athena/008.png" />

6. The database is now created.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/009.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=10906b199691b9d58e3a48e4813203de" alt="" width="1222" height="184" data-path="images/labs/querying-data-in-s3-with-amazon-athena/009.png" />

#### Task 4: Create a table in Glue

In this task, we are going to create a table in AWS Glue, which represents the structure and schema of the data stored in the S3 bucket.

1. In the left sidebar, Under the **Data catalog**, Click on **Tables.**

2. To create a table, click on the **Add table** button

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/010.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=0635263372a020227129a7d050544cff" alt="" width="1222" height="314" data-path="images/labs/querying-data-in-s3-with-amazon-athena/010.png" />

3. In the **Set table properties** section, do the following:

   * Enter the **Table name** : **whiz-sample-table**
   * **Database:** select **whizgluedatabase**

4. In the **Data store** section, do the following:

   * Select the type of source: **S3 (default)**
   * Data location is specified in: **my account (default)**
   * Include path: Select the S3 bucket name starting with **whizlabs… .** Make sure you **add** / in the end.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/011.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=3babfe843c9bf46d530815b80b4b0fe8" alt="" width="1222" height="380" data-path="images/labs/querying-data-in-s3-with-amazon-athena/011.png" />

5. In the **Choose a data format** section, do the following:

   * Select Classification as **CSV**
   * Choose delimiter as **Comma (,)**
   * Click on the **Next** button.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/012.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=9eaf93d909226e4c9cc9ab9102c4d590" alt="" width="1222" height="318" data-path="images/labs/querying-data-in-s3-with-amazon-athena/012.png" />

6. In the Schema section, we will add two columns by clicking on **Add** button.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/013.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=6be33f2aa858905811deb8951130ef6b" alt="" width="1222" height="316" data-path="images/labs/querying-data-in-s3-with-amazon-athena/013.png" />

   * Column # : **1**
   * Column name: Enter **Expense\_Type**
   * Data Type: Select **string**
   * Click on the **Save button** below.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/014.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=3e0a4fe96f44876286a296da6c7c8d15" alt="" width="1222" height="512" data-path="images/labs/querying-data-in-s3-with-amazon-athena/014.png" />
   * Click on the **Add** button again.
   * Column #: **2**
   * Column name: Enter **Expense\_Category**
   * Data Type: Select **string**
   * Click on the **Save button** below.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/015.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=73a3ff9c4a339c9b36dd8e58c0e847e9" alt="" width="1222" height="511" data-path="images/labs/querying-data-in-s3-with-amazon-athena/015.png" />
   * After adding both columns, click on the **Next** button.

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/016.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=b9c4422b7c4a3ad7e4a420556ef4dc2f" alt="" width="1222" height="203" data-path="images/labs/querying-data-in-s3-with-amazon-athena/016.png" />

7. Review the configuration of the table and click on the **Create** button. The table is now created.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/017.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=7399188d76332ab0eeb5dff840a4da41" alt="" width="1222" height="423" data-path="images/labs/querying-data-in-s3-with-amazon-athena/017.png" />

#### Task 5: Query table in Athena

In this task, we are going to use the SQL query capabilities of Amazon Athena to interact with and analyze the data stored in the created table. By executing SQL statements in the Athena Query Editor.

1. Make sure you are in the **N.Virginia** Region.
2. Navigate to **the Services** menu in the top, then click on **Athena** in the **Analytics** section.
3. In the left sidebar, Click on **Query Editor**
4. Switch to **WhizWorkgroup** at the top right.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/018.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=b10b74a9b90d8f8f01f435c38268b18d" alt="" width="1222" height="495" data-path="images/labs/querying-data-in-s3-with-amazon-athena/018.png" />
5. In the left sidebar, under data source, select the **database** as **whizgluedatabase.**
6. Then you will see our table, **whiz-sample-table**.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/019.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=51015c9b60cd2595fe309f0f6d886b9b" alt="" width="722" height="948" data-path="images/labs/querying-data-in-s3-with-amazon-athena/019.png" />
7. To **preview** click on

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/020.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=0a476c34f1d7aa6a4c6db5435aae1c8a" alt="" width="9" height="19" data-path="images/labs/querying-data-in-s3-with-amazon-athena/020.png" />

   **three dots** next to the **whiz-sample-table** table name and select the **Preview Table.**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/021.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=ef2d9a964a852fed7b98a59cf65e1727" alt="" width="1044" height="774" data-path="images/labs/querying-data-in-s3-with-amazon-athena/021.png" />
8. The query editor will automatically generate the SQL statement for querying the first ten columns.
9. The result of the query is shown below.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/022.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=f95db93add4daeebe978294a9d19e364" alt="" width="1222" height="618" data-path="images/labs/querying-data-in-s3-with-amazon-athena/022.png" />
10. To get the results of all expenses types under **expense\_category** of **Food**, paste the following SQL statement into the same **query editor** or open new query editor by clicking on **+ symbol** and click on **Run** button.

    ```
    SELECT * FROM "whizgluedatabase"."whiz-sample-table" WHERE expense_category = 'Food';
    ```

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/023.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=040218b764b426363e0ac5eb069fb303" alt="" width="1222" height="451" data-path="images/labs/querying-data-in-s3-with-amazon-athena/023.png" />

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/024.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=01e85245a8bb9c9bcfe7d4fa47ee1950" alt="" width="1222" height="658" data-path="images/labs/querying-data-in-s3-with-amazon-athena/024.png" />

11. You can play around with different queries like the following one.

12. To run the new query you can **replace** the **existing one**, or you can add a **new query** by clicking on the **+ symbol** from the query editor.

13. Getting the total number of rows present by running the following SQL statement in the query editor.

    ```
    SELECT * FROM "whizgluedatabase"."whiz-sample-table" WHERE expense_category = 'Transportation';
    ```

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/025.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=2e80692d79cc5dfefee0fba61353861d" alt="" width="1222" height="460" data-path="images/labs/querying-data-in-s3-with-amazon-athena/025.png" />

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/026.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=fe2dec5b91321a72ca9b431d5a9c840f" alt="" width="1222" height="664" data-path="images/labs/querying-data-in-s3-with-amazon-athena/026.png" />

<Tip>
  **Do you know?**

  Athena's serverless nature allows users to analyze data stored in S3 without the need for any infrastructure provisioning or management. This means that users can focus solely on querying and analyzing their data without worrying about setting up and maintaining servers or clusters. The pay-per-query pricing model of Amazon Athena ensures cost efficiency, as users are only charged for the actual queries they run. This flexibility and cost-effectiveness make Amazon Athena a powerful and convenient tool for data analysis tasks in AWS.
</Tip>

#### Task 6: Validation Test

1. Once the lab steps are completed, please click on the **Check my work** button on the left side panel.
2. This will validate the resources in the AWS account and display whether you have completed this lab successfully or not.
3. Sample output :

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/querying-data-in-s3-with-amazon-athena/027.gif?s=1d770f0a0600feb5789903f34447294a" alt="" width="800" height="397" data-path="images/labs/querying-data-in-s3-with-amazon-athena/027.gif" />

#### Task 7: Delete AWS Resources

##### Deleting the database and table in Glue

1. Navigate to the **Services** menu at the top, then click on **AWS Glue** in the **Analytics** section.
2. By default, you will be able to see the tables present. In the left sidebar, Under **Data catalog**, Click on **Databases**.
3. Databases will be present here, our database is whizgluedatabase.
4. To delete the database, perform the following tasks:

   * Select the database, **whizgluedatabase**
   * Choose **Delete** from the menu.
5. Confirm the deletion on the pop-up by clicking on the **Delete** button
6. The database is successfully deleted now.

##### Deleting workgroup

1. Navigate to **Services** menu in the top, then click on **Athena** in the **Analytics** section.
2. **Click** on the workgroup.
3. Select **WhizWorkgroup** and Click on **Actions**
4. Click on **Delete**
5. Confirm the deletion on the pop-up by entering workgroup name i.e., **WhizWorkgroup** and click on the **Delete** button

### Completion and conclusion

1. You have successfully created and configured a workgroup in Amazon Athena.
2. You have successfully created a table in Amazon Athena with S3
3. You have successfully queried the SQL statements in the Query editor of Athena.
4. You have successfully created a webpage and published it.

### End lab

1. Sign out of AWS Account.
2. You have successfully completed the lab.
3. Once you have completed the steps, click on **End lab** in the IP Lab Portal.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Create a Glue Database** — Check whether a Glue Database is created or not.
* **Create a Glue Table** — Check whether an AWS Glue Table exists in any database or not
* **Create an Amazon Athena Workgroup** — Check whether an Amazon Athena Workgroup is created or not
