Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL for finding the counts per user

Lets say I have the following table:

Student      Course           University
1             a                   x
1             b                   x
1             c                   x
1             a                   y
2             a                   x
2             a                   y
2             a                   z
3             a                   x

For each student, I am trying to find the number of unique courses and universities that they are enrolled in.

The output would be as follows:

Student     No. of Courses        No. of Universities
 1             3                         2
 2             1                         3
 3             1                         1

How would I construct the SQL for this?

like image 728
rekha Avatar asked Dec 16 '22 15:12

rekha


1 Answers

SELECT Student,
       COUNT(DISTINCT Course)     AS NumberOfCourses,
       COUNT(DISTINCT University) AS NumberOfUniversities
FROM   YourTable
GROUP  BY Student 
like image 151
Martin Smith Avatar answered Dec 27 '22 13:12

Martin Smith