Bachelor of Science in Computer Science
Course ContentSQL
Habari Class! Welcome to the World of SQL!
Imagine you're the head of a school, maybe like Alliance High School or Kenya High. You have thousands of student records. Now, the Ministry of Education calls and asks, "How many students from Nakuru County scored an A in Mathematics last year?"
How would you find that information? Go through thousands of paper files one by one? 😩 That would take forever! This is where databases and our new superpower, SQL, come to the rescue!
Think of a database as a super-organized digital filing cabinet. And SQL? It's the magical language you use to talk to that cabinet, asking it to find, add, change, or remove files in the blink of an eye. It's the secret language behind everything from M-Pesa transactions to your Facebook feed.
So, let's learn how to speak this language! Sawa?
Image Suggestion: A vibrant illustration of a Kenyan student sitting at a modern computer desk. Glowing lines of blue code flow from the screen, connecting to a holographic, neatly organized digital filing cabinet floating in the air. The background has subtle hints of the Nairobi skyline. Style: Afrofuturistic, bright, and optimistic.
What is a Database, Really?
Before we learn the language (SQL), let's quickly understand what we're talking to. A database is simply a structured collection of data. The most common type uses tables, which look a lot like spreadsheets.
A table has rows (records) and columns (fields). For example, a Students table might look like this:
+-------------+----------------+------------+----------+
| StudentID | Name | County | Grade |
+-------------+----------------+------------+----------+
| 101 | Juma Otieno | Kisumu | A- |
| 102 | Wanjiku Mwangi | Kiambu | B+ |
| 103 | David Kiptoo | Uasin Gishu| A |
+-------------+----------------+------------+----------+
Each row is a unique student, and each column gives us a specific piece of information about them. Easy, right?
The Four Superpowers of SQL: Your Basic Commands
SQL is all about giving commands. We'll start with the four most important ones, which data experts call CRUD (Create, Read, Update, Delete). Let's call them our Data Superpowers!
- SELECT: The Finder (Reads data)
- INSERT: The Creator (Adds new data)
- UPDATE: The Editor (Changes existing data)
- DELETE: The Remover (Deletes data)
1. SELECT - The Finder 🔎
This is the command you'll use the most. It's for retrieving, or "selecting," data from your tables. It's how you ask the database questions.
Let's say we want to get the names of all students from our Students table. The command is simple:
SELECT Name, County FROM Students;
But what if we only want students from Kiambu County? We add a WHERE clause to filter our results:
SELECT Name FROM Students WHERE County = 'Kiambu';
The database would instantly reply with: Wanjiku Mwangi.
2. INSERT - The Creator ➕
Your school has a new student! You need to add them to your digital register. The INSERT INTO command lets you create a new record (row).
Let's add a new student, Fatuma Ali from Mombasa, who has a B grade.
INSERT INTO Students (StudentID, Name, County, Grade)
VALUES (104, 'Fatuma Ali', 'Mombasa', 'B');
Just like that, Fatuma is now officially in our school's database!
3. UPDATE - The Editor ✏️
People's details change. Maybe Juma Otieno worked hard and improved his grade from an A- to a solid A! We need to update his record. The UPDATE command is perfect for this.
Crucial part: You MUST use a WHERE clause to specify exactly WHICH record to change. If you forget it, you might change every single student's grade to an A! 😱
UPDATE Students
SET Grade = 'A'
WHERE StudentID = 101;
Phew! Juma's record is now updated, and everyone else's is safe.
4. DELETE - The Remover ❌
Sometimes, a record needs to be removed. Perhaps a student has transferred to another school. The DELETE command handles this.
Like UPDATE, this command is powerful and needs a WHERE clause. If you forget it... you could delete ALL your student records. A true disaster!
Let's say Fatuma Ali (StudentID 104) transferred out.
DELETE FROM Students WHERE StudentID = 104;
Her record is now safely removed from the table.
Real-World Story: Think about M-Pesa. When you send money, Safaricom's database doesn't create a new you. It UPDATES your balance (subtracts money) and UPDATES the receiver's balance (adds money). It also INSERTS a new record into a `Transactions` table to keep a history of that specific payment. See? These commands are happening millions of times a day across Kenya!
Let's Do Some Math! Aggregate Functions
SQL is not just for fetching text; it's great with numbers too! Aggregate functions perform a calculation on a set of rows and return a single value.
Let's use a new table, for a Boda Boda Sacco called 'ChapChap Riders'.
+---------+----------------+------------+
| RiderID | Name | DailyTrips |
+---------+----------------+------------+
| 201 | Peter Kamau | 15 |
| 202 | Brian Ochieng | 22 |
| 203 | Adan Noor | 18 |
+---------+----------------+------------+
-
COUNT(): How many riders do we have?
This counts the number of rows.
SELECT COUNT(RiderID) FROM ChapChapRiders; -- Result: 3 -
SUM(): What's the total number of trips made today?
This adds up all the values in a column.
SELECT SUM(DailyTrips) FROM ChapChapRiders; -- Calculation: 15 + 22 + 18 -- Result: 55 -
AVG(): What's the average number of trips per rider?
This calculates the average of the values in a column.
SELECT AVG(DailyTrips) FROM ChapChapRiders; -- Calculation: (15 + 22 + 18) / 3 -- Result: 18.33 -
MAX(): Who was the busiest rider?
This finds the highest value in a column.
SELECT MAX(DailyTrips) FROM ChapChapRiders; -- Result: 22
Image Suggestion: A digital dashboard on a tablet held by a Kenyan Sacco manager. The screen shows a bar chart of 'Daily Trips per Rider'. An SQL query `SELECT Name, DailyTrips FROM ChapChapRiders ORDER BY DailyTrips DESC;` is visible in a small window, with the results populating the chart. The manager is smiling, clearly happy with the data. Style: Clean, modern, corporate.
Your Turn to Practice!
Congratulations! You've just learned the absolute fundamentals of SQL. You now know how to talk to a database to read, create, edit, and delete data. You can even make it do math for you!
The best way to become a master is to practice. Try to think about the databases all around you - at the bank, in your favorite food delivery app, or even the school's library system. Think about what questions you could ask and how you might write the SQL query for it.
This is just the beginning. Soon we'll learn how to combine data from multiple tables using JOINs, but for now, celebrate learning your first data superpower!
Keep coding, and see you in the next lesson! Safari njema on your SQL journey!
Pro Tip
Take your own short notes while going through the topics.