Student grades sample database for PostgreSQL
A ready-to-run PostgreSQL script with test scores, averages, letter grades and attendance for grades 9-12. Tested on PostgreSQL 15.18. Updated 19 November 2026.
Student grades database for PostgreSQL
Each row is one fictional high-school student: grade level, homeroom, three test scores, the average, a letter grade from fixed cut-offs and attendance percentage.
This page has it as a PostgreSQL script: one table and 1,000 rows, with column types chosen for PostgreSQL, primary keys. The script drops the table if it exists, creates it and inserts the rows in one transaction, so you can run it again to reset the data. It is the same fictional data, seed 42, as on the student grades dataset page, where it is also available as CSV and Excel.
We loaded this exact file into PostgreSQL 15.18 and ran every question below on it; the answers shown are that output. The same questions, written for SQLite, PostgreSQL, MySQL, SQL Server and DuckDB, return the same results on all five.
Load it
psql
createdb practice
psql -d practice -f students-postgresql.sqlpgAdmin or DBeaver
Create a database, open the Query Tool, open the .sql file and run it.
Tables and columns
students 1,000 rows
| Column | PostgreSQL type |
|---|---|
| student_id | VARCHAR(10) |
| first_name | VARCHAR(20) |
| last_name | VARCHAR(20) |
| grade_level | INTEGER |
| homeroom | VARCHAR(10) |
| math_score | INTEGER |
| reading_score | INTEGER |
| science_score | INTEGER |
| average_score | DECIMAL(7,2) |
| letter_grade | VARCHAR(10) |
| attendance_pct | DECIMAL(7,2) |
Practice questions
answers from PostgreSQL 15.18
How many students got each letter grade?
SQL
SELECT letter_grade, COUNT(*) AS students
FROM students
GROUP BY letter_grade
ORDER BY letter_grade;Result
| letter_grade | students |
|---|---|
| A | 78 |
| B | 274 |
| C | 379 |
| D | 226 |
| F | 43 |
What is the average score in each subject by grade level?
SQL
SELECT grade_level, ROUND(AVG(math_score), 2) AS math, ROUND(AVG(reading_score), 2) AS reading, ROUND(AVG(science_score), 2) AS science
FROM students
GROUP BY grade_level
ORDER BY grade_level;AVG of an integer column returns a numeric, so no cast is needed.
Result
| grade_level | math | reading | science |
|---|---|---|---|
| 9 | 76.75 | 75.97 | 76.53 |
| 10 | 74.54 | 74.69 | 75.60 |
| 11 | 76.96 | 76.45 | 77.41 |
| 12 | 75.73 | 75.35 | 76.06 |
Who are the top 5 students by average score?
SQL
SELECT student_id, first_name, last_name, homeroom, average_score
FROM students
ORDER BY average_score DESC, student_id
LIMIT 5;LIMIT n at the end returns the first n rows.
Result
| student_id | first_name | last_name | homeroom | average_score |
|---|---|---|---|---|
| S20546 | Ashley | Wright | 10C | 100.00 |
| S20607 | Sarah | Robinson | 9A | 100.00 |
| S20898 | Carol | Thompson | 9A | 100.00 |
| S20891 | Michael | Lopez | 11C | 99.70 |
| S20028 | Stephanie | Cook | 11B | 98.00 |
How many students scored 90 or more in all three subjects?
SQL
SELECT COUNT(*) AS students
FROM students
WHERE math_score >= 90 AND reading_score >= 90 AND science_score >= 90;Result
| students |
|---|
| 34 |
Rank homerooms by average score within each grade level.
SQL
SELECT grade_level, homeroom, ROUND(AVG(average_score), 2) AS avg_score,
RANK() OVER (PARTITION BY grade_level ORDER BY AVG(average_score) DESC) AS rnk
FROM students
GROUP BY grade_level, homeroom
ORDER BY grade_level, rnk, homeroom;RANK() and ROW_NUMBER() OVER (PARTITION BY ...) work on any supported version.
Result
| grade_level | homeroom | avg_score | rnk |
|---|---|---|---|
| 9 | 9C | 76.86 | 1 |
| 9 | 9A | 76.42 | 2 |
| 9 | 9D | 76.33 | 3 |
| 9 | 9B | 75.96 | 4 |
| 10 | 10B | 76.77 | 1 |
| 10 | 10D | 74.54 | 2 |
| 10 | 10C | 74.37 | 3 |
| 10 | 10A | 74.09 | 4 |
| 11 | 11D | 78.91 | 1 |
| 11 | 11A | 76.69 | 2 |
| 11 | 11C | 76.37 | 3 |
| 11 | 11B | 75.92 | 4 |
| 12 | 12C | 77.62 | 1 |
| 12 | 12B | 75.88 | 2 |
First 14 of 16 rows.
Do students with attendance under 90% score lower on average?
SQL
SELECT CASE WHEN attendance_pct < 90 THEN 'under 90%' ELSE '90% or more' END AS attendance,
COUNT(*) AS students, ROUND(AVG(average_score), 2) AS avg_score
FROM students
GROUP BY CASE WHEN attendance_pct < 90 THEN 'under 90%' ELSE '90% or more' END
ORDER BY attendance;Result
| attendance | students | avg_score |
|---|---|---|
| 90% or more | 861 | 75.90 |
| under 90% | 139 | 76.59 |
PostgreSQL differences used here
- AVG of an integer column returns a numeric, so no cast is needed.
- LIMIT n at the end returns the first n rows.
- RANK() and ROW_NUMBER() OVER (PARTITION BY ...) work on any supported version.