How do I write SQL queries for a student course registration database?
- Expert answer
- Undergraduate
- Asked
The question
My database assignment gives Students, Courses, Enrolments and Departments tables and asks me to write SQL queries for course registration reports.
I need to use joins, grouping and subqueries, but I get confused about which table to start from.
Short answer
For a student course registration database, start from the schema, identify keys and relationships, then write SQL queries using joins, filters, grouping and constraints. Explain what each query returns and why.
Full expert answer
Database systems tutor
MSc Data Science, Oracle certified
SQL assignments become much easier when you start from relationships. In a course registration system, students and courses usually have a many-to-many relationship, resolved by an Enrolments table. Most useful queries pass through that table.
Typical schema
textStudents(student_id, name, email)
Courses(course_id, title, department_id, credits)
Departments(department_id, department_name)
Enrolments(student_id, course_id, semester, grade)Primary keys identify rows. Foreign keys connect tables. In this design, Enrolments.student_id points to Students.student_id, and Enrolments.course_id points to Courses.course_id.
Query pattern
- 1Decide what the output should show.
- 2Identify which tables contain those columns.
- 3Join through the relationship keys.
- 4Add filters in
WHERE. - 5Use
GROUP BYonly when summarising. - 6Use
HAVINGfor conditions on grouped results.
Mini query examples
List students enrolled in Database Systems:
sqlSELECT s.student_id, s.name, c.title
FROM students s
JOIN enrolments e ON e.student_id = s.student_id
JOIN courses c ON c.course_id = e.course_id
WHERE c.title = 'Database Systems';Count enrolments per course:
sqlSELECT c.course_id, c.title, COUNT(*) AS enrolment_count
FROM courses c
JOIN enrolments e ON e.course_id = c.course_id
GROUP BY c.course_id, c.title;The second query groups by course because one output row should represent one course.
Sample university-style questions and how to answer them
| Sample question | What a strong answer should do |
|---|---|
| List all students enrolled in a particular course. | Join Students to Enrolments to Courses and filter by course ID or title. |
| Count how many students are enrolled in each course. | Group by course and use COUNT. Include course title in GROUP BY if selected. |
| Find students not enrolled in any course. | Use a LEFT JOIN from Students to Enrolments and filter where enrolment is null. |
| Show average grade by department. | Join Departments, Courses and Enrolments, then group by department. |
| Add constraints to prevent duplicate enrolment. | Explain composite primary key or unique constraint on student_id, course_id and semester. |
Common mistakes
- Joining tables without using the correct key
- Using
WHEREfor aggregate conditions that belong inHAVING - Selecting non-grouped columns in a grouped query
- Forgetting the Enrolments table in many-to-many relationships
- Using
COUNT(column)whenCOUNT(*)is clearer for counting rows - Not explaining what the query result means
Testing notes
Create small sample data and predict the result before running queries. For example, if Course A has three enrolment rows, your count query should return 3. If it returns 9, you probably created a duplicate-producing join.
Academic use note
This guide is for database assignment support. SQL dialects vary slightly, so follow your module's database system where syntax differs.
Sources and further reading
This answer explains a method for you to apply to your own work. Copying it into a submission would count as plagiarism, and it is indexed by similarity checkers.
All questions