Unays Learn ยท SQL

SQL joins and GROUP BY: combine and summarise tables

Real databases spread information across several tables. This tutorial shows how to bring related rows together with joins and how to turn many rows into useful totals.

Two related tables

Instead of repeating a class name on every student, we store classes once and give each student a class_id that points to a class. This link is called a foreign key.

CREATE TABLE classes (id INTEGER PRIMARY KEY, name TEXT, teacher TEXT);
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, class_id INTEGER, score INTEGER);

INSERT INTO classes VALUES (1, '8A', 'Ms Hoxha'), (2, '8B', 'Mr Berisha'), (3, '9A', 'Ms Kola');
INSERT INTO students VALUES
  (1, 'Arta', 1, 92), (2, 'Dren', 2, 67), (3, 'Bora', 1, 78),
  (4, 'Leon', 2, 85), (5, 'Ina', 1, 54), (6, 'Erion', NULL, 71);

SELECT * FROM students;

Notice that class 9A has no students yet, and Erion has no class. These gaps matter for joins. If SELECT and WHERE are new to you, read SQL basics first.

INNER JOIN

CREATE TABLE classes (id INTEGER PRIMARY KEY, name TEXT, teacher TEXT);
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, class_id INTEGER, score INTEGER);
INSERT INTO classes VALUES (1, '8A', 'Ms Hoxha'), (2, '8B', 'Mr Berisha'), (3, '9A', 'Ms Kola');
INSERT INTO students VALUES (1, 'Arta', 1, 92), (2, 'Dren', 2, 67), (3, 'Bora', 1, 78), (4, 'Leon', 2, 85), (5, 'Ina', 1, 54), (6, 'Erion', NULL, 71);

SELECT students.name, classes.name AS class, classes.teacher
FROM students
INNER JOIN classes ON students.class_id = classes.id
ORDER BY class, students.name;

ON says how rows match. An inner join keeps only rows that have a match on both sides, so Erion (no class) and 9A (no students) do not appear. AS renames a column in the result.

LEFT JOIN

CREATE TABLE classes (id INTEGER PRIMARY KEY, name TEXT, teacher TEXT);
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, class_id INTEGER, score INTEGER);
INSERT INTO classes VALUES (1, '8A', 'Ms Hoxha'), (2, '8B', 'Mr Berisha'), (3, '9A', 'Ms Kola');
INSERT INTO students VALUES (1, 'Arta', 1, 92), (2, 'Dren', 2, 67), (3, 'Bora', 1, 78), (4, 'Leon', 2, 85), (5, 'Ina', 1, 54), (6, 'Erion', NULL, 71);

SELECT classes.name AS class, students.name AS student
FROM classes
LEFT JOIN students ON students.class_id = classes.id
ORDER BY class, student;

A left join keeps every row from the first (left) table, even without a match. Class 9A now appears with an empty (NULL) student. Use it to answer questions like "which classes have no students?" by adding WHERE students.id IS NULL.

GROUP BY and aggregates

CREATE TABLE classes (id INTEGER PRIMARY KEY, name TEXT, teacher TEXT);
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, class_id INTEGER, score INTEGER);
INSERT INTO classes VALUES (1, '8A', 'Ms Hoxha'), (2, '8B', 'Mr Berisha'), (3, '9A', 'Ms Kola');
INSERT INTO students VALUES (1, 'Arta', 1, 92), (2, 'Dren', 2, 67), (3, 'Bora', 1, 78), (4, 'Leon', 2, 85), (5, 'Ina', 1, 54), (6, 'Erion', NULL, 71);

SELECT classes.name AS class,
       COUNT(students.id) AS students,
       ROUND(AVG(students.score), 1) AS average,
       MAX(students.score) AS best
FROM classes
LEFT JOIN students ON students.class_id = classes.id
GROUP BY classes.id
ORDER BY class;

GROUP BY puts rows with the same class into one group, and aggregate functions (COUNT, AVG, MAX, MIN, SUM) turn each group into one number. COUNT(students.id) counts only real students, so 9A correctly shows 0.

Filtering groups with HAVING

CREATE TABLE classes (id INTEGER PRIMARY KEY, name TEXT, teacher TEXT);
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, class_id INTEGER, score INTEGER);
INSERT INTO classes VALUES (1, '8A', 'Ms Hoxha'), (2, '8B', 'Mr Berisha'), (3, '9A', 'Ms Kola');
INSERT INTO students VALUES (1, 'Arta', 1, 92), (2, 'Dren', 2, 67), (3, 'Bora', 1, 78), (4, 'Leon', 2, 85), (5, 'Ina', 1, 54), (6, 'Erion', NULL, 71);

SELECT classes.name AS class, COUNT(*) AS students
FROM students
JOIN classes ON students.class_id = classes.id
WHERE students.score >= 60
GROUP BY classes.id
HAVING COUNT(*) >= 2;

WHERE filters single rows before grouping; HAVING filters whole groups after grouping. This query lists classes with at least two students who scored 60 or more. (JOIN on its own means INNER JOIN.)

Try it yourself

In Unays Code, choose SQL, paste the two tables and write queries for: each teacher with the number of students they teach; students who are not in any class; and the class with the highest average score.

This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].