Problems / Extract Email Domain / Editorial
split_part(email, '@', 2)
'@'
regexp_extract(email, '@(.+)$', 1)
GROUP BY domain
COUNT(*) AS user_count
user_count DESC, domain
In PySpark, F.split("email", "@").getItem(1) is the equivalent of split_part, followed by groupBy("domain").agg(F.count("*")).
F.split("email", "@").getItem(1)
split_part
groupBy("domain").agg(F.count("*"))
String extraction followed by aggregation is a two-step pattern: compute the derived column first (in the SELECT list or a withColumn), then aggregate over it. Because counts can tie, an ordering on the aggregate alone is not deterministic — always add a tiebreaker column.
withColumn
Solve Extract Email Domain yourself →