diff --git a/dev/change-scala-version.sh b/dev/change-scala-version.sh
index d9170218f6f21..77a3402414ff9 100755
--- a/dev/change-scala-version.sh
+++ b/dev/change-scala-version.sh
@@ -64,7 +64,8 @@ find "$BASEDIR" -name 'pom.xml' -not -path '*target*' -print \
# First find the right full version from the profile's build
# NOTE: We used to fetch the value of scala.version before but sed is used now. This is a workaround for SPARK-34762.
ESCAPED_TO_VERSION=$(echo $TO_VERSION | sed -n "s/\./\\\\./gp")
-SCALA_VERSION=$(sed -n "s;^.*\(.*\).*$;\1;p" pom.xml)
+SCALA_VERSION=$(sed -n "/scala-$ESCAPED_TO_VERSION<\/id>/,/<\/profile>/ \
+ s;^.*\(.*\).*$;\1;p" pom.xml)
sed_i '1,/[0-9]*\.[0-9]*\.[0-9]*[0-9]*\.[0-9]*\.[0-9]*'$SCALA_VERSION'' \
"$BASEDIR/pom.xml"