The best SQL practice for beginners is a small set of questions on one database, each a little harder than the last. Here are 10 exercises with answers, using the student records of the fictional Northbridge University in Port Alder.
Try each exercise before you read the answer. The results below come from real runs on the database, so you can check your own output against them.
What does the database look like?
You need only these tables and columns:
student:id,first_name,last_name,department_id,year,city,email,has_scholarship,gpadepartment:id,name,facultyenrollment:id,student_id,offering_id
There are 120 students. Some have no email and some have no GPA yet. Those missing values appear in two of the exercises.
Which exercises start with SELECT and WHERE?
1. How many students are there?
SELECT COUNT(*) AS students FROM student;
Answer: 120.
2. List the five students with the highest GPA, at least 3.8. Break ties by last name.
SELECT first_name, last_name, gpa FROM student WHERE gpa >= 3.8 ORDER BY gpa DESC, last_name LIMIT 5;
| first_name | last_name | gpa |
|---|---|---|
| Noor | García | 4.00 |
| Omar | Kim | 4.00 |
| Camila | Osei | 3.88 |
| Anika | Rivera | 3.88 |
| Zara | Larsen | 3.85 |
3. How many students have a scholarship?
SELECT COUNT(*) AS scholarship_students FROM student WHERE has_scholarship;
Answer: 28.
4. How many students have no email address?
SELECT COUNT(*) AS no_email FROM student WHERE email IS NULL;
Answer: 9. Remember that = NULL never matches anything. Use IS NULL.
Which exercises use GROUP BY?
5. Which three cities have the most students?
SELECT city, COUNT(*) AS students FROM student GROUP BY city ORDER BY students DESC, city LIMIT 3;
| city | students |
|---|---|
| Port Alder | 45 |
| Lisbon | 9 |
| Seoul | 9 |
6. How many students are in each year?
SELECT year, COUNT(*) AS students FROM student GROUP BY year ORDER BY year;
| year | students |
|---|---|
| 1 | 29 |
| 2 | 35 |
| 3 | 35 |
| 4 | 21 |
7. What is the average GPA in each department?
SELECT department_id, ROUND(AVG(gpa), 2) AS avg_gpa FROM student GROUP BY department_id ORDER BY department_id;
| department_id | avg_gpa |
|---|---|
| 1 | 2.48 |
| 2 | 2.44 |
| 3 | 2.80 |
| 4 | 2.15 |
| 5 | 2.70 |
| 6 | 2.25 |
| 7 | 2.50 |
AVG ignores students with no GPA. In this data, 32 students have none, so the average covers only the others.
Which exercises use JOIN and HAVING?
8. Show the first three students with their department names.
SELECT s.first_name, s.last_name, d.name AS department FROM student s JOIN department d ON d.id = s.department_id ORDER BY s.id LIMIT 3;
| first_name | last_name | department |
|---|---|---|
| Anika | Park | Industrial Engineering |
| Anika | Rivera | Management Information Systems |
| Felix | Abara | Business Administration |
9. Which departments have more than 20 students?
SELECT d.name, COUNT(*) AS students FROM student s JOIN department d ON d.id = s.department_id GROUP BY d.name HAVING COUNT(*) > 20 ORDER BY students DESC;
| name | students |
|---|---|
| Business Administration | 24 |
| Psychology | 23 |
10. How many students are not enrolled in any course?
SELECT COUNT(*) AS not_enrolled FROM student s WHERE NOT EXISTS ( SELECT 1 FROM enrollment e WHERE e.student_id = s.id );
Answer: 3. The subquery looks for matching enrollments, and NOT EXISTS keeps the students who have none.
How should you practice these?
1. Cover the answer and write the query yourself.
2. Run it. If you get an error, read the message. It usually names the problem.
3. Compare with the answer. A different query with the same result is fine.
4. Change one thing and predict the result before you run it.
If you get stuck on exercises 7 to 9, the fix is usually the order in which SQL runs a query. The lesson on [execution order and common errors](/learn/sql/execution-order-errors) explains it.
What to read next
- [SQL course: 41 short lessons with exercises, free](/courses/sql)
- [GROUP BY lesson](/learn/sql/group-by)
- [WHERE vs HAVING in SQL](/blog/sql-where-vs-having)
- [SQL joins explained](/blog/sql-join-types)
- #sql
- #sql practice
- #exercises
- #beginners