SQL: Count No of Students Enrolled in Subjects

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

The discussion focuses on counting the number of students enrolled in subjects using MySQL. The original SQL query provided by the user only returned subjects with enrolled students, omitting those without any enrollments. The solution offered involves using a LEFT JOIN instead of an INNER JOIN, allowing all subjects to be listed regardless of student enrollment. The corrected SQL query is: SELECT s.*, count(distinct e.studid) NoStud FROM subjects s LEFT JOIN enrolled e on e.subjid = s.idSubjects GROUP BY e.subjid ASC.

PREREQUISITES
  • Understanding of MySQL 8.0 syntax and functions
  • Knowledge of SQL JOIN types, specifically LEFT JOIN and INNER JOIN
  • Familiarity with GROUP BY clause in SQL
  • Basic database design concepts, including tables and relationships
NEXT STEPS
  • Learn more about MySQL LEFT JOIN syntax and use cases
  • Explore advanced SQL aggregation functions in MySQL
  • Study the implications of using DISTINCT in SQL queries
  • Investigate database normalization principles to optimize table structures
USEFUL FOR

Database developers, SQL learners, and anyone involved in managing student enrollment systems will benefit from this discussion.

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:
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

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