Using Min, Max and Count on HQL.

Using Min, Max and Count on HQL.

How to use Min, Max and Count on HQL ?

View Answers

June 1, 2012 at 7:13 PM

HQL supports many aggregate functions. Min,max and count are some of them.

min() function returns minimum value of specified value in the table.

max() function returns maximum value of specified value in the table.

count() function returns number of rows of the table.

Example:

package net.roseindia.main;

import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class AggregateFunctionHQL {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String minHql = "Select min(emp.salary) FROM Employee emp";
        String maxHql = "Select max(emp.salary) FROM Employee emp";
        String countHql = "Select count(*) FROM Employee emp";

        Query minQuery = session.createQuery(minHql);
        Query maxQuery = session.createQuery(maxHql);
        Query countQuery = session.createQuery(countHql);
        System.out
                .println("Minimum salary in list : " + minQuery.list().get(0));
        System.out
                .println("Maximum salary in list : " + maxQuery.list().get(0));
        System.out.println("Number of Employee : " + countQuery.list().get(0));
    }
}

Output:

Hibernate: select min(employee0_.salary) as col_0_0_ from employee employee0_
Minimum salary in list : 15000
Hibernate: select max(employee0_.salary) as col_0_0_ from employee employee0_
Maximum salary in list : 220000
Hibernate: select count(*) as col_0_0_ from employee employee0_
Number of Employee : 4

Description: Here min(salary) returns the minimum salary in the employee table using HQL.

max(salary) returns the maximum salary in the employee table using HQL.

count(*) returns the number of employee in the table.


September 26, 2012 at 8:56 PM

Suppose that rather than the simple SELECT MAX(emp.salary)... I want the whole Employee object, with the max. salary, how do I write that query?

Something like: SELECT e from Employee e WHERE e.salary = MAX(e.salary) Except that's wrong!









Related Tutorials/Questions & Answers:
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
ModuleNotFoundError: No module named 'min-max'
ModuleNotFoundError: No module named 'min-max'  Hi, My Python...-max' How to remove the ModuleNotFoundError: No module named 'min-max... to install padas library. You can install min-max python with following command
Advertisements
ModuleNotFoundError: No module named 'min-max'
ModuleNotFoundError: No module named 'min-max'  Hi, My Python...-max' How to remove the ModuleNotFoundError: No module named 'min-max... to install padas library. You can install min-max python with following command
how to calculate max and min - RUP
and minimum using java language.  Hi friend, In Math class having two function max and min in java. public class MathMaxMin { public static...how to calculate max and min  hye!i'm a new for the java and i want
Count distinct value in HQL.
Count distinct value in HQL.  How do you count distinct value in HQL?   count(distinct fieldName) returns number of distinct value... = sessionFactory.openSession(); String countHql = "Select count(distinct salary
Example of Average, Sum, Min and Max functions in MySQL
Example of Average, Sum, Min and Max functions in MySQL  Hi, How to use the aggregate functions like Average, Sum, Min and Max in MySQL? Thanks... in set (0.00 sec) mysql> select max(email_count) from email
how to calculate max and min in the loop - Java Beginners
how to calculate max and min in the loop  thanks coz giving me... main(String a[]) throws IOException { int max,min; max=Integer.parseInt(a[0]); min=Integer.parseInt(a[0]); for(int i=0;imax) max=Integer.parseInt
Mysql Min Max
Mysql Min Max       Mysql Min Max is useful to find the minimum and maximum records from a table... Mysql Min Max. To understand and grasp the example we create a table 'employee
ModuleNotFoundError: No module named 'count_min_sketch'
ModuleNotFoundError: No module named 'count_min_sketch'  Hi, My... named 'count_min_sketch' How to remove the ModuleNotFoundError: No module named 'count_min_sketch' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'count_min_sketch'
ModuleNotFoundError: No module named 'count_min_sketch'  Hi, My... named 'count_min_sketch' How to remove the ModuleNotFoundError: No module named 'count_min_sketch' error? Thanks   Hi, In your
SQL Max Count
SQL Max Count       SQL Max Count is used to return the maximum records value of the specified column... an example from 'SQL Max Count'. To elaborate example we create a table 'Stu
Delete Query using HQL
Delete Query using HQL  Can we write 'AND' with 'WHERE' clause in delete query using HQL? For example: delete from table name where field1 = value1 and field2 = value2   The example query i have written
hibernate criteria Max Min Average Result Example
hibernate criteria Max Min Average Result Example In this Example, We... the Projections class max, min ,avg methods. Here is the simple Example code files...("Max Salary is : "); System.out.print(row[i]); System.out.printf("\n
Hibernate criteria query using Min().
Hibernate criteria query using Min().  How to find minimum value in Hibernate criteria query using Projection?   You can find out minimum value in hibernate criteria by using projection. Projection is an interface
Hibernate criteria query using Max()
Hibernate criteria query using Max()  What is Hibernate criteria query using Max()?   You can find out maximum value in hibernate criteria by using projection. Projection is an interface and a class
sum and avg function using HQL.
sum and avg function using HQL.  How to use sum and avg function in HQL?   HQL supports many aggregate functions. Sum and avg are some of them. sum() function returns summation of specified field in the table
Hibernate min() Function
Hibernate min() Function This section contains an example of the HQL min() function. min() function in HQL returns the smallest value from the selected... given below will demonstrate you how to embed the HQL min() function within
Hibernate HQL query by using like operator.
Hibernate HQL query by using like operator.  How use like operator in HQL?   HQL stands for Hibernate Query Language, provided by Hibernate... = sessionFactory.openSession(); String hql="SELECT emp.name, emp.salary, emp.dateOfJoin
HQL
HQL  What is the HQL in hibernate ? Explain the use of HQL.   Hi Friend, Please visit on this link....... Hibernate Query Language
column count using metadata
GET COLUMN COUNT USING "RESULTSETMETADATA"   In this program ,we will get column count using "resultsetmetadata ".Using resultset interface and metadata interface ,we can get properties of column
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Hibernate max() Function
Hibernate max() Function In this tutorial you will learn about how to use HQL max() function. max() function in HQL returns the largest value from... is being given below will demonstrate you how to embed the HQL max() function
Case-insensitive search using Hibernate Query Language(HQL).
Case-insensitive search using Hibernate Query Language(HQL).  What is Case-insensitive search using Hibernate Query Language
Finding MAX value from emp_id column using SQL query?
Finding MAX value from emp_id column using SQL query?  I need to select maximum of the Emp_id values from SQL server 2005 table.I was using... of emp id from the table.(Actually it picks up Max value using by looking at 1st
how to count words in string using java
how to count words in string using java  how to count words in string using corejava   Hi Friend, Try the following code: import...: "); String st=input.nextLine(); int count=0
count values using jsp - JSP-Servlet
count values using jsp  hai deepak i have a query i am... and subheadings i want to get the count of textboxes which are heading under that how... by array count can you help me to sort out this problem expecting your favourable
MySQL Aggregate Function
', 'MIN', 'MAX', and 'SUM') in the MySQL Query. The group (aggregate) functions.... Query   SELECT designation, MIN(salary) , MAX...; +----------------------+-------------+-------------+ | designation | MIN(salary) | MAX(salary
How to calculate minimum in Hibernate using the min() Function
How to calculate minimum in Hibernate using the min() Function The min... in Java. The min() function is part of HQL and follows the semantics of Hibernate Query Language. ADS_TO_REPLACE_1 min() function in HQL returns the smallest
Count Records using the Prepared Statement
Count Records using the Prepared Statement  ... to count all records of the database table by using the PreparedStatement... the records of database table by using the PreparedStatement. For this, firstly
Generator Tag (Control Tags) Using Count Attributes
Generator Tag (Control Tags) Using Count Attributes   ... tag using the count attribute.  Add the following code snippet... and <s:iterator> tag prints it out using the  <s
Hibernate count() Function
Hibernate count() Function In this tutorial you will learn how to use the HQL count() function count() function counts the maximum number of rows in a table however it doesn't count the null values of the specified column
Hibernate Min() Function (Aggregate Functions)
(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all... Hibernate Min() Function (Aggregate Functions)  ... the Min() function. Hibernate supports multiple aggregate functions. When
Subset Tag (Control Tags) Example Using Count
Subset Tag (Control Tags) Example Using Count       In this section, we are going to describe the subset tag using the count parameter. The count parameter indicates the number of entries
HQL query
HQL query  how to select only password based on username using HQL query from login table where uname and paswrd are two column names
Hibernate Max() Function (Aggregate Functions)
(...), sum(...), min(...), max(...) , count(*), count(...), count(distinct... Hibernate Max() Function (Aggregate Functions... to use the Max() function. Hibernate supports multiple aggregate functions. When
Hibernate Named HQL using XML Mapping
In this section, you will learn executing Named HQL written in XML Mapping file
HQL in hibernate ? Explain the use of HQL.
HQL in hibernate ? Explain the use of HQL.  What is the HQL in hibernate ? Explain the use of HQL.   HQL stands for Hibernate Query...(); Session session = sessionFactory.openSession(); String hql="SELECT
Rows Count Example Using JDBC ResultSet
Rows Count Example Using  JDBC ResultSet: In this tutorial you can count the number of rows in the database table using ResultSet. Through this example... with count method and execute on created statement object and store result
convert into hql
convert into hql   String sqlListSurveys= "select distinct s.surveyAutoID, s.surveyTitle, date(s.modifiedOn) modifiedOn, s.createdOn..., (select ifnull(max(respondentID),0) FROM response, survey sur where surveyID
SQL Aggregate Functions List
List Queries. The Aggregate Function include the average, count, min, max, sum etc... rows in set (0.00 sec) COUNT Functions:  The  count (id..., name, count(id) as 'paper' -> from stu group by id
Program to count the number of unique words in a file using HashMap
Program to count the number of unique words in a file using HashMap  import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class CountUniqueWords { public static void main(String args
Min Value
Min Value   How to access min value of column from database table... min(id) from QuestionModel result_info"; Query query = session.createQuery(selectQuery); System.out.println("Min Value : " +query.list().get(0
Find max and min value from Arraylist
max() and min() respectively. Here is an example that consists of ArrayList of integers and using the max and min methods of Collections class, we have find...Find max and min value from Arraylist In this tutorial, you will learn how
HQL Query
HQL Query  Hibernate query language: How to use a left join in Hibernate query language. Can somebody help with an example. I am getting a unexpected token error in HQL Query tool If I use joins
HQL Sample
HQL Sample In this site there are many tutorials and example of HQL. These examples comes with HQL sample code that you can download and run on your computer and learn the HQL by modifying and executing these examples. The HQL Sample
Hibernate Count Query
) max( [ distinct | all ] object.property) min( [ distinct | all ] object.property... Hibernate Count Query       In this section we will show you, how to use the Count Query. Hibernate
HQL order by.
HQL order by.  HQL Order by query giving problem.   Order... into descending order you can follow the following HQL ? String hql="Select emp FROM...(); String hql="Select emp FROM Employee emp ORDER BY name"; Query query
HQL group By.
HQL group By.  How to use group by in HQL?   In general...=sessionFactory.openSession(); String hql="Select emp.name FROM Employee emp GROUP BY name"; Query query=session.createQuery(hql); query.setResultTransformer
min project with vb - WebSevices
min project with vb  i have do the min project with VB Frantend and oracle backend

Ads