Student grades sample database for SQL Server
A ready-to-run SQL Server script with test scores, averages, letter grades and attendance for grades 9-12. Tested on SQL Server 2022 16.0.4325.4. Updated 19 November 2026.
Student grades database for SQL Server
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 SQL Server script: one table and 1,000 rows, with column types chosen for SQL Server, 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 SQL Server 2022 16.0.4325.4 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
sqlcmd
sqlcmd -S localhost -Q "CREATE DATABASE practice"
sqlcmd -S localhost -d practice -i students-sqlserver.sqlSSMS or Azure Data Studio
Create a database, open the .sql file in a new query window connected to it, and press Execute.
Tables and columns
students 1,000 rows
| Column | SQL Server type |
|---|---|
| student_id | NVARCHAR(10) |
| first_name | NVARCHAR(20) |
| last_name | NVARCHAR(20) |
| grade_level | INT |
| homeroom | NVARCHAR(10) |
| math_score | INT |
| reading_score | INT |
| science_score | INT |
| average_score | DECIMAL(7,2) |
| letter_grade | NVARCHAR(10) |
| attendance_pct | DECIMAL(7,2) |
Practice questions
answers from SQL Server 2022 16.0.4325.4
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(CAST(math_score AS DECIMAL(18,4))), 2) AS math, ROUND(AVG(CAST(reading_score AS DECIMAL(18,4))), 2) AS reading, ROUND(AVG(CAST(science_score AS DECIMAL(18,4))), 2) AS science
FROM students
GROUP BY grade_level
ORDER BY grade_level;AVG of an INT column returns an INT in SQL Server, so the column is cast to DECIMAL first.
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 TOP 5 student_id, first_name, last_name, homeroom, average_score
FROM students
ORDER BY average_score DESC, student_id;SELECT TOP n ... ORDER BY returns the first n rows; SQL Server has no LIMIT.
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(CAST(average_score AS DECIMAL(18,4))), 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 in SQL Server 2012 and later.
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(CAST(average_score AS DECIMAL(18,4))), 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 |
SQL Server differences used here
- AVG of an INT column returns an INT in SQL Server, so the column is cast to DECIMAL first.
- SELECT TOP n ... ORDER BY returns the first n rows; SQL Server has no LIMIT.
- RANK() and ROW_NUMBER() OVER (PARTITION BY ...) work in SQL Server 2012 and later.