# SQL cell

SQL is a first-class citizen in DataLab, with a powerful query engine to query databases, data warehouses, data frames and data files like CSV files or Excel files\*. With autocompletion, a full-fledged schema browser and SQL interpolation, DataLab brings all the SQL features you need to power your analyses.&#x20;

Through the ability to query data frames and data files with SQL code, you can easily switch from SQL to Python/R and back. Through the 'Query mode' you can break up SQL queries riddled with common table expressions (CTEs) into chains of SQL cells, which makes things easier to debug and reuse.

<sub>\*Reading Excel files using SQL cells is supported in Python 3.10 and R 4.4 environments</sub>

### Using the SQL cell

* Click "Add SQL cell" at the bottom of a notebook file (or in between two cells).
* In the first dropdown in the header of the SQL cell, select **which data source you want to query**:
  * To run a query **against a remote data warehouse or database**, select it from the list. If you have a SQL database that you want to connect to, see [Connect your data to DataLab](/connect-to-data/connect-your-data-to-workspace.md) If you don't have a SQL database to connect to, you can select one of the sample databases.
  * To run a query **against a local DataFrame or CSV file** inside your workbook, select "DataFrames and CSVs".
* In the second dropdown in the SQL cell, **decide how the SQL cell should run**:
  * **DataFrame mode**: the full result of the SQL query will be stored as a DataFrame with a name you can specify.
  * **Query mode**: only a preview of the SQL query will show. The query is saved with a name that you can refer to in subsequent SQL queries.
* (Optional) In the input field behind 'available as', specify the name of the data frame or the query (depending on your choice in the previous step).
* Write the SQL query.
* Click "Run" in the cell menu (or use the shortcuts). Depending on the mode you selected, you'll have a DataFrame to continue your analysis or a query that you can reference in a next SQL cell.

<figure><img src="/files/pJtw08u2zaiKW4YP0V6b" alt=""><figcaption><p>Example of a running a query against the <code>Ticket Sales</code> sample database. The query result is available as <code>tickets</code>.</p></figcaption></figure>

### Dive deeper

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).

Finally, there are times you want to dynamically update your SQL queries based on results of previous calculations or other data in your notebook. If this rings a bell, learn how you can [Parameterize your SQL query](/work/sql-cell/parameterize-your-sql-query.md).

###


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
