Streamline Data Analysis with VSCode's Polyglot Notebook
Streamline Data Analysis with VSCode's Polyglot Notebook

Share SQL Query Results with Python in Polyglot Notebook for VSCode

Discover how VSCode's Polyglot Notebook streamlines SQL-to-Python workflows for seamless data analysis and visualization.5 min


Switching between SQL and Python in traditional workflows can be tedious. Often, data professionals find themselves repeatedly exporting query results, saving them as CSV files, and importing them into Python scripts every time they move between these two tools. Thankfully, VSCode’s Polyglot Notebook provides an easier way.

Polyglot Notebook for VSCode allows users to work seamlessly with multiple programming languages—including SQL and Python—within the same notebook. Imagine writing queries, performing Python data manipulations, and sharing variables between cells effortlessly. Sound useful? Let’s quickly set it up.

Setting Up Polyglot Notebook in VSCode

If you’re new to Polyglot Notebook, start by installing the Polyglot Notebook extension for VSCode:

  • Open Visual Studio Code, navigate to the Extensions tab on the sidebar.
  • Search for “Polyglot Notebook” and install the official extension from Microsoft.
  • Once installed, restart VSCode so the changes take effect.

Now you’re set. Create a fresh Polyglot Notebook by hitting CTRL + SHIFT + P (or CMD + SHIFT + P on Mac) and selecting “Polyglot Notebook: Create new blank notebook.”

You’ll instantly notice the familiar Jupyter-like notebook style—with one significant advantage: support for multiple languages, from SQL and Python to JavaScript, comfortably within one interactive notebook.

Sharing SQL Query Results in Polyglot Notebook

One of Polyglot Notebook’s main perks is its ability to seamlessly transfer data between SQL cells and Python cells. Let’s explore exactly how to export SQL query results and then immediately utilize them in Python.

Say you’ve got a straightforward database and write the following SQL query in a notebook cell:

%%sql
SELECT EmployeeID, FirstName, LastName, Department
FROM Employees
WHERE Department = 'Engineering';

Running this SQL cell conveniently displays your results right in the notebook. More importantly, Polyglot Notebook allows you to export this data as a variable, easily accessible from Python. Here’s how you make the results accessible downstream:

%%sql --output engineering_team
SELECT EmployeeID, FirstName, LastName, Department
FROM Employees
WHERE Department = 'Engineering';

In this snippet, the query results are saved into a new notebook variable called engineering_team. Now let’s hop into Python to access and manipulate this data directly.

Next, in a Python cell, access the exported data:

# convert SQL results to pandas DataFrame
import pandas as pd
eng_team_df = pd.DataFrame(engineering_team)

# Display the first few rows
eng_team_df.head()

Within seconds, you transitioned smoothly from SQL querying into full Python data analysis without exporting files manually. Productivity at its finest.

Working with SQL and Python Together in Polyglot Notebook

Polyglot notebook supports more advanced workflows, too. For example, you may want to analyze data in SQL but perform aggregations, visualizations, or machine learning tasks in Python.

Consider an example of analyzing sales data. First, create a temporary SQL table storing sales summaries, then pull this right into Python:

%%sql
CREATE TABLE #TempSales (
    Product VARCHAR(50),
    TotalSales INT
);

INSERT INTO #TempSales (Product, TotalSales)
SELECT ProductName, SUM(SalesAmount)
FROM ProductSales
GROUP BY ProductName;

-- Export the table to Python
SELECT * FROM #TempSales;

Remember to export the SQL result explicitly:

%%sql --output sales_summary
SELECT * FROM #TempSales;

Now, within the next Python cell, you directly access and analyze the sales summary results:

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# Convert SQL result into pandas DataFrame
sales_df = pd.DataFrame(sales_summary)

# Plot the data
sns.barplot(data=sales_df, x='Product', y='TotalSales')
plt.xticks(rotation=45)
plt.title('Total Sales by Product')
plt.show()

In moments, you’ve easily combined SQL and Python skills without leaving the notebook or exporting files.

How does Polyglot Notebook Compare with Other Tools?

Polyglot notebooks provide a convenient alternative to standard workflows involving separate IDEs, database viewers, Jupyter notebooks, or Excel files. Let’s quickly summarize key advantages:

  • Unified Environment: Eliminates juggling multiple applications, scripts, or exporting CSV files. Everything can happen neatly within one tool.
  • Easy Data Exchange: Seamlessly share complex result sets or variables between SQL and Python—effortlessly enhancing productivity and experiments.
  • Interactive Visualizations: Instantly visualize SQL-based aggregations or analyses using Python’s powerful libraries (Seaborn, Matplotlib, etc.), enhancing readability and insight.

However, Polyglot Notebooks do come with their limitations you should be aware of:

  • Steeper Initial Setup: Requires installation and some initial configuration, presenting a minor hurdle for absolute beginners.
  • Performance: Best for small to medium-sized datasets; extra-large databases or complex computations may require specialized database tools or data engineering workflows.

For heavy lifting in complex pipelines or huge datasets, Polyglot Notebook might not entirely replace a powerful ETL process, but it remains a valuable tool to streamline daily data science and analysis tasks.

Polyglot Notebook simplifies workflows tremendously by quickly bridging the gap between SQL-centric analysis and Python-driven data exploration. Its ability to execute multiple languages in one shareable notebook is changing how data professionals approach analytical tasks.

Give the Polyglot Notebook a try today—your workflow might thank you for it. Have you experimented with Polyglot Notebook yet, or do you prefer other multi-language tools? Let us know your thoughts!


Like it? Share with your friends!

Shivateja Keerthi
Hey there! I'm Shivateja Keerthi, a full-stack developer who loves diving deep into code, fixing tricky bugs, and figuring out why things break. I mainly work with JavaScript and Python, and I enjoy sharing everything I learn - especially about debugging, troubleshooting errors, and making development smoother. If you've ever struggled with weird bugs or just want to get better at coding, you're in the right place. Through my blog, I share tips, solutions, and insights to help you code smarter and debug faster. Let’s make coding less frustrating and more fun! My LinkedIn Follow Me on X

0 Comments

Your email address will not be published. Required fields are marked *