Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to SQL for Data Analysis ยท Lesson 9 of 9

Module Eight

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
9
This is the final module of **Introduction to SQL for Data Analysis**. It focuses on practical SQL skills, using a real-world school database project to help learners practise what they have learned in all eight modules.

Module 8: Practical SQL for Data Analysis

Introduction

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.

1. Learning Objectives

By the end of this module, you should be able to:

  • Create a database for a practical project.
  • Create tables and insert sample records.
  • Retrieve information using SELECT.
  • Filter records using WHERE.
  • Sort records using ORDER BY.
  • Calculate totals, averages and counts.
  • Group information using GROUP BY.
  • Combine related tables using JOIN.
  • Answer real-world questions using SQL.
  • Interpret query results and communicate findings.

2. Understanding the Practical Project

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:

  • How many students wrote the examination?
  • Who scored the highest mark?
  • What is the average examination score?
  • Which subject has the highest average score?
  • How many students passed or failed?
  • Which students scored below 50 marks?

Your job is to use SQL to find the answers.

3. Step One: Create a Database

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.

4. Step Two: Create the Tables

Our project will use two tables: Students and Results.

Students Table

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)
);

Results Table

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.

5. Step Three: Insert Sample Data

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.

6. Step Four: Retrieve Data Using SELECT

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;

7. Step Five: Filter Data Using WHERE

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.

8. Step Six: Sort Data Using ORDER BY

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;

9. Step Seven: Calculate Results Using SQL Functions

SQL has special functions that help analysts perform calculations. Let's use some of them.

A. Count the Number of Students

SELECT COUNT(*) AS total_students
FROM Students;

COUNT(*) counts the number of records in the table.

B. Calculate the Average Score

SELECT AVG(score) AS average_score
FROM Results;

AVG() calculates the average of numerical values.

C. Find the Highest Score

SELECT MAX(score) AS highest_score
FROM Results;

D. Find the Lowest Score

SELECT MIN(score) AS lowest_score
FROM Results;

E. Calculate the Total of All Scores

SELECT SUM(score) AS total_scores
FROM Results;

10. Step Eight: Group Data Using GROUP BY

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;

11. Step Nine: Filter Groups Using HAVING

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:

  • WHERE filters individual records before grouping.
  • HAVING filters groups after GROUP BY.

12. Step Ten: Combine Tables Using JOIN

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.

13. Step Eleven: Answer Real-World Questions

Now let's use SQL to answer some of the questions asked by the school management.

Question 1: Who scored the highest mark in Mathematics?

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;

Question 2: Which students scored below 50?

SELECT
    Students.student_name,
    Results.subject,
    Results.score
FROM Students
INNER JOIN Results
ON Students.student_id = Results.student_id
WHERE Results.score < 50;

Question 3: What is the average score in English?

SELECT AVG(score) AS english_average
FROM Results
WHERE subject = 'English';

Question 4: How many students scored at least 50 in Mathematics?

SELECT COUNT(*) AS students_with_50_or_more
FROM Results
WHERE subject = 'Mathematics'
AND score >= 50;

Question 5: Show each student's total score across both subjects.

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;

14. Step Twelve: Interpret Your Results

A good data analyst does more than write SQL queries. They also explain what the results mean.

For example:

  • A high average score may suggest strong performance in a subject.
  • A low average score may indicate that students need additional support.
  • Students with scores below 50 may need extra lessons.
  • The highest-scoring student may be recognised for excellent performance.

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.

15. Practical Assignment

It is time to practise. Use the SchoolAnalysis database to complete the following tasks.

  1. Display all students in the Students table.
  2. Display only the names of students from Oyo State.
  3. Find all students who scored above 70.
  4. Arrange all Mathematics scores from highest to lowest.
  5. Count the number of students in the database.
  6. Calculate the average score in Mathematics.
  7. Find the highest and lowest scores in English.
  8. Calculate the average score for each subject.
  9. Display students' names alongside their examination results.
  10. Find the total score for each student and arrange the results from highest to lowest.

16. Knowledge Check

Answer these questions to test your understanding.

  1. Which SQL command retrieves information from a database?
    A. DELETE
    B. SELECT
    C. UPDATE
    D. DROP
  2. Which clause filters individual records?
    A. WHERE
    B. GROUP BY
    C. ORDER BY
    D. JOIN
  3. Which function calculates an average?
    A. SUM()
    B. COUNT()
    C. AVG()
    D. MAX()
  4. Which clause groups records with similar values?
    A. LIMIT
    B. GROUP BY
    C. WHERE
    D. DISTINCT
  5. Which JOIN returns matching records from two tables?
    A. INNER JOIN
    B. CROSS JOIN
    C. RIGHT JOIN
    D. FULL JOIN
  6. Which keyword sorts records from highest to lowest?
    A. ASC
    B. DESC
    C. GROUP
    D. WHERE
  7. Which SQL function counts records?
    A. SUM()
    B. AVG()
    C. COUNT()
    D. MIN()
  8. Which clause filters grouped results?
    A. WHERE
    B. HAVING
    C. SELECT
    D. INSERT
  9. What is the purpose of a foreign key?
    A. To delete a database
    B. To connect related tables
    C. To sort records
    D. To calculate averages
  10. What should a data analyst do after running SQL queries?
    A. Ignore the results
    B. Delete the tables
    C. Interpret the results
    D. Stop analysing

17. Course Summary

Congratulations! You have completed all eight modules of Introduction to SQL for Data Analysis.

Here is a recap of what you have learned:

  1. Introduction to SQL and Databases
  2. Setting Up and Understanding SQL
  3. Retrieving Data Using SELECT
  4. Filtering and Searching Data
  5. Sorting and Organising Query Results
  6. SQL Functions and Data Aggregation
  7. Combining and Working with Data
  8. Practical SQL for Data Analysis

18. Your Next Learning Steps

SQL is an important skill for people interested in data analysis, business intelligence and database management.

To improve your skills, continue learning:

  • Advanced SQL queries.
  • Excel for data analysis.
  • Power BI for data visualisation.
  • Python for data analysis.
  • Database management.
  • Real-world data analysis projects.

Final Project Challenge

Imagine that a school gives you examination records for 500 students. Your task is to use SQL to prepare a report containing:

  • The total number of students.
  • The average score for each subject.
  • The highest and lowest scores.
  • The number of students who passed and failed.
  • The best-performing students.
  • Subjects that require more attention.
  • Recommendations for improving academic performance.

Complete the project using MySQL Workbench or phpMyAdmin. Save your SQL queries and explain your findings in a short report.

Congratulations on Completing the Course!

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.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’