0
Can anyone explain GROUP BY and HAVING commands sql with an examples
8 Respuestas
+ 6
Learn SQL from sololearn
https://www.sololearn.com/course/SQL
+ 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
+ 3
Sakshi you are right, the group by is covered in the Intermediate SQL course, I haven't seen it previously :)
+ 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.
+ 2
Tnq Anushka
+ 1
Tibor Santa Thank you
+ 1
Tnq Sakshi