From the course: Java Persistence with JPA and Hibernate

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Aggregate functions

Aggregate functions

- [Instructor] Just as you use aggregate functions in SQL, you can use aggregate functions with JPQL as well. Just to refresh, what are aggregate functions? Well, functions like MIN, MAX, SUM, COUNT, and AVG. All of them can perform a calculation on a set of values and return a single value. All five aggregate functions supported in SQL are supported by JPQL as well. Let's try to use them with simple examples. Let's run mariadb-init.sql script and mariadb-07_04.sql script first. Let's say you want to know the number of books authored by the author Allen. You can use the aggregate function COUNT to find this. If you check in the database, there are two books authored by this author. Author Allen has an ID of two, and there are two books authored by that author, which means you should get the result as two. If you go to the Main class and to the method aggregateFunctions. So your query should be SELECT and you should count the books, so you use COUNT(b). B is the alliance or reference…

Contents