In some cases select statement contains duplicate key columns which causes BadSqlGrammarException when "order by" gets included in sql. Code that causes duplication: https://github.com/spring-projects/spring-data-relational/blob/21e8d9a4fac7203f0f4d9a331b43e758ac47052c/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java#L537-L539