codingstreets
Search
Close this search box.
get-started-sql

Get Started: SQL GROUP BY Statement

In This Article, You Will Know About SQL GROUP BY Statement

SQL GROUP BY – Before moving ahead, let’s know a bit about SQL Self Join.

Table of Contents

SQL GROUP BY

The SQL GROUP BY statement is used to group the rows that have the same values in rows.

The SQL GROUP BY statement is often used with functions like COUNT(), MAX(), MIN(), SUM(), AVG() to group the result by columns.

Syntax

				
					SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
				
			

Example Database

Table name: Data_1

S.No

Name

Class

Section

1

Alex

5

A

2

Rolex

6

B

3

Salex

7

B

4

Salex

8

D

5

Alex

9

E

SQL GROUP BY Examples

Example: Use the GROUP BY statement to group the result.

				
					SELECT COUNT(S.No), Name
FROM Data_1
GROUP BY Name;
				
			

Example: Use the GROUP BY statement to group the result.

				
					
SELECT COUNT(S.No), Name
FROM Data_1
GROUP BY Name
ORDER BY COUNT(S.No) DESC;
				
			

Example Database

Table name: Data_1

S.No

Name

Class

1

Alex

5

2

Rolex

6

2

Salex

7

3

Jalex

8

3

Kalex

9

Table name: Data_2

S.No

Roll_No

Class

6

451

5

7

452

6

7

453

7

8

454

8

8

455

9

GROUP BY With JOIN Example

Example: Use the GROUP BY statement with JOIN to group the result.

				
					SELECT Data_1.Name, COUNT(S.No) AS Number FROM Data_2
RIGHT JOIN Data_1 ON Data_1.Class = Data_2.Class
ORDER BY Name;

				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on