Skip to content
Advertisement

How to read double signups for a class in two separate databases without subqueries? SQL

I am using MySQL for an intro to databases course. We are currently practicing using aggregations and joins to create new relevant columns. I have a class table that contains information about classes such as the class id, name, code, etc.

I also have a ClassStudent table that shows all students that are signed up for a class as well as sign up times and class codes.

The task at hand is to display the how may students have signed up for each class twice, where applicable. If you look at the table above, you can see that there are students with IDs 2, 3, and 4, that have signed up for class number 5, 3 times. So there should be an additional column called DoubleSignUps.

Using the following query I have added a few columns to answer other parts of the question that is being asked, however I can’t figure out the last column.

Any help/explanation as to how I can do this would be greatly appreciated. All I need to do is for every ClassID, there should only be one StudentID associated, and any extra would add to a double sign up count.

EDIT:

Here is the database that I am expecting to see

NOTE: My query above will also display a Earliest Signup column,Latest Signup, and Student Count, however there is not enough room in the window to add them.

Advertisement

Answer

You need a subquery that groups by ClassID and StudentID to get duplicate signups.

You can then group this by ClassID to get the number of double signups for each class:

Then you can join this with your original query.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement