GROUP BY in SQL

Date Published: 19/07/2020 Published By: JaiSchool

GROUP BY clause is used to display strings (data) which is duplicate in a column. For example, if in an admission table 2 students are doing BCA then BCA will be two times in the course column. Now if we use GROUP BY then it will display BCA because it is 2 times in the course column.

Database- jai_school

CREATE DATABASE jai_school;

Create table- admission

CREATE TABLE admission(adm_no INT(10) NOT NULL AUTO_INCREMENT, student VARCHAR(50), course VARCHAR(30), PRIMARY KEY(adm_no));

INSERT ROWS

INSERT INTO admission(student,course)VALUES("Ram","MCA");
INSERT INTO admission(student,course)VALUES("Shyam","BCA");
INSERT INTO admission(student,course)VALUES("Raman","MCA");
INSERT INTO admission(student,course)VALUES("Ravina","BTECH");
INSERT INTO admission(student,course)VALUES("Ashok","MCA");
INSERT INTO admission(student,course)VALUES("Aman","BCA");
INSERT INTO admission(student,course)VALUES("Priya","BTECH");
INSERT INTO admission(student,course)VALUES("Rakesh","BCA");
INSERT INTO admission(student,course)VALUES("Anita","BCA");

Table Image -

table for sql group by

USE OF GROUP BY

SELECT course FROM admission GROUP BY course;

Query result~

sql group by result

Each duplicate data length in a column

With the help of count function, You are able to know how many students are in MCA, BCA, etc. Because it will count the lenght of each duplicate data.

SELECT COUNT(adm_no),course FROM admission GROUP BY course;

Query Result~

sql group by with count

Publish A Comment

Leave a Reply

Your email address will not be published. Required fields are marked *