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?

Beginnerโฑ 2 Minutesโญ 170 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