> For the complete documentation index, see [llms.txt](https://datalab-docs.datacamp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://datalab-docs.datacamp.com/work/sql-cell/sql-scenarios.md).

# SQL scenarios

The different types of data sources to query and the different modes of querying enable powerful workflows that are useful in different combinations. Learn more about different ways of using the SQL cell in [SQL scenarios](/work/sql-cell/sql-scenarios.md).

#### Query a database with SQL and return a data frame, continue in Python

Create a SQL cell to query a database with a SQL cell in DataFrame mode. Next, create a Python cell and write Python code to analyze the query, e.g. to create a visualization.

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2FO2K8xTL2t7fUGfDlWvKG%2Fimage.png?alt=media&amp;token=589a3582-0ee2-46a1-a041-a7e187b0fde8" alt=""><figcaption><p>Query a database with SQL, continue in Python</p></figcaption></figure>

This approach works if you prefer Python over SQL and the SQL query you're executing doesn't return a huge result (there is a query limit of 100 MB for free users and 500 MB for Premium users).

#### Query a database with SQL and return a data frame, continue in SQL

Create a SQL cell to query a database with a SQL cell in DataFrame mode. Next, create another SQL cell, select "DataFrames and CSVs" as a source and write SQL code to query the data frame created by the first SQL cell.

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2FnSfiQT8AGLntAggYU7Ok%2Fimage.png?alt=media&amp;token=7a0dfae1-673f-46f8-8b3b-0e8b77a482ad" alt=""><figcaption><p>Query a database with SQL, continue analyzing the dataframe with SQL</p></figcaption></figure>

This approach works if you prefer SQL over Python and the first SQL query you're executing doesn't return a huge result (there is a query limit of 100 MB for free users and 500 MB for paying users). The second SQL query will execute right inside DataLab using [DuckDB](https://duckdb.org/). The SQL syntax is similar to PostgreSQL; check the [DuckDB docs](https://duckdb.org/docs/sql/functions/overview) for a full overview.

#### Query a CSV file or Excel file with SQL

Make sure the CSV file or Excel file you want to query is in your workbook file system by uploading it through the file browser. Next, create a SQL cell, select "DataFrames and CSVs" in the first dropdown and write SQL code to query the data file. The SQL query will execute right inside DataLab using [DuckDB](https://duckdb.org/). The SQL syntax is similar to PostgreSQL; check the [DuckDB docs](https://duckdb.org/docs/sql/functions/overview) ([CSV](https://duckdb.org/docs/stable/guides/file_formats/csv_import), [Excel](https://duckdb.org/docs/stable/guides/file_formats/csv_import)) for a full overview. Parquet and JSON files are also supported ([Docs](https://duckdb.org/docs/stable/guides/file_formats/overview)).

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2Fos7Z8wAxWTZ4YmYzhSaP%2Fimage.png?alt=media&amp;token=fb630eb8-632e-4600-b72c-b3d60b0516c0" alt=""><figcaption><p>Query a CSV file with SQL</p></figcaption></figure>

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2FVNoz4TXICAU3rspMYvdz%2FScreenshot%202025-04-23%20at%2017.44.30.png?alt=media&amp;token=720d2312-3fb6-4274-8e47-33ac1375bd70" alt=""><figcaption><p>Query an Excel file with SQL</p></figcaption></figure>

Use this approach if you prefer SQL over Python to analyze tabular data but have the data in a CSV file or any other supported file type by DuckDB.

#### Query a database with SQL and reference it in another SQL cell

You typically do this if you want to more easily debug a complex SQL query that consists of one or more common table expressions.&#x20;

Rather than writing this:

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2FmuqFjFUbRrYekGv0k9BF%2Fimage.png?alt=media&amp;token=e11011f6-77cc-4bde-b004-7c9f36529384" alt=""><figcaption><p>SQL Query with a common table expression</p></figcaption></figure>

You can write this:

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2FRY6kyU2C8VdprG9xraVl%2Fimage.png?alt=media&amp;token=7866737b-020d-437d-9351-c8bdb17243ac" alt=""><figcaption><p>CTE turned into a SQL cell in "Query mode" that is referenced in a second SQL cell.</p></figcaption></figure>

This approach works if your first query would return a huge result. Only a preview of that query result will be shown. When you execute the second query, the 'CTE-based query' is recompiled and executed on the database side rather than locally inside your workbook. You can review the compiled SQL that's being executed by by clicking on the "View Compiled SQL" icon:

<figure><img src="https://4179539225-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZqboFGZzD87nn7oPsm%2Fuploads%2F30w8R65yr7D8vEMvj6qg%2Fimage.png?alt=media&amp;token=8efcc6ef-3ef9-48cb-b825-2263034473b1" alt=""><figcaption><p>You can review the compiled SQL code that gets executed on the database side.</p></figcaption></figure>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://datalab-docs.datacamp.com/work/sql-cell/sql-scenarios.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
