Home Answers Viewqa Hibernate Using Min, Max and Count on HQL.

 
 


ratna rathor
Using Min, Max and Count on HQL.
2 Answer(s)      11 months ago
Posted in : Hibernate

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 Pages:
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
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
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... will demonstrate you how to embed the HQL min() function within your code
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 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
Mysql Min Max
Mysql Min Max       Mysql Min Max is useful to find the minimum and maximum records from a table. Understand with Example The Tutorial illustrate an example from Mysql Min Max
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
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... below will demonstrate you how to embed the HQL max() function within your code
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 criteria count.
Hibernate criteria count.  How do we count rows using criteria...(0)); } } Description: Hibernate criteria is used here as alternative to HQL. In general an application has a single sessionFactory. If you are using multiple
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
SQL Max Count
SQL Max Count       SQL Max Count is used to return the maximum records value of the specified column... 'SQL Max Count'. To elaborate example we create a table 'Stu'. The create table
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
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
MySQL Aggregate Function
in the MySQL Query. In this example we use the aggregate function ('COUNT', 'MIN', 'MAX', and 'SUM') in the MySQL Query. The group (aggregate) functions...; SELECT designation, MIN(salary) , MAX(salary) FROM employee GROUP
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
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
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
Hibernate Query Language
are: avg(...), sum(...), min(...), max(...) ...; Hibernate Query Language or HQL for short is extremely powerful query language. HQL is much like SQL  and are case-insensitive, except
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 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
SQL Aggregate Functions List
List Queries. The Aggregate Function include the average, count, min, max, sum etc... | +------+---------+-----------+ 5 rows in set (0.00 sec) COUNT Functions:  The  count (id) Function is a aggregate function that return the sum number of records
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
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
HQL
HQL  What is the HQL in hibernate ? Explain the use of HQL.   Hi Friend, Please visit on this link....... Hibernate Query Language
Java bigdecimal min example
Java bigdecimal min example       Example below demonstrates the working of bigdecimal class min() method. Java min method analysis the bigdecimal objects value and returns
HQL, Hibernate Query Language
columns. But in case of HQL we write queries using the persistence object... the programmer to write Query using the persistence entity. The HQL is fully... using HQL. HQL allows the developers to write queries in object oriented
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 Query in execute method of Struts2
data from database using hql. And where I need to write the hql query...HQL Query in execute method of Struts2  I am making login page in struts2 and using hibernate . Now I wants to pull user name and password from
Hibernate Named HQL using XML Mapping
In this section, you will learn executing Named HQL written in XML Mapping file
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
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
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
php array loop count
php array loop count  Count the array using loop in php
No Max-height Fixed
No Max-height Fixed      ... of a Textarea with minimum fixed height but no max height fixed. Steps to develop the Auto generate of a Textarea  with minimum fixed height but no max height fixed
SQLl query count and group by
SQLl query count and group by  How to write a sql query to count and group by the data?   Sql query to count the data and group the data using group by function... Select Name, Count(Name) As Count From Table Group
With Fixed Max-Height
; No Min-Height and max-height 100Px <textarea style="width... With Fixed Max-Height      ...; textarea  with fixed Max-Height. Steps to develop the Auto generate
Java bigdecimal max example
Java bigdecimal max example       Example below demonstrates the working of bigdecimall class max() method. Java max method analysis the bigdecimal objects value and returns
Heap Sort in Java
are going to sort integer values of an array using heap sort. There are two types of heap. First one is  Max heap and second one is min heap. Max heap is a special type of binary tree .The roots of  the max heap is greater than its
Hibernate criteria count.
Hibernate criteria count.  How do we count rows using criteria in hibernate?   We can get the rows count using the Projections. My code is like this: Criteria criteria = session.createCriteria(getReferenceClass
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
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
Count Row - JSP-Servlet
Count Row  Hello all, Please I need your help on how to desplay... using java servlet and html form. Thanks for your good job!   Hi friend... db = "register"; String driver = "com.mysql.jdbc.Driver"; int count= 0
Count instances of each word
Count instances of each word  I am working on a Java Project... of the words preceded by the occurrence count. My program compiles and runs... number 1 of 1 program 2 test 2 that 1 the 1 this 1 to 1 total 1 using 1 verify 1
PHP array count
The PHP array_count_values() function is used to count the elements... of occurrences. Example of PHP Array Count <?php    ...",7=>"ggg");     $arr=array_count_values($ar);   
Table maximum Row count - JDBC
, Sql Query for select maximum value max() function use. "select max
Count Active Thread in JAVA
Count Active Thread in JAVA In this tutorial, we are using activeCount() method of thread to count the current active threads. Thread activeCount() : Thread class provides you to check the current active thread by providing

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.