0

I am new with sql queries so dont know much

i have a table named registration

this table has the following structure

Student_ID int,
Course varchar(15),
Score int,
Semester varchar(15),
Discipline varchar(10),
Campus varchar(15),
Degree varchar(10),
Year int

it does not contain any primary key it has the data of students from 4 different campuses of the same university so student_id is repeated

i am required total number of students who have taken more then 5 courses in a particular semester

i hope i have made the question clear kindly help if any one can.

0

3 Answers 3

1

Use GROUP BY and HAVING COUNT to get all the students that have taken more than five courses:

SELECT student_id
FROM yourtable
WHERE Semester = ....
GROUP BY student_id
HAVING COUNT(DISTINCT Course) > 5

To get the number of students you can count the number of rows that query returns:

SELECT COUNT(*) AS total FROM
(
    SELECT student_id
    FROM yourtable
    WHERE Semester = ....
    GROUP BY student_id
    HAVING COUNT(DISTINCT Course) > 5
)
1
  • Thank You very much Mark this worked helped alot once again thank you very much my man Dec 25, 2012 at 23:36
0

To get the students who are registered for more than 5 courses you do this...

SELECT CAMPUS, STUDENT_ID
FROM REGISTRATION
WHERE SEMESTER = 'GIVEN SEMESTER'
GROUP BY CAMPUS, STUDENT_ID
HAVING COUNT(*) > 5;

To get the number of students who are registered for more than 5 courses you do this...

SELECT COUNT(*)
FROM REGISTRATION
WHERE SEMESTER = 'GIVEN SEMESTER'
GROUP BY CAMPUS, STUDENT_ID
HAVING COUNT(*) > 5;

You need to group by campus AND student id since student id is repeated for different campuses. Also, the primary key should be composite (campus, student_id, semester, year, course) if I understand you correctly.

1
  • Yes Waleed You did get my question right,i got the query its working thank you very much really appreciate your help. Dec 25, 2012 at 23:37
0
SELECT COUNT(DISTINCT(sub.Student_ID)) FROM
(
SELECT un.Student_ID, COUNT(*) FROM university un 
GROUP BY un.Student_ID, un.Semester
HAVING 
COUNT(un.Course)>5
) AS sub

I assume the table name is university

Not the answer you're looking for? Browse other questions tagged or ask your own question.