SQL: Count No of Students Enrolled in Subjects

  • Thread starter Thread starter chrisalviola
  • Start date Start date
  • Tags Tags
    Sql
Click For Summary

Discussion Overview

The discussion revolves around SQL queries for counting the number of students enrolled in various subjects within a database system for a school. It addresses the challenge of ensuring that subjects with no enrolled students still appear in the output.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant seeks SQL code to count the number of students enrolled in each subject, providing a sample output format.
  • Another participant shares an SQL query but notes that subjects with no enrolled students do not appear in the results.
  • A third participant explains that the issue arises from using an inner join, suggesting that a left join would include all subjects regardless of student enrollment.
  • The suggested SQL query using a left join is provided, which counts distinct student IDs and groups by subject ID.
  • A later reply confirms that the suggested solution works effectively.

Areas of Agreement / Disagreement

Participants generally agree on the need for a left join to include all subjects in the output, and the proposed solution is confirmed to work. However, there is no explicit discussion of alternative methods or further refinements to the query.

Contextual Notes

The discussion does not address potential limitations of the SQL queries, such as performance implications or specific database configurations that might affect the results.

Who May Find This Useful

Database developers, students learning SQL, and individuals interested in database management systems may find this discussion relevant.

chrisalviola
Messages
80
Reaction score
0
Iam currently coding a database system for a school here, currently this is my database in MYSQL
db1.jpg


I want to know what SQL code to use to count how many students has enrolled in every subjects in the subjects table

the new relation would appear as

idsubjects--code---desc---units--sched---NoStud
1----------cs111--------------------------10
2----------cs201--------------------------20
3----------cs202--------------------------14
 
Technology news on Phys.org
I have here the SQL Query
SELECT subjects.*, count(distinct enrolled.studid) as nostud FROM subjects, enrolled where subjects.idsubjects = enrolled.subjid group by subjects.idsubjects

but the problem is if there's no students enrolled on that subject it won't appear on the relation, any solution to this?
 
Greetings chrisalviola,

It is in the way in which you are joining the tables. By default (inner) join only produces an output when there is a corresponding item in both tables. Whereas in this specific case, you are wanting a list of all subjects regardless if they have any students linking to them or not.

I believe for this given set-up a left-join would be better suited.

Code:
SELECT s.*, count(distinct e.studid) NoStud 
FROM subjects s
LEFT JOIN enrolled e on e.subjid = s.idSubjects
GROUP BY e.subjid ASC

Hope it helps,
-dem
 
Last edited:
wow it works tnks a lot:smile:
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
3
Views
12K
  • · Replies 76 ·
3
Replies
76
Views
7K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
29
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 26 ·
Replies
26
Views
5K