Mysql Group By

Mysql Group By statement is conjecture term used with the aggregate functions to group the result-set by specified one or more columns.

Mysql Group By

Mysql Group By

     

Mysql Group By statement is conjecture term used with the aggregate functions to group the result-set by specified one or more columns.

Understand with Example

The Tutorial focuses you to understand an example from  'Mysql Group By'. To grasp this example we help you by giving an elaborate example that include create table keyword that is used to create a table 'Employee' with required fieldname and datatype respectively.

 

 

 

Query for creating table name Employee:

mysql> CREATE TABLE Employee(
    ->              Empid int(10),
    ->              Empname varchar(60),
    ->              Salary int(90),
    ->              DOB date
    ->            );
Query OK, 0 rows affected (0.01 sec)

Query for Multiple insertion of data in table:

The Query insert into add the records or values to the  table 'Employee'.

mysql>     INSERT INTO Employee (Empid,Empname,Salary,DOB) VALUES
    ->     (01,'Habib',2014,20041202),
    ->     (02,'Karan',4021,20030411),
    ->     (03,'Samia', 22,20080223),
    ->     (04,'Hui Ling', 25,20081015),
    ->     (05,'Yumie', 29,19990126);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

Query to view data inserted in table:

Now to view the records or rows from table we use select query.

mysql> select * from employee;

Output:-

+-------+----------+--------+------------+
| Empid | Empname  | Salary | DOB        |
+-------+----------+--------+------------+
| 1     | Habib    | 2014   | 2004-12-02 |
| 2     | Karan    | 4021   | 2003-04-11 |
| 3     | Samia    | 22     | 2008-02-23 |
| 4     | Hui Ling | 25     | 2008-10-15 |
| 5     | Yumie    | 29     | 1999-01-26 |
+-------+----------+--------+------------+
5 rows in set (0.00 sec)

Query for viewing data using group by clause:

The Query group by is used to return the selected records from 'employee' and group the result set by specified column 'empname'.

mysql> select * from employee group by empname;

Output:-

+-------+----------+--------+------------+
| Empid | Empname  | Salary | DOB        |
+-------+----------+--------+------------+
| 1     | Habib    | 2014   | 2004-12-02 |
| 4     | Hui Ling | 25     | 2008-10-15 |
| 2     | Karan    | 4021   | 2003-04-11 |
| 3     | Samia    | 22     | 2008-02-23 |
| 5     | Yumie    | 29     | 1999-01-26 |
+-------+----------+--------+------------+
5 rows in set (0.00 sec)