Question:
SQL QUERY involving GROUP BY?
Visual Treat
2013-01-22 07:49:50 UTC
How can the result, which is displayed using a 'group by' clause, be displayed without using it?

example:
To count the number of gold medals with the students in BLUE team and RED team and show it separately.
-Select team, sum(gold) from student where team='BLUE' or team='RED' group by team.

or
To count the number of gold medals with the students in various team and show it team wise.
-Select team, sum(gold) from student group by team.

now both the queries to be executed without using 'group by'. How?
Four answers:
Jignesh
2013-01-22 07:52:02 UTC
you can use order by to get the same result.
2016-12-11 09:04:05 UTC
this could artwork: decide on a.student_id, a.student_name, MAX(b.mark) AS maxmark, c.subject_name FROM student a inner connect Student_Subject b ON a.student_id = b.student_id inner connect situation c ON c.subject_id = b.subject_id team by skill of a.student_id, a.student_name, b.mark, c.subject_name, b.student_id, c.subject_id, b.subject_id in case you're applying an till now version of MySQL, you are able to no longer use inner JOINs. extremely, you utilize LEFT connect and exclude nulls from the impressive tables: decide on a.student_id, a.student_name, MAX(b.mark) AS maxmark, c.subject_name FROM student a LEFT connect Student_Subject b ON a.student_id = b.student_id LEFT connect situation c ON c.subject_id = b.subject_id HAVING b.mark isn't NULL and c.subject_name isn't NULL team by skill of a.student_id, a.student_name, b.mark, c.subject_name, b.student_id, c.subject_id, b.subject_id
Serge M
2013-01-22 11:44:35 UTC
select team, sum(gold) from student where team='BLUE'

union all

select team, sum(gold) from student where team='RED'
Ray
2013-01-22 11:35:55 UTC
select

s.team,

(select count(gold) from student t

where s.team = t.team) as team_count

from student s;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...