Skip to content
Hands-on Mentor
ENTR
Back to blog

SQL practice for beginners: 10 exercises with answers

Ten SQL practice exercises for beginners with answers and real results, from SELECT to JOIN and HAVING. Try each one before you read the solution.

4 min read

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, gpa
  • department: id, name, faculty
  • enrollment: 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_namelast_namegpa
NoorGarcía4.00
OmarKim4.00
CamilaOsei3.88
AnikaRivera3.88
ZaraLarsen3.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;
citystudents
Port Alder45
Lisbon9
Seoul9

6. How many students are in each year?

SELECT year, COUNT(*) AS students
FROM student
GROUP BY year
ORDER BY year;
yearstudents
129
235
335
421

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_idavg_gpa
12.48
22.44
32.80
42.15
52.70
62.25
72.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_namelast_namedepartment
AnikaParkIndustrial Engineering
AnikaRiveraManagement Information Systems
FelixAbaraBusiness 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;
namestudents
Business Administration24
Psychology23

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
All posts