
- DBMS Tutorial
- DBMS - Home
- DBMS - Overview
- DBMS - Architecture
- DBMS - Data Models
- DBMS - Data Schemas
- DBMS - Data Independence
- Entity Relationship Model
- DBMS - ER Model Basic Concepts
- DBMS - ER Diagram Representation
- DBMS - Generalization, Aggregation
- Relational Model
- DBMS - Codd's Rules
- DBMS - Relational Data Model
- DBMS - Relational Algebra
- DBMS - ER to Relational Model
- DBMS- SQL Overview
- Relational Database Design
- DBMS - Database Normalization
- DBMS - Database Joins
- Storage and File Structure
- DBMS - Storage System
- DBMS - File Structure
- Indexing and Hashing
- DBMS - Indexing
- DBMS - Hashing
- Transaction And Concurrency
- DBMS - Transaction
- DBMS - Concurrency Control
- DBMS - Deadlock
- Backup and Recovery
- DBMS - Data Backup
- DBMS - Data Recovery
- DBMS Useful Resources
- DBMS - Quick Guide
- DBMS - Useful Resources
- DBMS - Discussion
HAVING with GROUP BY in MySQL
To use HAVING with GROUPBY in MySQL, the following is the syntax. Here, we have set a condition under HAVING to get check for maximum value condition −
SELECT yourColumnName FROM yourTableName GROUP BY yourColumnName HAVING MAX(yourColumnName) < yourValue;
Let us see an example by creating a table in MySQL −
mysql> create table WhereAfterGroupDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserProcess int, -> UserThreadId int -> ); Query OK, 0 rows affected (5.74 sec)
Example
Insert some records in the table using insert command. The query is as follows −
mysql> insert into WhereAfterGroupDemo(UserProcess,UserThreadId) values(1211,3); Query OK, 1 row affected (0.10 sec) mysql> insert into WhereAfterGroupDemo(UserProcess,UserThreadId) values(1412,3); Query OK, 1 row affected (0.39 sec) mysql> insert into WhereAfterGroupDemo(UserProcess,UserThreadId) values(1510,4); Query OK, 1 row affected (0.19 sec) mysql> insert into WhereAfterGroupDemo(UserProcess,UserThreadId) values(1511,4); Query OK, 1 row affected (0.31 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from WhereAfterGroupDemo;
Output
+--------+-------------+--------------+ | UserId | UserProcess | UserThreadId | +--------+-------------+--------------+ | 1 | 1211 | 3 | | 2 | 1412 | 3 | | 3 | 1510 | 4 | | 4 | 1511 | 4 | +--------+-------------+--------------+ 4 rows in set (0.00 sec)
The following is the query to use HAVING and GROUP BY and get the UserThreaId with process less than 1510 −
mysql> SELECT UserThreadId FROM WhereAfterGroupDemo GROUP BY UserThreadId HAVING MAX(UserProcess) < 1510;
Output
+--------------+ | UserThreadId | +--------------+ | 3 | +--------------+ 1 row in set (0.00 sec)
- Related Articles
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING
- Listing all rows by group with MySQL GROUP BY?
- Get rows with GROUP BY in MySQL?
- MySQL: update field with Group By?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- SQL query describing usage of SUM aggregate function and GROUP-BY with HAVING
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Why should we not use group functions with non-group fields without GROUP BY clause in MySQL SELECT query?
- Does MySQL eliminate common subexpressions between SELECT and HAVING/GROUP BY clause? How to test it?
- SELECT DISTINCT vs GROUP BY in MySQL?
- How can we create a MySQL view with GROUP BY clause?
- MySQL- GROUP and COUNT by date?
- How to implement GROUP by range in MySQL?
- Limit the count using GROUP BY in MySQL

Advertisements