Having clause in MySQL




Hello and welcome everyone. In this article, we will learn about the HAVING clause in MySQL. Let’s go.
MySQL HAVING clause is used with the GROUP BY clause to filter groups on a specified condition and it’s also used in the SELECT statement to filter conditions for a group of aggregates or rows. Condition is TRUE then it always returned the rows. The WHERE clause is not used with aggregate functions, therefore HAVING clause added in the MySQL.

MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

Syntax
         SELECT column_name_1, column_name_2, aggregate_function
         FROM table_name
         WHERE condition
         GROUP BY column_name
         HAVING condition
         ORDER BY column_name;
Here is
SELECT – retrieve the specific condition result from the table.
column_name_1 – which columns you want to display in result.
aggregate_function – you can use aggregate functions like SUM, COUNT, MIN, MAX and AVG function that are present in MySQL.
table_name – which table data you want to see.
WHERE condition – it is the records to be selected from the condition.
GROUP BY – return rows by groups.
HAVING condition – it restrict the groups of returned rows and also it shows only groups in result set which conditions are TRUE.
ORDER BY – it showing result ascending or descending order.
For Example:
Here is the, we will create a table for an employee in the database.

CREATE TABLE IF NOT EXISTS employee (
                   e_id int (10) PRIMARY KEY,
                   e_firstname varchar (50) NOT NULL,
                   e_lastname varchar (50) NOT NULL,
                   e_age int (10),
                   e_city varchar (250)
e_sex char (1)
e_phnumber int (15) NOT NULL
)

Here we insert record in the employee table.
INSERT INTO employee (
  ‘e_id’, ’e_firstname’, ’e_lastname’,
  ’e_age’, ’e_city’, ’e_sex’,
  ’e_phnumber’
)
VALUES
  (
    1, ‘Hone’, ‘Reus’, 26, ‘San Jose’, ‘M’, ‘ + 1 555 555 1111’
  ),
  (
    2, ‘Cries’, ’Fence’, 23, ’Fresno’, ’F’, ’ +1 555 555 1111’
  );

Final Result
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’  ’e_sex’ ’e_phnumber’
  1         Hone              Reus              26      San Jose    M       +1 555 555 1111
  2         Cries              Fence             23      Fresno       F        +1 555 555 1111

So now I want to display the employee list that is greater than 25 ages.

          SELECT e_firstname as Fname, e_lastname as Lname, e_age as Age, e_sex as Sex
          FROM employee
          GROUP BY e_sex
          HAVING e_age > 25;

In this article, we learned about HAVING clause in MySQL with example.




MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/


Comments

Popular posts from this blog

ALTER statement in MySQL

MySQL Constraint – PRIMARY KEY

Order By Clause in MySQL