PyCharm 2025.1 Help

Work with SQL cells

PyCharm allows you to use SQL cells to query databases and dataframes in Jupyter notebooks and automatically save query results to pandas dataframes.

Enable the Database Tools and SQL plugin

This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in PyCharm by default. If the relevant features are not available, make sure that you did not disable the plugin.

  1. Press Ctrl+Alt+S to open settings and then select Plugins.

  2. Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.

Query a database

  1. Select SQL on the toolbar in-between cells.

    Select SQL cell
  2. Click the widget on top of the SQL cell to select the data source or dataframe from the list:

    Select data source

    To add a new data source, follow the steps below:

    Add a new data source
    • Open the Database tool window by clicking Database or go to Window | Tool Windows | Database in the main menu.

      Database tool window opened
    • Do one of the following:

      • Click New in the Database tool window.

      • Press Alt+Insert.

    • Select Data Source and choose the database type.

      Select data source
    • Configure the connection settings in the Data Sources and Drivers dialog.

      Configure the data source settings
  3. Set the name of the variable in which the result of the SQL request will be stored as a dataframe.

    Set Python variable
  4. Enter the SQL statement and execute it by running the SQL cell.

    To run the SQL cell, do one of the following:

    • Click Run icon in the gutter.

    • Right-click the SQL cell and select Run cell from the context menu.

    • Press Ctrl+Enter.

    The query result will be shown in the output and saved to the dataframe.

    Query results displayed in dataframes
  5. Continue to work with the SQL variable in the code cells.

    Continue to work with SQl variable
Last modified: 03 March 2025