Group By Clause in MySQL




Hello and welcome everyone. In this article, we will learn about the Group By Clause in MySQL. Let’s go.

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/

The MySQL Group By clause is used to group rows values that have the same values or multiple records from collect data and group the result. And also is used in the SELECT statement as well as its uses with aggregate functions like count, sum, avg, max, and min.

Syntax:
         SELECT column_name_1, column_name_2, . . .
         FROM table_name
         WHERE condition
         GROUP BY column_name_1, column_name_2, . . .;
Here is
         A SELECT statement is a retrieved group by data.
         WHERE condition that must be fulfilled for the records and it’s optional.
         GROUP BY performs the grouping on column_name_1 and column_name_2.


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
Example 1:
I want to see what is employee sex or gender
         SELECT * FROM employee
         GROUP BY e_sex;

Example 2:
I want to retrieve the number of employee in each employee city
         SELECT COUNT (e_id), e_city
         FROM employee
         GROUP BY e_city;

Example 3:
I want to retrieve a number of employee from Fresno city.
         SELECT * FROM employee
         WHERE e_city = ‘Fresno’
         GROUP BY e_sex;

In this article, we learned about GROUP BY clause with an 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

Order By Clause in MySQL

MySQL Constraint – UNIQUE