Congratulations! You have learned many important SQL commands. You now understand how to retrieve, filter, sort, calculate, and manage information stored in a database.
But how do people use these commands in real life?
Imagine you work as a junior database assistant in a school. Your responsibility is to help the school manage student records, examination scores, and class information.
You will need to write SQL queries to answer questions and solve everyday problems.
In this final module, you will complete practical projects that combine the SQL skills you have learned throughout this course.
By the end of this module, you should be able to:
Before writing SQL queries, you need a database to work with. For this project, we will create a simple school database.
CREATE DATABASE SchoolDB;
This command creates a database called SchoolDB.
USE SchoolDB;
The USE command tells MySQL that you want to work with SchoolDB.
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Class VARCHAR(20),
Age INT,
Score DECIMAL(5,2)
);
Let's understand the columns:
A newly created table is empty. You need to add records before you can retrieve useful student information.
Use the following SQL statement:
INSERT INTO Students
(StudentID, Name, Class, Age, Score)
VALUES
(1, 'Ade', 'JSS1', 12, 75),
(2, 'Bola', 'JSS1', 13, 90),
(3, 'Chika', 'JSS2', 12, 65),
(4, 'David', 'JSS2', 14, 85),
(5, 'Esther', 'JSS3', 13, 80),
(6, 'Farouk', 'JSS3', 14, 95),
(7, 'Grace', 'JSS1', 12, 88),
(8, 'Michael', 'JSS2', 13, 72);
This command inserts eight student records into the Students table.
SELECT * FROM Students;
This query displays all the records stored in the Students table.
A school administrator may want to find particular information about students instead of displaying every record.
SELECT * FROM Students;
This displays every column and record in the Students table.
SELECT Name FROM Students;
This displays only the Name column.
SELECT * FROM Students
WHERE Class = 'JSS1';
This displays students who belong to JSS1.
SELECT * FROM Students
WHERE Score >= 80;
This displays students whose scores are 80 or higher.
SELECT * FROM Students
WHERE Age = 13;
This displays students whose age is 13.
The school administrator wants to prepare a list of the best-performing students.
SELECT * FROM Students
ORDER BY Score DESC;
This arranges students from the highest score to the lowest.
SELECT * FROM Students
ORDER BY Score DESC
LIMIT 3;
This displays only the three highest-scoring students.
SELECT * FROM Students
ORDER BY Name ASC;
This arranges student names from A to Z.
SELECT * FROM Students
ORDER BY StudentID
LIMIT 3 OFFSET 3;
This skips the first three records and displays the next three.
The school wants to understand how students performed in their examinations. SQL functions can help calculate important statistics.
SELECT COUNT(*) AS TotalStudents
FROM Students;
SELECT SUM(Score) AS TotalScore
FROM Students;
SELECT AVG(Score) AS AverageScore
FROM Students;
SELECT MAX(Score) AS HighestScore
FROM Students;
SELECT MIN(Score) AS LowestScore
FROM Students;
SELECT
Class,
AVG(Score) AS AverageScore
FROM Students
GROUP BY Class;
This query groups students by class and calculates the average score for each class.
SELECT
Class,
AVG(Score) AS AverageScore
FROM Students
GROUP BY Class
HAVING AVG(Score) >= 75;
This query displays only classes whose average examination score is at least 75.
Database administrators sometimes need to add new records, correct information, or remove records.
INSERT INTO Students
(StudentID, Name, Class, Age, Score)
VALUES
(9, 'Daniel', 'JSS1', 13, 82);
UPDATE Students
SET Score = 90
WHERE StudentID = 9;
This changes Daniel's score to 90.
UPDATE Students
SET Class = 'JSS2'
WHERE StudentID = 9;
This moves Daniel's record into JSS2.
DELETE FROM Students
WHERE StudentID = 9;
This removes the record with StudentID 9.
Important: Always check your WHERE condition before running UPDATE or DELETE. Without a WHERE condition, these commands can affect every record in the table.
Imagine that the school stores students in two separate tables. The administrator wants to combine their names into one list.
CREATE TABLE JSS1_Students (
StudentID INT,
Name VARCHAR(100)
);
CREATE TABLE JSS2_Students (
StudentID INT,
Name VARCHAR(100)
);
INSERT INTO JSS1_Students
VALUES
(1, 'Ade'),
(2, 'Bola');
INSERT INTO JSS2_Students
VALUES
(3, 'Chika'),
(4, 'David');
SELECT Name FROM JSS1_Students
UNION
SELECT Name FROM JSS2_Students;
UNION combines the results and removes duplicate rows.
SELECT Name FROM JSS1_Students
UNION ALL
SELECT Name FROM JSS2_Students;
UNION ALL combines the results while keeping duplicate rows.
You have completed a school database project. Now let's try another practical example.
Imagine you work in a supermarket in Ibadan. The manager wants to keep track of products and their prices.
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Category VARCHAR(50),
Price DECIMAL(10,2),
Quantity INT
);
INSERT INTO Products
(ProductID, ProductName, Category, Price, Quantity)
VALUES
(1, 'Rice', 'Food', 75000, 10),
(2, 'Beans', 'Food', 45000, 15),
(3, 'Vegetable Oil', 'Food', 12000, 20),
(4, 'Milk', 'Drinks', 8500, 30),
(5, 'Soft Drink', 'Drinks', 1500, 50),
(6, 'Soap', 'Household', 2500, 25),
(7, 'Detergent', 'Household', 5000, 18);
SELECT * FROM Products;
SELECT * FROM Products
WHERE Category = 'Food';
SELECT * FROM Products
WHERE Price > 5000;
SELECT * FROM Products
ORDER BY Price DESC
LIMIT 5;
SELECT SUM(Quantity) AS TotalQuantity
FROM Products;
SELECT AVG(Price) AS AveragePrice
FROM Products;
SELECT
Category,
COUNT(*) AS NumberOfProducts
FROM Products
GROUP BY Category;
SELECT * FROM Products
WHERE Quantity < 20;
UPDATE Products
SET Price = 78000
WHERE ProductID = 1;
INSERT INTO Products
(ProductID, ProductName, Category, Price, Quantity)
VALUES
(8, 'Bread', 'Food', 2000, 40);
You are now working as a junior database assistant. Complete the following tasks using the SchoolDB database.
| SQL Command | What It Does |
|---|---|
| CREATE DATABASE | Creates a database. |
| USE | Selects a database. |
| CREATE TABLE | Creates a table. |
| INSERT INTO | Adds new records. |
| SELECT | Retrieves information. |
| WHERE | Filters records. |
| ORDER BY | Sorts query results. |
| LIMIT | Limits the number of results. |
| OFFSET | Skips a number of results. |
| COUNT() | Counts records or non-NULL values. |
| SUM() | Calculates a total. |
| AVG() | Calculates an average. |
| MIN() | Finds the smallest value. |
| MAX() | Finds the largest value. |
| GROUP BY | Groups records. |
| HAVING | Filters grouped results. |
| DISTINCT | Returns unique rows or values. |
| UNION | Combines query results without duplicate rows. |
| UPDATE | Changes existing records. |
| DELETE | Removes records. |
| COMMIT | Saves transaction changes. |
| ROLLBACK | Cancels uncommitted transaction changes. |
Well done! You have completed the Basic Queries in SQL course.
You have learned how databases work, how to write SQL statements, retrieve information, filter records, sort results, perform calculations, combine query results, and manage database records.
These skills provide a foundation for learning more advanced SQL topics, database management, data analysis, and backend development.
CRMNuggets โ Learn. Practice. Get Certified.