SQL: Count No of Students Enrolled in Subjects

  • Thread starter chrisalviola
  • Start date
  • Tags
    Sql
In summary, the conversation is about coding a database system for a school and using SQL code to count the number of students enrolled in each subject. The solution suggested is to use a left join instead of an inner join to ensure all subjects are included in the output.
  • #1
chrisalviola
80
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
  • #2
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?
 
  • #3
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:
  • #4
wow it works tnks a lot:smile:
 
  • #5
Hi there,

To count the number of students enrolled in each subject in your database, you can use the SQL COUNT function. The syntax for this function is as follows:

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

In your case, the column you would like to count is the "NoStud" column. So your code would look something like this:

SELECT COUNT(NoStud)
FROM subjects
WHERE code = 'cs111';

This will give you the total number of students enrolled in the subject with the code 'cs111'. You can repeat this for each subject code to get the count for all subjects.

Alternatively, if you want to count the total number of students enrolled in all subjects, you can use the SUM function. The syntax for this function is as follows:

SELECT SUM(column_name)
FROM table_name
WHERE condition;

In your case, the column you would use is still "NoStud". So your code would look like this:

SELECT SUM(NoStud)
FROM subjects;

This will give you the total number of students enrolled in all subjects.

I hope this helps. Happy coding!
 

What is SQL?

SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases. It is commonly used for data analysis and management.

What is the purpose of counting the number of students enrolled in subjects using SQL?

The purpose of counting the number of students enrolled in subjects using SQL is to gather information about the enrollment trends in different subjects. This can help in making decisions about course offerings, curriculum changes, and resource allocation.

How do I count the number of students enrolled in a specific subject using SQL?

To count the number of students enrolled in a specific subject using SQL, you can use the COUNT function along with a WHERE clause to specify the subject you want to count. For example, the SQL query would be: SELECT COUNT(*) FROM students WHERE subject = 'Math'. This will return the total number of students enrolled in the subject 'Math'.

Can I count the number of students enrolled in multiple subjects at once using SQL?

Yes, you can count the number of students enrolled in multiple subjects at once using SQL. You can use the GROUP BY clause to group the results by subject, and then use the COUNT function to get the total number of students for each subject. For example, the SQL query would be: SELECT subject, COUNT(*) FROM students GROUP BY subject. This will return a list of subjects and the corresponding number of students enrolled in each subject.

Is there a way to filter the results by specific criteria while counting the number of students enrolled in subjects using SQL?

Yes, you can use the WHERE clause in your SQL query to filter the results by specific criteria, such as the student's grade level or enrollment status. This will allow you to get a more specific count of students enrolled in subjects based on your chosen criteria.

Similar threads

  • Programming and Computer Science
Replies
7
Views
429
  • STEM Academic Advising
Replies
12
Views
1K
  • Programming and Computer Science
Replies
29
Views
3K
  • STEM Academic Advising
Replies
10
Views
3K
  • STEM Academic Advising
Replies
4
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • STEM Educators and Teaching
7
Replies
233
Views
18K
  • Engineering and Comp Sci Homework Help
Replies
26
Views
4K
  • General Discussion
Replies
12
Views
1K
Back
Top