Query S3 with SQL using S3 Select
Tags: programming, tutorial, aws, sql
Published: 2022-03-17 18:47:30

S3 Select is an [AWS S3 feature](https://aws.amazon.com/about-aws/whats-new/2018/04/amazon-s3-select-is-now-generally-available/) that allows developers to run SQL queries on objects in S3 buckets. Here's an example.

```
SELECT s.zipcode, s.id FROM s3object s where s.name = 'Harshil'
```

Previously we wrote about the different ways you can write SQL with AWS. In this article, we will see how to configure and use S3 Select to make working with big datasets easier.

{% link https://dev.to/arctype/10-aws-services-that-use-sql-55nf %}

## What is AWS S3 Select?

Amazon Simple Storage Solution (S3) is a limitless, durable, elastic, and cost-effective storage solution. However, applications using S3 often need to obtain a subset of a huge dataset, and processing the complete object each time to acquire a subset of the object is impossible. Because this influences the application's speed, Amazon S3 has created called S3 Select.

In other words, with S3, you no longer need to download, extract, process, and then get the output. S3 Select also supports [various file types](https://docs.aws.amazon.com/AmazonS3/latest/API/API_SelectObjectContent.html), including GZIP or BZIP2 compressed objects and server-side encrypted objects.

![](https://lh3.googleusercontent.com/0W9gXVSLoRZs_YpA8pdGIq53tclDNapFfPewbuHMv-IuOpphEsMXUzlGFq0ulCkg0WZd2a5-ZssgxwXC400zleQMGVe0NQBp8xsD1-8IbnWJEVsQnRBa5FpjcJaiHBKcpATJmBFz)

### Advantages of S3 Select

S3 Select has a few benefits that make it a [potential game-changer](https://blog.clairvoyantsoft.com/accelerate-s3-data-querying-performance-with-s3-select-ff84dde1b8da). First, since S3 Select functionality is available as an API, it does not require additional infrastructure or management. You can also easily integrate with other AWS tools and services like Lambda and EMR.

Furthermore, S3 Select can also increase the speed of most programs that frequently access data from S3 by up to 400% by minimizing the data that must be loaded and processed by your apps.

The amount of [file types supported](https://docs.aws.amazon.com/AmazonS3/latest/userguide/selecting-content-from-objects.html) is another plus. CSV, GZIP, BZIP2, JSON, and Parquet files are all perfectly fine. It also supports GZIP or BZIP2 compressed objects and server-side encrypted objects.

Lastly, S3 queries are cost-effective. This comes naturally - the fewer results you return, the less you spend.

### Limitation of S3 Select

Like any tool, though, S3 Select is not perfect. One limitation is that an SQL expression can have a maximum length of 256 KB. Additionally, the maximum length of a record in the input or result is 1 MB.

Furthermore, complex analytical queries and joins are not supported. And lastly, the select query can only run on one file at a time.

### How much does it cost?

AWS provides a cost calculator, which we can use to estimate how much we'd need to spend on Select as compared to some other S3 offerings. Here is an estimated cost structure, which you can see in more detail [here](https://calculator.aws/#/estimate).

- S3 object storage — $0.02/GB
- S3 Select — $0.0004 per 1000 SELECT commands
- S3 Data Transfer— $0.01 per GB (U.S. East Region)
- Data Returned by S3 Select — $0.0007/GB
- Data Scanned by S3 Select — $0.002/GB

## How to use S3 Select to perform a query

Ready to put what we've discussed into practice? Let's see how to use S3 Select to perform a query from the AWS console.

First, go to your S3 dashboard (search S3 in the AWS console). Here, you can choose to make a new bucket or use one that already exists. After you've created or selected your bucket, you'll need to **upload** the file you wish to query.

![Screenshot of AWS](https://lh6.googleusercontent.com/7g8Kue-6n2aNHF2jxG2m9cvdl0t0N-Zenx1Rx4erskUEY6IM6Zdl7wiEFBwujfl09ZvYAY7USjOSqvmsTpome6JhrGdTjvBywMej7hG3LAg4AniHhcFIEsOLGPvnf9N3513jnhTH)

Once the upload is complete, you will see a **success** message.

![](https://lh6.googleusercontent.com/nOaPWh1HWsMTd9lV8pUPjXUg4YBcAZyiYDcovNHj_PWVvOA7IZP2H8TPDTWj0JLqo5gC34Y9SAILbJHK4OBe-e-DmAyA-5IMpmW9G38ZhsiyOMkaQvlgHcm1Ff2UxzLhKHd5xjgU)

Now, go to **Actions** and choose **Query with S3 Select** after selecting the file you want to query.

![](https://lh5.googleusercontent.com/9wp2FSifZS9bR4nmA14p3pIWwPO9KJ27o5Lsqq_Na4f_j4zGzWZ_cm2wSnOWMk55Q8-fqV8AplrNLBbsqrb-LIaSx_hjCcdObWslIGyBrK08LbBE2M52p_QMa-XkXEohy5ZC3TbD)

Now, select the appropriate input and output setting depending on your file.

Note: If the first row of your file contains header data, select "Exclude the first line of CSV data."

![](https://lh6.googleusercontent.com/UGBPcRfD67PVvy4qc_K78TaHPnQnJgxDvPL0qugwHPZ4vk_I7_WV4tA9bEgU72Bjja2Q942vkYXwWQ_cpwnNVNFOY9siuvVd-c6JkOtfB1hYNZZspa0r9i3pgsd3EQYsF6WiQr72)

**Step 6:** It's time to write queries now that you've defined all of your parameters. The picture below shows where to write a question and how the results will be shown.

![](https://lh4.googleusercontent.com/RjxRcbaoK8cJHfuBHGTX6Ls_od4mHohx9Sy3XjB4aPHx6uFc4Rcfnt0oY70RWapD6psXRj6n_URgBXnPvqrFK_A_iHGNItXTgao4t-0uKki0MbworXSZ80EdvqPwkllD1DrOVS6D)

The best part about S3 is that you can query your files using predefined templates. You can also save the results as CSV or JSON files.

![](https://lh5.googleusercontent.com/2IG2XVP3qf3BI8-hAhjbxZ1uI9IDeCdNDZmmmOZrKS-pINla4kDJgChGAwA9pf_en2EmN6sCRAuu44-TSz1yV5-m_QehlK5OczmAEjqPXr9oE4GOPvqakwLk-_I-WNov4G5DV8aT)

When you're running queries, wouldn't it be easier to run and visualize all of your queries at one place? When working with a team, you might want a platform that allows you to manage analytics, display data, and collaborate. With [Arctype](https://arctype.com/), you can manage your databases and visualize your data. Workspaces can easily be shared, and its comprehensive access management makes inviting people and managing access rights a breeze. Now, let's run some queries on S3.

### Let's run some queries.

For example, we will be using a CSV file with *name, id, and zip code* field. To start off, let's formulate a query to get the **name** and **ID** field from our CSV file:

```
SELECT s.name, s.id FROM s3object s
```

This returns the result below.

![](https://lh3.googleusercontent.com/BKN1hBocvLOKaZhOp3mKmoK-wtuxbsjgqgeBZLh4LRdPgL_8WV-S_ToIfrZ4tg5ZErkMQNvLeG4Ep2Pe89IZrGTj8Ijw7N6RTBWCzQy32s67zjcHjEWmm5Erl83sESocgrBcjDBn)

To take it a step further, we can also write a query to return the **ID** and **Zipcode** where **Name** is 'Harshil.'

```
SELECT s.zipcode, s.id FROM s3object s where s.name = 'Harshil'
```

![](https://lh6.googleusercontent.com/Vfw2IeGub3Y4oSrY-OSYKIMXWnX1lRoLFrubgiU8sXFn4rfidHHHe4FdNRgxoFhJ2v_63zwtUwdNbymDsy5d9rMMTGn7tGbTDSh48jq63GEFOSUbAleacblRXQBH7Cr2Kxb2J8PE)

I wonder how many people have the same zip code. We can answer that question with the following query:

```
SELECT * FROM s3object s WHERE CAST(s.zipcode as INTEGER) = 11311
```

![](https://lh5.googleusercontent.com/FRuAP4YKaaKsJyQjAaTZc7wHmNTnV33Ak-DB4IVvQosQ-3uaKI6Tbai_9Z7qF8DmR9J9oT-BSrSIsYmk3SJ3gCzG3gdvGPvh0So0NzyTwKeBYI9iot9r0R91d2v7O4j36SJjU30R)

 We can also use a simple count query to count the items in our file.

```
SELECT count(*) FROM s3object s
```

![](https://lh4.googleusercontent.com/vcubyqr2RSLr8OBka206iem9jSSiLKsTXn7NDNzoYgVg812ZI3b_TJhL26bfqPk_X-DE3dP_IjW1x0XaLwGo5IN0xMquEY8fxduqaMfJuk5C75b2p36_RsfMIrRXNab4VykC6laa)

### S3 Select with Python and AWS SDK

Let's say you have a big file in S3, you're building an application with [Python](https://arctype.com/postgres/drivers/python-postgres), and you seek (no pun intended) to filter the file based on specific criteria. So you want to conduct some advanced querying and choose particular rows or columns. In that case, you can use S3 Select and [Boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html).

The AWS SDK for Python (Boto3) provides a Python API for AWS infrastructure services. Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more. You can simply import the necessary requirements using the code below.

```
import boto3
import pandas as pd

client = boto3.client('s3')
```

An example response is also shown:

```
response = client.select_object_content(
    Bucket='string',
    Key='string',
    SSECustomerAlgorithm='string',
    SSECustomerKey='string',
    Expression='string',
    ExpressionType='SQL',
    RequestProgress={
        'Enabled': True|False
    },
    InputSerialization={
        'CSV': {
            'FileHeaderInfo': 'USE'|'IGNORE'|'NONE',
            'Comments': 'string',
            'QuoteEscapeCharacter': 'string',
            'RecordDelimiter': 'string',
            'FieldDelimiter': 'string',
            'QuoteCharacter': 'string',
            'AllowQuotedRecordDelimiter': True|False
        },
        'CompressionType': 'NONE'|'GZIP'|'BZIP2',
        'JSON': {
            'Type': 'DOCUMENT'|'LINES'
        },
        'Parquet': {}

    },
    OutputSerialization={
        'CSV': {
            'QuoteFields': 'ALWAYS'|'ASNEEDED',
            'QuoteEscapeCharacter': 'string',
            'RecordDelimiter': 'string',
            'FieldDelimiter': 'string',
            'QuoteCharacter': 'string'
        },
        'JSON': {
            'RecordDelimiter': 'string'
        }
    }
)
```

## Conclusion

With S3 Select, you can use basic SQL queries to speed up S3 data querying efficiency. S3 Select may also be integrated with other AWS services to increase performance and lower costs. In simple terms, S3 Select will improve your data querying performance.