This is dynamic SQL.
It allows you to build a query whose construction relies on information that might not be available at the time you're writing the query. For example, the query might take advantage of parameter values that come out of a table, which weren't there when the query was written.
It makes a query flexible and re-usable, which means that you don't end up writing similar code over and over.