ROW_NUMBER() OVER () with order by in H2
11,181
This is not supported in the H2 database before V1.4.198 (release February 2019). You would need to use:
select rownum(), name
from students
order by data
As of V1.4.198, support for ROW_NUMBER (and some other window functions) was added (see H2 Changelog), so now your query should work as expected.
Author by
Jose Victor
Updated on June 14, 2022Comments
-
Jose Victor almost 2 years
i'm trying to execute a query on a table in H2 database with ROW_NUMBER clause. Here is my query:
SELECT ROW_NUMBER() OVER (order by data), name FROM students
But i get an error in H2 console:
Syntax error in SQL statement "SELECT ROW_NUMBER() OVER (order[*] by data), name FROM students"; expected ")";
I noticed that it only works if OVER clause is empty like OVER();
Any ideas?
-
Pacerier over 8 yearsI see that you're the creator of H2. Why did you create a new database when there're already good open-source and free stuff like MySQL?
-
Thomas Mueller over 8 yearsThere are many reasons, for example MySQL is not written in Java, and it is more complex and bigger.
-
Pacerier over 8 yearsHmm, but what exactly is the benefit of being written in Java? (MySQL can run on Windows, Unix, and all major OS.) Usually people don't cite "written in Java" as an advantage, because being binded to the JVM is a disadvantage not an advantage.....
-
Thomas Mueller over 8 yearsFor developers that use Java, using a Java database is much simpler than using MySQL (no need to install additional software). It is also much faster, specially the in-memory variant. By the way, you can run H2 on Android as well (even thought SQLite is used there usually).
-
Thomas Mueller over 8 yearsYou are right having to use the JVM is a disadvantage. I was hoping to convert the source code and then use LLVM to compile it at some point.
-
Thomas Mueller over 8 yearsActually, most people use H2 either as a unit-test database, or as an embedded database (multi-platform, easier to install than for example MySQL if your application is written in Java).
-
WestCoastProjects about 7 yearsI ran into bugs in sqllite that made h2 indispensable. I use it for small/ quick up and running db's and testing db's. Mysql is as @ThomasMueller said heavier.
-
Pavel_K about 7 yearsCan you say when this feature will be implemented?
-
Pavel_K over 5 yearsSeems you can not say.
-
sleske over 4 years@Pavel_K: It's implemented now :-).
-
Pavel_K over 4 years@sleske Thank you for your comment. I will check.