---
title: "How to Write SQL Queries in PySpark: 3 Top Methods"
description: "Here are the 3 top methods people use to write SQL queries in PySpark"
url: https://srinimf.com/2023/09/17/how-to-write-sql-queries-in-pyspark-3-top-methods/
date: 2023-09-17
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/09/pexels-photo-1558690.jpeg?fit=1733%2C1300&ssl=1
categories: ["PySpark"]
tags: ["PySpark SQL"]
type: post
lang: en
---

# How to Write SQL Queries in PySpark: 3 Top Methods

In PySpark, you can write SQL queries using the Spark SQL module, which allows you to execute SQL queries on DataFrames. Here are several [ways to](https://srinimf.com/2024/03/25/5-best-ways-to-delete-rows-in-pyspark/) write and execute SQL in PySpark.

![](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/09/pexels-photo-8903617.jpeg?resize=1880%2C1253&ssl=1)*Photo by RDNE Stock project on Pexels.com*

## 01. Using the SQL Method

You can use the SQL method on a SparkSession to execute SQL queries. Here’s an example:

```
from pyspark.sql import SparkSession
```

**#Create a Spark session**

```
spark = SparkSession.builder.appName("example").getOrCreate()
```

**#Create a DataFrame**

```
data = [("Alice", 25),
("Bob", 30),
("Charlie", 35)]
columns = ["name", "age"]
df = spark.createDataFrame(data, columns)
```

**#Register the DataFrame as a temporary table**

```
df.createOrReplaceTempView("people")
```

**#Write and execute SQL queries**

```
result = spark.sql("SELECT name, age FROM people WHERE age > 30")
result.show()
```

## 02. Using the spark.sql Function

You can also use the spark.sql function to execute SQL queries directly:

```
from pyspark.sql import SparkSession
```

**#Create a Spark session**

```
spark = SparkSession.builder.appName("example").getOrCreate()
```

**#Create a DataFrame**

```
data = [("Alice", 25),
("Bob", 30),
("Charlie", 35)]
columns = ["name", "age"]
df = spark.createDataFrame(data, columns)
```

**#Register the DataFrame as a temporary table**

```
df.createOrReplaceTempView("people")
```

**#Write and execute SQL queries using spark.sql**

```
result = spark.sql("SELECT name, age FROM people WHERE age > 30")
result.show()
```

## 03. Using Inline SQL in DataFrame Operations

You can use inline SQL expressions with DataFrame operations like filter, selectExpr, and agg. For example:

```
from pyspark.sql import SparkSession
```

**#Create a Spark session**

```
spark = SparkSession.builder.appName("example").getOrCreate()
```

**#Create a DataFrame**

```
data = [("Alice", 25),
("Bob", 30),
("Charlie", 35)]
columns = ["name", "age"]
df = spark.createDataFrame(data, columns)
```

**#Use inline SQL with a filter**

```
result = df.filter("age > 30")
result.show()
```

**#Using Python Variables in SQL Queries**

You can use Python variables in SQL queries by interpolating them using f-strings or .format():

```
from pyspark.sql import SparkSession
```

**#Create a Spark session**

```
spark = SparkSession.builder.appName("example").getOrCreate()
```

**#Create a Python variable**

```
min_age = 30
```

**#Write and execute SQL queries with Python variables**

```
result = spark.sql(f"SELECT name, age FROM people WHERE age > {min_age}")
result.show()
```

These are some common ways to write and execute SQL in PySpark. You can choose the method that best suits your requirements and coding style.

## Sports/Running Shoes

ASIAN Men’s Wonder-13 Sports Running Shoes.

- [Asian Sports Running Shoes](https://www.amazon.in/ASIAN-Wonder-Firozi-Sports-Indian/dp/B01MTQ5M7B/?tag=srinimf-21)

Related

- [An In-Depth Tutorial on Setting Up PySpark SQL from Scratch](https://srinimf.com/2023/10/21/an-in-depth-tutorial-on-setting-up-pyspark-sql-from-scratch/)DateOctober 21, 2023
- [Perficient: Top SQL, PySprak Interview Questions](https://srinimf.com/2024/02/27/perficient-top-sql-pysprak-interview-questions/)DateFebruary 27, 2024
- [SQL Query: Extracting Employees Sal > Avg Salary](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/)DateApril 6, 2024

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F09%2F17%2Fhow-to-write-sql-queries-in-pyspark-3-top-methods%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F09%2Fpexels-photo-1558690.jpeg%3Ffit%3D1200%252C900%26ssl%3D1&description=How%20to%20Write%20SQL%20Queries%20in%20PySpark%3A%203%20Top%20Methods)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/09/17/how-to-write-sql-queries-in-pyspark-3-top-methods/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/09/17/how-to-write-sql-queries-in-pyspark-3-top-methods/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/09/17/how-to-write-sql-queries-in-pyspark-3-top-methods/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/09/17/how-to-write-sql-queries-in-pyspark-3-top-methods/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Write%20SQL%20Queries%20in%20PySpark%3A%203%20Top%20Methods&body=https%3A%2F%2Fsrinimf.com%2F2023%2F09%2F17%2Fhow-to-write-sql-queries-in-pyspark-3-top-methods%2F&share=email)
-

### Like this:

Like Loading…
