# How to get column names while pulling data from Singlestore into a CSV file?

**URL:** <https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027>\
**Category:** Help\
**Created:** [December 9, 2024, 6:56pm UTC](https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027 "2024-12-09T18:56:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![inshub5](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/i/b4bc9f/32.png) [@inshub5](https://www.singlestore.com/forum/u/inshub5)\
**Post date:** [December 9, 2024, 6:56pm UTC](https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027/1 "2024-12-09T18:56:04Z")

</div>

While pulling data from SingleStore to S3 using SELECT INTO S3, columns names are not included in CSV output files. What is the best way to include column names?

---

<div class="post-metadata">

**Author:** ![hanson1](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson1/32/2092_2.png) [@hanson1](https://www.singlestore.com/forum/u/hanson1)\
**Post date:** [December 12, 2024, 7:26pm UTC](https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027/2 "2024-12-12T19:26:53Z")

</div>

There’s no direct way to do this so you’d have to use some other application logic to deal with it. If absolutely must get column names into the output file and you want to use SELECT INTO S3 then you can use a UNION ALL in your query to UNION a one-row result set with the headers with the rest of your data. Like this:

```auto
create table t(a int, b varchar(30));

insert t values(1, "red"),(2, "blue");

select * from (
  select "a" , "b"
  union all
  select a, b
  from t
);

Output:

+------+------+
| a | b |
+------+------+
| a | b |
| 2 | blue |
| 1 | red |
+------+------+

```

You can then modify a query like this to add on SELECT INTO S3 syntax.

---

<div class="post-metadata">

**Author:** ![inshub5](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/i/b4bc9f/32.png) [@inshub5](https://www.singlestore.com/forum/u/inshub5)\
**Post date:** [December 16, 2024, 1:27pm UTC](https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027/3 "2024-12-16T13:27:20Z")

</div>

Running above query from Python using singlestoredb connector, column names are coming as part of only one csv not all in the output. I have multiple files getting created like .csv, .csv\_0, .csv\_1 and so on.

---

<div class="post-metadata">

**Author:** ![srachamadugu](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/srachamadugu/32/2173_2.png) [@srachamadugu](https://www.singlestore.com/forum/u/srachamadugu)\
**Post date:** [December 17, 2024, 1:48pm UTC](https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027/4 "2024-12-17T13:48:01Z")

</div>

To output to a single CSV file, you need to use aggregations, ORDER BY, or GROUP BY in your SELECT statement. When these are used, the query will run on each leaf but the result will be collected on the aggregator and output as a single file.[1](https://docs.singlestore.com/cloud/reference/sql-reference/data-manipulation-language-dml/select/#select-into-s-3)[2](https://docs.singlestore.com/db/v8.9/reference/sql-reference/data-manipulation-language-dml/select/#select-into-s-3)

For example, if you add ORDER BY to your query, it will output to a single file named `<bucket/target>` instead of separate partition files named `<bucket/target>_<partition ID>`

---

<div class="post-metadata">

**Author:** ![SawyerFinn](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@SawyerFinn](https://www.singlestore.com/forum/u/SawyerFinn)\
**Post date:** [January 23, 2025, 5:04pm UTC](https://www.singlestore.com/forum/t/how-to-get-column-names-while-pulling-data-from-singlestore-into-a-csv-file/8027/5 "2025-01-23T17:04:02Z")

</div>

Thank you so much, you saved my day.
