# Native SQL: SQL / Code mode and simple mode

When working with a datasource supporting [NativeSQL](https://docs-v3.toucantoco.com/data-management-in-datahub/datasets-in-toucan/preparing-data/youprep-tm-native-sql), you have two modes for writing your queries.

After connecting to your database, when you click on "create a dataset from datasource", you land on a new page where, on the right, you have three modals: *"Configuration", "*&#x51;*uery*"*,* and "R*eview*" which will lead you to the creation of a dataset.

## Query configuration mode

{% hint style="info" %}
If your workspace is fueled by our New Data Execution System, you have access to a fully redesigned form, refer to [this part](#new-query-configuration-form)
{% endhint %}

### Simple mode

In **Simple mode,** (which is the default mode for all connectors which support NativeSQL), you can query your database without the hassle of writing a SQL query, and look at your database model constantly to avoid errors.

After choosing the database you want to query and clicking on "*Validate configuration",* you switch to the "Q*uery"* tab

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-68d0c5202f203b60eea936f15c07279548178c37%2FPostgreSQL_edit_datasource_configuration.png?alt=media" alt=""><figcaption><p>Edit datasource - configuration</p></figcaption></figure>

you switch to the "Q*uery"* tab where you can select the table to query

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-dbe9d358174090690403a5bf61e7868577507ee2%2FPostgreSQL-edit_datasource_query_single_code_mode.png?alt=media" alt=""><figcaption><p>Edit datasource - Query - Table selection</p></figcaption></figure>

You can navigate through the database schema to choose the table you want to query.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-93382bd224cf51363923f7facc5424c55e2feacd%2FCapture%20d%E2%80%99e%CC%81cran%202024-02-26%20a%CC%80%2015.19.58.png?alt=media" alt=""><figcaption><p>Edit datasource - Query - Table selection</p></figcaption></figure>

Then you can choose the columns you want to keep in your dataset by clicking/un-clicking on the tick box.

You can choose to select all columns by clicking on "*Select all*" or unselect all columns by clicking on "*Clear All*".

You can click on the "*Preview*" button to display on the right the columns of your selection.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-54dfb3da6320f71509b2c087f36e865b1565e1fc%2FCapture%20d%E2%80%99e%CC%81cran%202024-02-26%20a%CC%80%2015.23.55.png?alt=media" alt=""><figcaption><p>Edit datasource - Query - Table selection -> Columns selection</p></figcaption></figure>

Once you are satisfied with your query, you can click on the "*Apply Data Selection*" button. You are redirected to the "*Review"* tab, where you can review the query you will send to your database.

On the right of each field, you have an *Edit* icon for each field which will redirect you to the designated section when clicked.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-cf26a939c712cd1a6815fa6782542f0871709ca0%2FCapture%20d%E2%80%99e%CC%81cran%202024-02-26%20a%CC%80%2015.30.21.png?alt=media" alt=""><figcaption><p>Edit datasource - Review</p></figcaption></figure>

By clicking on the "*Preview*" button, you can view the data.

By clicking on "*Save query"* you will be redirected to the dataset creation where you will be able to apply data transformation.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-9274ac7ded8f8bd0063b353fcb956a0df1dfa57a%2FCapture%20d%E2%80%99e%CC%81cran%202024-02-26%20a%CC%80%2016.44.02.png?alt=media" alt=""><figcaption><p>New dataset from source</p></figcaption></figure>

### Code/SQL mode

In the configure datasource interface, once you have chosen the database in the configuration tab, in the query mode, at the bottom right, there's an icon (burger menu). You can click on the message "*Switch to code mode*" that is displayed.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-3c1b954f7604d8d7a3ae8ee88dc32624275a2c95%2FPostgreSQL-edit_datasource_query-table-columns-select_switch_code_mode.png?alt=media" alt=""><figcaption><p>PostgreSQL - Edit datasource - Query -Columns selection - switch to code mode</p></figcaption></figure>

You can switch to code mode since table selection and after: And the current SQL request will be displayed. You can insert a variable using the '`/`' key or by clicking on `insert a variable` at the top right of the input box. Learn more about variables in the [dedicated section](https://docs-v3.toucantoco.com/data-management-in-datahub/managing-variables-in-toucan).

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-99b0b798a42cc2aa792b94b688cadf5d5b151893%2Fcode_editor_first.png?alt=media" alt=""><figcaption><p>Edit datasource - Query - Columns selection - code mode</p></figcaption></figure>

This field is a playground SQL where you can write the query you desire to get data from your database. You can erase the current query to write your own.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-84ca3a33cb0e87b83c2de299c1b08872b7bcf32d%2FCapture%20d%E2%80%99e%CC%81cran%202025-02-20%20a%CC%80%2014.38.01.png?alt=media" alt=""><figcaption><p>Edit datasource - Query -Columns selection - code mode</p></figcaption></figure>

## New query configuration form

{% hint style="info" %}
If the new [Data Execution System](https://docs-v3.toucantoco.com/data-management-in-datahub/new-data-execution-system) is activated on your workspace and your connector is nativeSQL compatible, A new query configuration form has been designed.

Check the [latest release note](https://docs-v3.toucantoco.com/additional-ressources/latest-releases) to see which data connectors are supported and nativeSQL compatibles.
{% endhint %}

### Simple mode

In *Simple Mode*, users can build queries without writing SQL manually (the query sent is in the following form `SELECT [selected columns] FROM [schema].[selected table] LIMIT 400`). This mode provides a guided interface to select databases, tables, and columns.

* **Workflow**:
  1. Select a **Database** from a list (or use the default variable if dynamic selection is enabled).
  2. Browse and choose a **Table**; all tables available in the selected database schemas are displayed.
  3. Select or deselect individual **Columns** that should appear in the query output.
  4. Execute the query against the selected database through the `Preview` button
  5. Or / then click on `Next` button to pass to the YouPrep

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-c95c927b8c2f85dbae8782c991eb05c0772bd886%2Ffirst_screen.png?alt=media" alt=""><figcaption></figcaption></figure>

**Selection through a Fixed Database**

* Users explicitly choose a database from a dropdown list.
* Once chosen, the relevant tables from that database are listed for selection.
* The query will always target this fixed database.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-be347401f96fcb8992edc1fa3d9643dc5e95b2df%2FCapture%20d%E2%80%99e%CC%81cran%202025-09-12%20a%CC%80%2015.03.50.png?alt=media" alt=""><figcaption></figcaption></figure>

**Selection through a Dynamic Database**

* A **variable** is used to define the target database.
* A default value for the variable is provided, so Simple Mode can still list the available tables and columns.
* The actual query execution adapts based on the variable's runtime value.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-1d77dcc6ae4ffd39868e091852a7003a19a5cc53%2FCapture%20d%E2%80%99e%CC%81cran%202025-09-15%20a%CC%80%2013.45.04.png?alt=media" alt=""><figcaption></figcaption></figure>

### Code/SQL Mode

**Workflow**:

1. Select a **Database** from a dropdown list (`Fixed database`) or via a variable(`Dynamic database`).
2. Use the **SQL editor input box**, where you can enter multiline queries.
3. Execute the query against the selected database through a preview button
4. Then click on `Preview` button to have a preview of your query, the query is sent with `LIMIT 400` for performance purposes
5. Then/or click on `Next` button to pass to the YouPrep

**Fixed Database**

* The user chooses a database explicitly from a fixed list.
* SQL queries written in the editor will always target this database.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-8f77dfaa0ea807ce3d9e7bf5bc1e0ad1b1297603%2Fcode_mode_1.png?alt=media" alt=""><figcaption></figcaption></figure>

**Dynamic Database**

* The database is chosen through a **variable**.
* The editor displays the tables and autocompletion based on the variable’s default database, but queries execute against whichever database is resolved at runtime.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-757d40dadb2e33cd2dbf28d048fd2926dcc92817%2FCapture%20d%E2%80%99e%CC%81cran%202025-09-15%20a%CC%80%2014.47.24.png?alt=media" alt=""><figcaption><p>Code/SQL mode: with dynamic database</p></figcaption></figure>

***

#### Explore Database

The **Database Explorer** provides navigation and exploration tools for users to better understand their database structure and build queries more efficiently.

* Users can browse multiple schemas and tables within the selected database.
* The explorer helps in quickly referencing table names, column structures, and relationships.
* This feature is especially useful in Code/SQL Mode to assist in building complex queries manually.

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-94d0f6719db1534f879925d00877293f6f09bd31%2Fcode_mode_explore_db_one_table.png?alt=media" alt=""><figcaption><p>code SQL mode with one table selected</p></figcaption></figure>

<figure><img src="https://1809014303-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZxYYf1KpgarKMgMsDCrw%2Fuploads%2Fgit-blob-88c973dece594effd30288bf64932813b5c3ffce%2Fcode_mode_explore_db_twotables.png?alt=media" alt=""><figcaption><p>code SQL mode with multiple tables selected</p></figcaption></figure>
