Skip to main content
PYSPARK • LESSON 26

SQL Expressions with selectExpr()

How can we apply inline SQL calculations and column renaming directly inside a select operation?

Beginner2 Minutes170 XP
🤔 THE QUESTION

How can we apply inline SQL calculations and column renaming directly inside a select operation?

💡 WHAT IS IT?

selectExpr() accepts standard SQL expression strings, allowing simultaneous calculation and alias projection.

🎯 WHAT IS IT USED FOR?

Quick column arithmetic, conditional SQL expressions, and column aliasing without importing functions.

💻 EXAMPLE
df.selectExpr("name", "salary * 1.10 AS revised_salary").show()

🎯 Mission Objectives

Practice typing production-grade PySpark code for SQL Expressions with selectExpr().

  • Use selectExpr() for SQL expressions
  • Perform inline percentage calculations
  • Alias derived output column