MariaDB HAVING Clause
Objectives
-
Understanding of MariaDB HAVING Clause
-
MariaDB HAVING Clause
Create database and table and insert data if it is not created already
--- Create database if not exist
CREATE DATABASE IF NOT EXISTS mariadb_tutorial;
--- Select the database for further operation
USE mariadb_tutorial;
--- Create table if not exist
CREATE TABLE IF NOT EXISTS person (
id int(12) NOT NULL AUTO_INCREMENT,
first_name varchar(150) NOT NULL,
last_name varchar(150),
email varchar(100),
age int,
income double,
PRIMARY KEY (id)
)
--- Remove old Data
TRUNCATE person;
--- Insert data into table if not exist
INSERT INTO person (first_name, last_name, email, age, income)
VALUES
('Faiyaz', 'Mia', 'faiyaz@pf.local', 10, 5000),
('John', 'Doe', 'john@pf.local', 19, 100),
('Tahsin', NULL, 'tahsin@pf.local', 10, 150),
('Jane', 'Doe', 'jane@gmail.com', 10, 300),
('Rakib', 'Mia', 'rakib@bf.local', 24, 200),
('Sagor', 'Sowrov', 'email10@bf.local', 20, 250),
('Touhid', NULL, 'hmtmcse.com@gmail.com', 30, 500);
Understanding of MariaDB HAVING Clause
In general WHERE keyword cannot be used aggregate functions. The HAVING clause behaves like the WHERE clause and can use aggregate functions. HAVING we mostly use with GROUP BY
MariaDB HAVING Clause
Syntax
SELECT column_name(s) FROM table_name HAVING condition;
Example
SELECT age, COUNT(age) AS total FROM person GROUP BY age HAVING COUNT(age) >= 2;
Output
+------+-------+
| age | total |
+------+-------+
| 10 | 3 |
+------+-------+
1 row in set (0.097 sec)