> For the complete documentation index, see [llms.txt](https://upsolver.gitbook.io/content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://upsolver.gitbook.io/content/reference-1/sql-commands/jobs/create-job/transformation/insert/map_columns_by_name.md).

# MAP\_COLUMNS\_BY\_NAME

Maps columns from the `SELECT` statement to the table by the names of the returned columns in the query.

If set, all columns in the query must either have an alias, be a simple column reference, or be a star expression.

Note that this is required when using a star query in the `SELECT` statement.&#x20;

## Examples

A basic `INSERT` statement may look as follows:

```sql
INSERT INTO target_table (col1, col2, col3)
    SELECT a, b, c ...
```

In this case, mapping the fields `a, b, c` to the column names `col1, col2, col3` is based on their ordinal positions.

However, since Upsolver supports tables with dynamic columns, you may be unable to provide a full list of columns when mapping to a dynamic list of columns. As such, there would be no way to map columns based on ordinality.

In such a case, the `MAP_COLUMNS_BY_NAME` keyword should be used with the resulting SQL looking as follows:

```sql
INSERT INTO target_table MAP_COLUMNS_BY_NAME
    SELECT a AS col1, 
           b AS col2, 
           c AS col3 ...
```

When mapping a column directly as seen below with `col3`:

```sql
INSERT INTO target_table MAP_COLUMNS_BY_NAME
    SELECT a AS col1, 
           b AS col2, 
           col3 ...
```

Upsolver assumes the intended column name to be `col3` and maps it accordingly.

In the case that the column name cannot be inferred, the job will fail to run.

For example, if you have a calculated field:

```sql
INSERT INTO target_table MAP_COLUMNS_BY_NAME
    SELECT col + 'a' ...
```

Typically, SQL would name `col + 'a'` as `col1` by default; in SQLake, however, this is not allowed.&#x20;

To ensure columns are mapped correctly when using `MAP_COLUMNS_BY_NAME`, a column name must be provided in the query if it cannot be directly inferred as shown below:

```sql
INSERT INTO target_table MAP_COLUMNS_BY_NAME
    SELECT col + 'a' AS col1 ...
```

This also applies when you have a calculated field from using a function:

```sql
INSERT INTO target_table MAP_COLUMNS_BY_NAME
    SELECT ABS(col) AS col1 ...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://upsolver.gitbook.io/content/reference-1/sql-commands/jobs/create-job/transformation/insert/map_columns_by_name.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
