| title | Quickstart: Python SQL Driver - mssql-python Connect to a SQL Database from a Jupyter Notebook in Visual Studio Code | ||
|---|---|---|---|
| description | This quickstart describes connect to your SQL database from a Jupyter Notebook in Visual Studio Code. | ||
| author | dlevy-msft-sql | ||
| ms.author | dlevy | ||
| ms.reviewer | vanto, randolphwest | ||
| ms.date | 12/29/2025 | ||
| ms.service | sql | ||
| ms.subservice | connectivity | ||
| ms.topic | quickstart-sdk | ||
| ms.custom |
|
In this quickstart, you use Jupyter Notebook in Visual Studio Code to quickly derive business insights. You use the mssql-python driver for Python to connect to your SQL database and read the data that is then formatted for use in emails, reports presentations, etc.
The mssql-python driver doesn't require any external dependencies on Windows machines. The driver installs everything that it needs with a single pip install, allowing you to use the latest version of the driver for new scripts without breaking other scripts that you don't have time to upgrade and test.
mssql-python documentation | mssql-python source code | Package (PyPI) | Visual Studio Code
-
Python 3
-
If you don't already have Python, install the Python runtime and pip package manager from python.org.
-
Don't want to use your own environment? Open as a devcontainer using GitHub Codespaces.
:::image type="icon" source="https://github.com/codespaces/badge.svg":::
-
-
Visual Studio Code with the following extensions:
-
Azure Command-Line Interface (CLI) for passwordless authentication on macOS and Linux.
-
If you don't already have
uv, follow the installation instructions. -
A database on SQL Server, Azure SQL Database, or SQL database in Fabric with the [!INCLUDE sssampledbobject-md] sample schema and a valid connection string.
-
Install one-time operating system specific prerequisites.
apk add libtool krb5-libs krb5-devapt-get install -y libltdl7 libkrb5-3 libgssapi-krb5-2dnf install -y libtool-ltdl krb5-libszypper install -y libltdl7 libkrb5-3 libgssapi-krb5-2zypper install -y libltdl7brew install openssl
This quickstart requires the [!INCLUDE sssampledbnormal-md] Lightweight schema, on Microsoft SQL Server, SQL database in Fabric or Azure SQL Database.
Create a SQL database in minutes using the Azure portal
Load AdventureWorks sample data in your SQL database in Microsoft Fabric
AdventureWorks sample databases
Create a new local copy of a database in a container with sqlcmd
Create a new SQL Server container using the MSSQL extension for Visual Studio Code
- Create a new project
- Add dependencies
- Launch Visual Studio Code
- Update pyproject.toml
- Save the connection string
- Create a Jupyter Notebook
- Display results in a table
- Display results in a chart
-
Open a command prompt in your development directory. If you don't have one, create a new directory called
python,scripts, etc. Avoid folders on your OneDrive, the synchronization can interfere with managing your virtual environment. -
Create a new project with
uv.uv init jupyter-notebook-qs cd jupyter-notebook-qs
In the same directory, install the mssql-python, python-dotenv, rich, pandas, and matplotlib packages. Then add ipykernel and uv as dev dependencies. VS Code requires ipykernel and uv are added to be able to interact with uv from within your notebook cells using commands like !uv add mssql_python.
uv add mssql_python dotenv rich pandas matplotlib
uv add --dev ipykernel
uv add --dev uvIn the same directory, run the following command.
code .-
The pyproject.toml contains the metadata for your project.
-
Update the description to be more descriptive.
description = "A quick example using the mssql-python driver and Jupyter Notebooks."
-
Save and close the file.
-
Open the
.gitignorefile and add an exclusion for.envfiles. Your file should be similar to this example. Be sure to save and close it when you're done.# Python-generated files __pycache__/ *.py[oc] build/ dist/ wheels/ *.egg-info # Virtual environments .venv # Connection strings and secrets .env -
In the current directory, create a new file named
.env. -
Within the
.envfile, add an entry for your connection string namedSQL_CONNECTION_STRING. Replace the example here with your actual connection string value.SQL_CONNECTION_STRING="Server=<server_name>;Database=<database_name>;Encrypt=yes;TrustServerCertificate=no;Authentication=ActiveDirectoryInteractive"[!TIP]
The connection string used here largely depends on the type of SQL database you're connecting to. If you're connecting to an Azure SQL Database or a SQL database in Fabric, use the ODBC connection string from the connection strings tab. You might need to adjust the authentication type depending on your scenario. For more information on connection strings and their syntax, see connection string syntax reference.
-
Select File, then New File and Jupyter Notebook from the list. A new notebook opens.
-
Select File, then Save As... and give your new notebook a name.
-
Add the following imports in the first cell.
from os import getenv from mssql_python import connect from dotenv import load_dotenv from rich.console import Console from rich.table import Table import pandas as pd import matplotlib.pyplot as plt
-
Use the + Markdown button at the top of the notebook to add a new markdown cell.
-
Add the following text to the new markdown cell.
## Define queries for use later -
Select the check mark in the cell toolbar or use the keyboard shortcuts
Ctrl+EnterorShift+Enterto render the markdown cell. -
Use the + Code button at the top of the notebook to add a new code cell.
-
Add the following code to the new code cell.
SQL_QUERY_ORDERS_BY_CUSTOMER = """ SELECT TOP 5 c.CustomerID, c.CompanyName, COUNT(soh.SalesOrderID) AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC; """ SQL_QUERY_SPEND_BY_CATEGORY = """ select top 10 pc.Name as ProductCategory, SUM(sod.OrderQty * sod.UnitPrice) as Spend from SalesLT.SalesOrderDetail sod inner join SalesLT.SalesOrderHeader soh on sod.salesorderid = soh.salesorderid inner join SalesLT.Product p on sod.productid = p.productid inner join SalesLT.ProductCategory pc on p.ProductCategoryID = pc.ProductCategoryID GROUP BY pc.Name ORDER BY Spend; """
-
Use the + Markdown button at the top of the notebook to add a new markdown cell.
-
Add the following text to the new markdown cell.
## Print orders by customer and display in a table -
Select the check mark in the cell toolbar or use the keyboard shortcuts
Ctrl+EnterorShift+Enterto render the markdown cell. -
Use the + Code button at the top of the notebook to add a new code cell.
-
Add the following code to the new code cell.
load_dotenv() with connect(getenv("SQL_CONNECTION_STRING")) as conn: # type: ignore with conn.cursor() as cursor: cursor.execute(SQL_QUERY_ORDERS_BY_CUSTOMER) if cursor: table = Table(title="Orders by Customer") # https://un5kkfjgtd6vrk5rzvubfp0.julianrbryant.com/en/stable/appendix/colors.html table.add_column("Customer ID", style="bright_blue", justify="center") table.add_column("Company Name", style="bright_white", justify="left") table.add_column("Order Count", style="bold green", justify="right") records = cursor.fetchall() for r in records: table.add_row(f"{r.CustomerID}", f"{r.CompanyName}", f"{r.OrderCount}") Console().print(table)
[!TIP]
On macOS, bothActiveDirectoryInteractiveandActiveDirectoryDefaultwork for Microsoft Entra authentication.ActiveDirectoryInteractiveprompts you to sign in every time you run the script. To avoid repeated sign-in prompts, log in once via the Azure CLI by runningaz login, then useActiveDirectoryDefault, which reuses the cached credential. -
Use the Run All button at the top of the notebook to run the notebook.
-
Select the jupyter-notebook-qs kernel when prompted.
-
Review the output of the last cell. You should see a table with three columns and five rows.
-
Use the + Markdown button at the top of the notebook to add a new markdown cell.
-
Add the following text to the new markdown cell.
## Display spend by category in a horizontal bar chart -
Select the check mark in the cell toolbar or use the keyboard shortcuts
Ctrl+EnterorShift+Enterto render the markdown cell. -
Use the + Code button at the top of the notebook to add a new code cell.
-
Add the following code to the new code cell.
with connect(getenv("SQL_CONNECTION_STRING")) as conn: # type: ignore data = pd.read_sql_query(SQL_QUERY_SPEND_BY_CATEGORY, conn) # Set the style - use print(plt.style.available) to see all options plt.style.use('seaborn-v0_8-notebook') plt.barh(data['ProductCategory'], data['Spend'])
-
Use the Execute Cell button or
Ctrl+Alt+Enterto run the cell. -
Review the results. Make this notebook your own.
Visit the mssql-python driver GitHub repository for more examples, to contribute ideas or report issues.
[!div class="nextstepaction"] mssql-python driver on GitHub