COMPUTER SCIENCE

SQL Joins, Group By and Subqueries Explained for College Coursework

Learn how INNER JOIN, LEFT JOIN, GROUP BY, HAVING and subqueries work in SQL database assignments, with clear examples and common pitfalls.

Student learning resource7 min read

In college database and MIS courses, SQL assignments quickly move past simple SELECT statements into multi-table queries. Understanding the order in which SQL executes a query makes JOINs, aggregations and filtering much easier to write.

INNER JOIN vs. LEFT JOIN: when rows disappear

An INNER JOIN keeps only rows that have a match in both tables. If an assignment asks for 'all students and the number of courses they enrolled in, including students with zero enrollments,' an INNER JOIN will accidentally drop the students with zero enrollments.

Use a LEFT JOIN from the students table to the enrollments table and count the foreign key column rather than COUNT(*) so zero-enrollment rows return 0 instead of 1.

SELECT s.student_id, s.name, COUNT(e.course_id) AS course_count
FROM students s
LEFT JOIN enrollments e ON s.student_id = e.student_id
GROUP BY s.student_id, s.name;

WHERE vs. HAVING: filtering before or after aggregation

WHERE filters individual rows before GROUP BY groups them. HAVING filters the grouped results after aggregation functions like COUNT, SUM or AVG have been calculated.

  • Use WHERE for conditions on raw columns (for example, WHERE semester = 'Fall 2026').
  • Use HAVING for conditions on aggregates (for example, HAVING AVG(score) >= 85).
  • Include every non-aggregated SELECT column in your GROUP BY clause.

Need help with a SQL or database assignment?

If your query returns duplicate rows or your ER diagram needs normalization to 3NF, visit our [SQL & Database Assignment Help](https://www.getsetgrades.com/services/sql-assignment-help) page for one-to-one tutoring.

KEEP LEARNING

Understanding the concept is the first step.

Explore related resources or learn about subject-focused tutoring designed to help you understand the material.