DataBrighten

    Student grades sample database for PostgreSQL

    1,000rows · 1 table · 6 questions with answers

    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.

    textnumberdateyes/no

    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.sql

    pgAdmin or DBeaver

    Create a database, open the Query Tool, open the .sql file and run it.

    Tables and columns

    students 1,000 rows

    ColumnPostgreSQL type
    student_idVARCHAR(10)
    first_nameVARCHAR(20)
    last_nameVARCHAR(20)
    grade_levelINTEGER
    homeroomVARCHAR(10)
    math_scoreINTEGER
    reading_scoreINTEGER
    science_scoreINTEGER
    average_scoreDECIMAL(7,2)
    letter_gradeVARCHAR(10)
    attendance_pctDECIMAL(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_gradestudents
    A78
    B274
    C379
    D226
    F43
    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_levelmathreadingscience
    976.7575.9776.53
    1074.5474.6975.60
    1176.9676.4577.41
    1275.7375.3576.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_idfirst_namelast_namehomeroomaverage_score
    S20546AshleyWright10C100.00
    S20607SarahRobinson9A100.00
    S20898CarolThompson9A100.00
    S20891MichaelLopez11C99.70
    S20028StephanieCook11B98.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_levelhomeroomavg_scorernk
    99C76.861
    99A76.422
    99D76.333
    99B75.964
    1010B76.771
    1010D74.542
    1010C74.373
    1010A74.094
    1111D78.911
    1111A76.692
    1111C76.373
    1111B75.924
    1212C77.621
    1212B75.882

    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

    attendancestudentsavg_score
    90% or more86175.90
    under 90%13976.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.