filter & where — keeping rows
filter and where are the exact same method — where is just a SQL-friendly alias. Both keep only the rows for which the condition is true. You can pass a Column expression or a SQL string.
def filter(condition: Column): Dataset[Row] def filter(conditionExpr: String): Dataset[Row] def where(condition: Column): Dataset[Row] // alias of filter def where(conditionExpr: String): Dataset[Row]
1. A Column expression
The idiomatic form — build the condition with column operators. Keep only people from India:
people.filter($"country" === "India").show()
2. A SQL string
Sometimes a plain SQL string is more readable — especially for people coming from SQL. where makes it read naturally:
people.where("age > 30 AND country = 'India'").show()3. Chaining = implicit AND
Stacking filter calls is the same as joining them with AND — handy when conditions are built up in stages:
people .filter($"country" === "India") .filter($"age" > 30) .show()
filter and where are 100% interchangeable — pick one per codebase for consistency. Spark teams usually use filter in Scala (it's the Dataset method name) and where when the condition reads like SQL.