Can anyone explain GROUP BY and HAVING commands sql with an examples | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain GROUP BY and HAVING commands sql with an examples

10th Apr 2023, 10:14 AM
Kethoji Manohar
Kethoji Manohar - avatar
7 Answers
+ 6
Learn SQL from sololearn https://www.sololearn.com/course/SQL
10th Apr 2023, 12:06 PM
Sakshi
Sakshi - avatar
+ 5
Tibor Santa hmm, I think you are right but maybe in intermediate course that mention, I don't know but I think because SQL intermediate course i don't learn yet, btw Kethoji Manohar sorry for that what I said earlier but you can search on google according to your understanding https://www.guru99.com/group-by.html Hope it's helpful to you
10th Apr 2023, 2:15 PM
Sakshi
Sakshi - avatar
+ 3
Sakshi you are right, the group by is covered in the Intermediate SQL course, I haven't seen it previously :)
10th Apr 2023, 7:02 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Sakshi this is not very helpful, actually these topics are not covered by the Sololearn course. GROUP BY is used in aggregate queries: when you don't just want to retrieve individual lines from the table, but combine all the results. As an example, assume you have the table "grades" with colums student, subject and grade. The grade is numeric, so we can do calculations on them. Like you can figure out what is the average grade of each student (considering all the subjects) like this: SELECT student, AVG(grade) FROM grades GROUP BY student; So AVG is an aggreagetion function here and you will have as many lines in the result, as there are unique students. HAVING must always follow the GROUP BY clause and you can use it to filter the calculated results, rather than filtering individual table rows with WHERE. SELECT subject, COUNT(*) FROM grades GROUP BY subject HAVING COUNT(*) > 2; This query shows how many grades were given per each subject, only when there were more than 2 grades.
10th Apr 2023, 1:01 PM
Tibor Santa
Tibor Santa - avatar
+ 2
12th Apr 2023, 10:15 AM
Kethoji Manohar
Kethoji Manohar - avatar
+ 1
Tibor Santa Thank you
10th Apr 2023, 1:04 PM
Kethoji Manohar
Kethoji Manohar - avatar
+ 1
Tnq Sakshi
12th Apr 2023, 10:14 AM
Kethoji Manohar
Kethoji Manohar - avatar