Sorting a field by unique values of another field in the same table

Hello Bubble Community, I’m Kojo from Ghana and new to Bubble and I’m using it presently to work on student management for an examination web application. I’ve got a sorting-ish problem and would appreciate any help.

I want to be able to query my database to show all Users that are Enrolled in a specific subject and show the result in a group, repeating group or some visible element.

Details are as follows:

I have 3 tables detailed below (PK-Primary Key, FK-Foreign Key) :

User - UserID (PK) , First Name, Last Name, Email
Subject - SubjectID (PK) , Subject Name
Enrol - EnrolID (PK) , Subject ID (FK) , UserID (FK)

User and Subject tables stores students and subject info respectively.
The Enrol table stores only the the subject id and student id of the User and Subjects table.
This is so that one student can enrol in multiple subjects/courses.
The Enrol table is basically a composite entity for the User and Subject Tables

I want to be able to query my database to show all Users that are Enrolled in a specific subject and show the result in a group, repeating group or some visible element.

For example, I’d like to see all students presently taking Mathematics in a visible element. I can’t quite figure it out so far and would appreciate some help.

So far I’ve created a dropdown that shows the unique subjects. I made the dropdown choices dynamic, set the type of choice as text, and choices source as do a search for enrollments’s subject’s subject name:unique elements

This (I suppose) allows me to retrieve the unique subjects in the enrol table.

I would like the User to be able to click on the text option in the drop down and see all unique students taking that subject in a group, repeating group or visible element.

Can someone please guide me on how to go about this? (I’ve read that Bubble doesn’t presently support sorting by foreign keys) but i think this use case is a bit different. Any help would be appreciated. Thanks in advance.

You need to create a relational database structure…

Datatype1 = students
datatype2 = courses

On the datatype2 have a list of students which is referencing your datatype1

Then in a repeating group that shows the courses have a repeating group in side that shows the list of students for that course

or create a function so the user can select the course from a list and a new repeating group is shown with a list of the students enrolled…your student list repeating group will have a datasource connected to their choice of course.

To do that you may want to use a custom state to store the course the user selects; call it “state_course_selected” then you list of students repeating group will be a data type of students and a datasource would be
do a search for courses with a constraint of course name = “state_course_selected’s name”

then you should have a list of all students enrolled in that course