Welcome to the final module of Introduction to SQL for Data Analysis! In the previous modules, you learned how databases work and how to use SQL to retrieve, filter, sort, group and combine information.
In this module, you will bring everything together by completing a practical data analysis project. You will act like a data analyst who has been asked to study students' examination results and help a school understand its academic performance.
By the end of this module, you should be able to:
Imagine you work as a junior data analyst at a secondary school in Ibadan. The school management wants to understand how students performed in their examinations.
They want answers to questions such as:
Your job is to use SQL to find the answers.
First, create a database called SchoolAnalysis. A database is a digital storage location where related information is organised.
CREATE DATABASE SchoolAnalysis;
USE SchoolAnalysis;
The first command creates the database. The second command selects it so that you can work inside it.
Our project will use two tables: Students and Results.
This table stores information about students.
CREATE TABLE Students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
student_name VARCHAR(100),
class VARCHAR(20),
state VARCHAR(50)
);
This table stores students' examination scores.
CREATE TABLE Results (
result_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
subject VARCHAR(50),
score INT,
FOREIGN KEY (student_id)
REFERENCES Students(student_id)
);
Notice that student_id appears in both tables. It connects each examination result to the correct student. This is an example of a relationship between two database tables.
Now let's add some students to the Students table.
INSERT INTO Students
(student_name, class, state)
VALUES
('Samuel', 'SS2', 'Oyo'),
('Mary', 'SS2', 'Lagos'),
('Peter', 'SS2', 'Ogun'),
('Esther', 'SS2', 'Oyo'),
('David', 'SS2', 'Osun');
Next, insert their examination results.
INSERT INTO Results
(student_id, subject, score)
VALUES
(1, 'Mathematics', 85),
(1, 'English', 78),
(2, 'Mathematics', 92),
(2, 'English', 88),
(3, 'Mathematics', 45),
(3, 'English', 55),
(4, 'Mathematics', 75),
(4, 'English', 80),
(5, 'Mathematics', 60),
(5, 'English', 70);
You now have sample student and examination data to analyse.
The school principal wants to see all registered students. Use SELECT to retrieve the records.
SELECT * FROM Students;
The asterisk (*) tells SQL to display all the columns in the table.
To display only students' names and classes, use:
SELECT student_name, class
FROM Students;
Suppose the principal wants to see students who scored at least 80 marks in Mathematics.
SELECT *
FROM Results
WHERE subject = 'Mathematics'
AND score >= 80;
To find students who scored below 50 in any subject:
SELECT *
FROM Results
WHERE score < 50;
WHERE helps you select only the records that meet a particular condition.
The school wants to arrange Mathematics scores from the highest to the lowest.
SELECT *
FROM Results
WHERE subject = 'Mathematics'
ORDER BY score DESC;
DESC means descending order. It displays the highest score first.
To display the lowest score first, use ASC:
SELECT *
FROM Results
WHERE subject = 'Mathematics'
ORDER BY score ASC;
SQL has special functions that help analysts perform calculations. Let's use some of them.
SELECT COUNT(*) AS total_students
FROM Students;
COUNT(*) counts the number of records in the table.
SELECT AVG(score) AS average_score
FROM Results;
AVG() calculates the average of numerical values.
SELECT MAX(score) AS highest_score
FROM Results;
SELECT MIN(score) AS lowest_score
FROM Results;
SELECT SUM(score) AS total_scores
FROM Results;
The principal wants to know the average score for each subject. We can use GROUP BY to organise the results by subject.
SELECT
subject,
AVG(score) AS average_score
FROM Results
GROUP BY subject;
This query calculates a separate average for Mathematics and English.
To count how many students took each subject:
SELECT
subject,
COUNT(*) AS number_of_students
FROM Results
GROUP BY subject;
What if the principal wants to see only subjects with an average score of at least 70?
SELECT
subject,
AVG(score) AS average_score
FROM Results
GROUP BY subject
HAVING AVG(score) >= 70;
Remember the difference:
The Results table contains student IDs instead of student names. The principal wants to see each student's name alongside their scores.
We can use INNER JOIN to combine the two tables.
SELECT
Students.student_name,
Results.subject,
Results.score
FROM Students
INNER JOIN Results
ON Students.student_id = Results.student_id;
This query connects each student to their examination results. It displays the student's name, subject and score together.
Now let's use SQL to answer some of the questions asked by the school management.
SELECT
Students.student_name,
Results.score
FROM Students
INNER JOIN Results
ON Students.student_id = Results.student_id
WHERE Results.subject = 'Mathematics'
ORDER BY Results.score DESC
LIMIT 1;
SELECT
Students.student_name,
Results.subject,
Results.score
FROM Students
INNER JOIN Results
ON Students.student_id = Results.student_id
WHERE Results.score < 50;
SELECT AVG(score) AS english_average
FROM Results
WHERE subject = 'English';
SELECT COUNT(*) AS students_with_50_or_more
FROM Results
WHERE subject = 'Mathematics'
AND score >= 50;
SELECT
Students.student_name,
SUM(Results.score) AS total_score
FROM Students
INNER JOIN Results
ON Students.student_id = Results.student_id
GROUP BY Students.student_id, Students.student_name
ORDER BY total_score DESC;
A good data analyst does more than write SQL queries. They also explain what the results mean.
For example:
These findings can help teachers and school managers make better decisions. However, examination scores alone do not tell the complete story of a student's ability or progress.
It is time to practise. Use the SchoolAnalysis database to complete the following tasks.
Answer these questions to test your understanding.
Congratulations! You have completed all eight modules of Introduction to SQL for Data Analysis.
Here is a recap of what you have learned:
SQL is an important skill for people interested in data analysis, business intelligence and database management.
To improve your skills, continue learning:
Imagine that a school gives you examination records for 500 students. Your task is to use SQL to prepare a report containing:
Complete the project using MySQL Workbench or phpMyAdmin. Save your SQL queries and explain your findings in a short report.
You have taken your first major step towards becoming a data analyst. Keep practising, work with real datasets and continue building your portfolio.
CRMNuggets โ Learn. Practice. Get Certified.