Sql Group By Clause Examples – Query Examples

Sql Group By Clause Examples

In this post we’ll write many example about group by clause on library database. Some of this examples will be easy,  and the others ones will be expert. With this examples you can learn how the group by clause used.

TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK

 

Sql Group By Clause Examples on Library Database

Example 1:  List the class names and student count of each class.

Result:

20 rows listed.

 

Example 2:  List the genders and student count of each gender

Result:

2 rows listed.

 

Example 3:  List the genders and student count of each gender

Result:

2 rows listed.

 

Example 4:  List the numbers of boys and girls in each class.

Result:

40 rows listed. There are 16 girls and 7 boys in 10A class.

 

Example 5:  List only the number of female students in each class.

Result:

20 rows listed.

 

Example 6:  List the class names and number of students which the number of students more than 30.

Result:

2 rows listed.

 

Example 7:  List the name and surname of the students and the number of books they read.

Not: We must add the studentId to group by clause because there may be the students whoes has same name and surname.

Solution 2 with join:

Result:

504 rows listed.

Example 8:  List the name and surname of the students and the number of books they read sorted by BookCount.

Solution 2 with join:

Result:

504 rows listed. Have you noticed. Students who have never read a book have not been listed. Let’s list them in the next example.

Example 9:  List the name and surname of the students and the number of books they read sorted by BookCount. Also list the students who have never read a book.

Not: We changed count(*) to count(BorrowId). If we don’t change, BookCount Column’s value in the first row will have been 1. It is an important difference.

Result:

505 rows listed. There is one student who have never read a book.

Example 10:

List the number of books read grouped by date quarter

Result Of Query

Example 11:

List the student count gruped by date quarter

Result Of Query

 

YOU MAY WANT TO SEE OUR ALL EXAMPLES PAGE, THEN CLICK HERE

About the author

admin

Leave a Comment

%d bloggers like this: