Menu
Stuff by Yuki
  • Home
  • Data Engineering
    • Python
  • Business Intelligence
    • Power BI
    • Tableau
  • Perspectives
  • About
  • Contact
Stuff by Yuki

How to Use SQL in Polars

Posted on May 15, 2023September 6, 2024

Introduction

There are a few ways you can use SQL in Polars. One option is to use other libraries such as DuckDB and pandas. And another option is to actually run SQL without using other libraries.

I’ll be demonstrating the latter in this blog post. Please refer to this post on how to use DuckDB in Polars.

I myself didn’t know you could run SQL directly in Polars without relying on other libraries until one of my fellow LinkedIn connections, Luca introduced it in his post.

If you follow along, here’s the link to my Github repo.

Use SQL in Polars

The code is as simple as it looks.

  1. Create a context for SQL to work on a Polars dataframe.
  2. Register dataframe’s name to be used in your SQL query.
  3. Execute the query that returns a Polars dataframe.
Copy Copied Use a different Browser

import polars as pl

# read in data
df_pl = pl.scan_csv('../sample_data.csv')

# prep for sql execution
sql = pl.SQLContext()
sql.register('df_pl', df_pl)

result_df = sql.execute(
    """
      select 
        *
      from df_pl
      where Name = 'Mike'
    """
).collect()

print(result_df)

"""
output:
shape: (1, 4)
┌───────────┬──────┬─────┬───────────────────┐
│ studentId ┆ Name ┆ Age ┆ FirstEnrolledDate │
│ ---       ┆ ---  ┆ --- ┆ ---               │
│ i64       ┆ str  ┆ i64 ┆ str               │
╞═══════════╪══════╪═════╪═══════════════════╡
│ 1         ┆ Mike ┆ 24  ┆ 2020-01-17        │
└───────────┴──────┴─────┴───────────────────┘
"""

Summary

Using SQL directly in Polars is pretty simple in that you just need to know a few lines of specific Polars code. Many people working in data are familiar with SQL. The ability to use SQL in Polars helps unlock the performance of Polars to those who otherwise cannot.

Github repo

References

  • https://pola-rs.github.io/polars/py-polars/html/reference/sql.html
  • https://www.linkedin.com/posts/lucazanna_data-python-sql-activity-7033560005182148611-5pKp/?utm_source=share&utm_medium=member_desktop
  • https://www.confessionsofadataguy.com/polars-laziness-and-sql-context/

Leave a Reply Cancel reply

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

Recent Posts

  • Where I’m Headed in the Next 5 Years
  • Open-Source vs Vendor Data Tools
  • Developing the Habit of Writing
  • How to Inspect and Optimize Query Plans in Python Polars
  • Learn Python Polars with Polars Cookbook

Popular Posts

  • A Running Total Calculation with Quick Measure in Power BI
  • A Complete Guide to Git Integration in Power BI
  • How To Copy And Paste Report Page in Power BI
  • Handling Missing Values in Polars
  • How to Convert String to Date or Datetime in Polars

connect with me

  • LinkedIn
  • Twitter
  • Github
  • Website

Search Articles

©2025 Stuff by Yuki | Powered by SuperbThemes