SQL HAVING command used with GROUP BY CommandWe can use SQL HAVING command to add condition to the query. The advantage of using HAVING command is it can be used in aggregate functions like count, max etc. We can't use SQL WHERE clause here. This way we can add value to our select command as per requirement. Usually SQL HAVING command is used in conjunction with group by command. Please read the SQL group by command and SQL sub group by command to know more about grouping the records. Here is the full table with all the rows. On this we will apply the group by query
This will display the total records in each group and sub group. Like this
Using WHERE conditionWe can’t use SQL WHERE condition to get similar result.This query will generate error message.
Use HAVING in place of WHERE in above query. Without the HAVING command you will get a result records like this below. SELECT category,type, count(*) as total FROM `documents` group by category,type This will display the total records in each group and sub group. Like this
![]()
This article is written by plus2net.com team.
https://www.plus2net.com
![]() ▼ More on getting records from table with different combinations of commands | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||