Aggregate Methods in HQL


What is HQL? Why do we need it?

HQL stands for Hibernate Query Language. Designed as a "small" object-focused extension in SQL it provides an excellent bridge between Java classes and website tables.

We need HQL because it is more beneficial to use instead of native SQL for retrieving data from databases. The following are some of the reasons:

  • Provides full support for relational operations.  
  • Return results as objects. Which can be used to manipulate or retrieve data in an application without explicitly creating objects.
  • Supports polymorphic queries. 
  • Easy to learn and use as its syntax is quite similar to that of SQL.
  • It supports a number of advanced features compared to SQL, such as lie, download with a flexible profile, internal / external / full join, and cartesian product. It also supports predictions, aggregation (size, scale), compilation, order, sub-queries, and SQL function calls.
  • Provides database independence.

Aggregate Functions 

Hibernate Query Language (HQL) supports various aggregate functions in the SELECT statement. These calculate the final result using the property values of all objects satisfying the given query criteria.

  • avg(property name): The average of a property's value
  • count(property name or *): The number of times a property occurs in the results
  • max(property name): The maximum value of the property values
  • min(property name): The minimum value of the property values
  • sum(property name): The sum total of the property values

Just like any other SQL keyword, usage of these functions is case-insensitive.