Write a sql query that prints domain interest and number students who
are interested in that domain.+------+----------+----------+ | sno | sub1 | sub2 | +------+----------+----------+ | s1 | ds | networks | | s2 | os | ds | | s3 | ds | os | | s4 | networks | db | | s5 | os | networks | | s6 | db | ds | | s7 | networks | os | | s8 | db | os | +------+----------+----------+
Expected output:
+----------+-------------+ | sub1 |Totalstudents| +----------+-------------+ | db | 3 | | ds | 4 | | networks | 4 | | ob | 5 | +----------+-------------+
This is what he/she has tried:
select sub1, count(sub1)
from student
group by sub1;
then tried subquery as
select a.sub1
, count(a.sub1)
,count(b.sub1)
from student a, student b
where a.sub1=b.sub2
group by a.sub1, b.sub2;
2
Answers
You could utilize
UNION ALL
andGROUP BY
:Output:
Here’s my take on this, obviously more clumsy than UNION ALL (I’m learning SQL, so any comments are more than welcome).