CSC 282 · Database Programming
Our notes
2 min readBasic form
SELECT column_list
FROM table
WHERE condition
GROUP BY columns
HAVING group_condition
ORDER BY columns;
The clauses run in the order FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
Choosing rows and columns
SELECT surname, level FROM student
WHERE dept_code = 'CSC' AND level >= 200
ORDER BY surname ASC;
Operators: =, <> (not equal), <, >, BETWEEN 40 AND 59, IN ('CSC', 'PHY'), LIKE 'Ok%' (% any characters, _ one character), IS NULL.
Aggregates
COUNT, SUM, AVG, MIN, MAX.
SELECT course_code, COUNT(*) AS students, AVG(score) AS mean_score
FROM result
WHERE session = '2025/2026'
GROUP BY course_code
HAVING COUNT(*) >= 2
ORDER BY mean_score DESC;
WHERE filters rows before grouping; HAVING filters groups after.
Worked example
Result rows for 2025/2026: (A, CSC 282, 70), (B, CSC 282, 55), (C, CSC 282, 64), (A, STA 260, 48), (B, STA 260, 72), (A, MTH 210, 80).
The query above returns:
| course_code | students | mean_score |
|---|---|---|
| CSC 282 | 3 | 63.0 |
| STA 260 | 2 | 60.0 |
MTH 210 has one student and is removed by HAVING. Check: (70 + 55 + 64)/3 = 63; (48 + 72)/2 = 60.
Joins
Combine tables on matching keys.
SELECT s.surname, r.course_code, r.score
FROM student s
JOIN result r ON r.matric_no = s.matric_no
WHERE r.score < 40;
- INNER JOIN: only rows that match in both tables.
- LEFT JOIN: every row of the left table, with NULLs where the right has no match: "all students and their results, including students with none".
- Joining three tables: chain the joins (student → result → course to show course titles).
Subqueries
SELECT surname FROM student
WHERE matric_no IN (SELECT matric_no FROM result WHERE score < 40);
SELECT course_code, score FROM result
WHERE score > (SELECT AVG(score) FROM result);
Exams usually ask
- Write a SELECT with WHERE, ORDER BY, GROUP BY and HAVING for a scenario.
- Write an inner and a left join and explain the difference.
- Predict the output of a query on a small table.
Common mistake
Selecting a column that is neither grouped nor aggregated (SELECT course_code, surname … GROUP BY course_code). Every selected column must be in GROUP BY or inside an aggregate.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Workplace Software and Skills, 13.3 Querying a Database
(opens openstax.org in a new tab)
OpenStax, Workplace Software and Skills, 13.3 Querying a Database, CC BY-NC-SA 4.0 · licence
-
Select (SQL)
(opens en.wikipedia.org in a new tab)
Wikipedia, Select (SQL), CC BY-SA 4.0 · licence
-
Join (SQL)
(opens en.wikipedia.org in a new tab)
Wikipedia, Join (SQL), CC BY-SA 4.0 · licence
-
NOUN CIT 304: Data Management
NOUN course book (PDF, about 6.9 MB: best on Wi-Fi). Files, databases, the relational model and SQL.
(opens nou.edu.ng in a new tab)
-
OpenStax, Workplace Software and Skills (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Workplace Software and Skills (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
ORDER BY in SQL
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
5 Basic SELECT Statement Queries in SQL
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
MySQL: How to SELECT data from a TABLE
5 min · ≈ 23 MB · about ₦13
(opens youtube.com in a new tab)
-
CS50 SQL - Lecture 0 - Querying
1 h 19 min · ≈ 356 MB · about ₦200
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.