---
title: "SQL Query: Extracting Employees Sal > Avg Salary"
description: "The SQL query, Pandas, and PySpark code extract employees earning more than the average salary."
url: https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/
date: 2024-04-06
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2024/04/image-7.png?fit=1024%2C768&ssl=1
categories: ["PySpark"]
type: post
lang: en
---

# SQL Query: Extracting Employees Sal > Avg Salary

Here is an SQL query that extracts employees whose salary is greater than the average salary.

Related[Derived Table Vs. Sub-Query – Here are Top Differences](https://srinimf.com/2019/03/06/db2-derived-table-key-knowledge-points-you-need-to-learn/)

Table of contents

1. [SQL Query](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/#sql-query)
2. [Pandas Code](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/#pandas-code)
3. [PySpark Code](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/#pyspark-code)

## SQL Query

Here’s a [way to write](https://srinimf.com/2023/10/09/the-easy-way-to-write-correlated-sub-query-5-examples/) [in SQL](https://srinimf.com/2018/06/02/alias-is-an-optional-column/)

```
SELECT 
    employee_id, 
    last_name, 
    first_name, 
    salary 
FROM 
    employee 
WHERE 
    salary > (SELECT AVG(salary) FROM employee);
```

Related[Mainframe With COBOL Avg Salary](https://srinimf.com/2017/04/21/cobol-mainframe-avg-salary/)

## Pandas Code

Here’s a way to write in Pandas

```
import pandas as pd

# Assuming you have a DataFrame named 'employee_df' containing employee data

# Calculate the average salary
average_salary = employee_df['salary'].mean()

# Extract employees with salary greater than the average salary
high_salary_employees = employee_df[employee_df['salary'] > average_salary]

# Display the result
print(high_salary_employees)
```

## [PySpark Code](https://srinimf.com/2024/09/30/how-to-calculate-click-percentage-dept-wise-salary-matches-in-pyspark/)

Here’s a way to write in PySpark.

```
from pyspark.sql import SparkSession
from pyspark.sql.functions import avg

# Initialize SparkSession
spark = SparkSession.builder \
    .appName("HighSalaryEmployees") \
    .getOrCreate()

# Assuming you have loaded your data into a DataFrame named 'employee_df'
# If not, replace 'employee_df' with your actual DataFrame

# Calculate the average salary
average_salary = employee_df.select(avg("salary")).collect()[0][0]

# Extract employees with salary greater than the average salary
high_salary_employees = employee_df.filter(employee_df["salary"] > average_salary)

# Display the result
high_salary_employees.show()

# Stop SparkSession
spark.stop()
```

Related[PySpark Practice Guide: 10 Essential Examples for Data Analysis](https://srinimf.com/2023/09/05/how-to-practice-apache-pyspark-10-best-examples/)

References

- Original post: [SQL Query and Code in Pandas & PySpark](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/)

Related[Top 10 Nike Data Engineer Interview Questions on Spark, SQL, and Delta Lake (2025)](https://srinimf.com/2025/09/17/top-10-nike-data-engineer-interview-questions-on-spark-sql-and-delta-lake-2025/)

### 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%2F2024%2F04%2F06%2Fsalavgsal-sql-query-how-to-write-in-pyspark-and-pandas%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2024%2F04%2Fimage-7.png%3Ffit%3D1024%252C768%26ssl%3D1&description=SQL%20Query%3A%20Extracting%20Employees%20Sal%20%26gt%3B%20Avg%20Salary)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2024/04/06/salavgsal-sql-query-how-to-write-in-pyspark-and-pandas/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20SQL%20Query%3A%20Extracting%20Employees%20Sal%20%3E%20Avg%20Salary&body=https%3A%2F%2Fsrinimf.com%2F2024%2F04%2F06%2Fsalavgsal-sql-query-how-to-write-in-pyspark-and-pandas%2F&share=email)
-

### Like this:

Like Loading…
