SQL for Beginners: 10 Queries You Will Use Every Day
The Qtec Mastery
Author
Most of the SQL people write at work comes down to about ten patterns. This guide on SQL for beginners walks through those ten, one at a time, on a small Kenyan college database you can build in two minutes. By the end you will be able to find, sort, count, add, change and remove data with confidence, and avoid the one mistake that wipes out a whole table.
The SQL queries beginners use most are SELECT (read data), WHERE (filter), ORDER BY and LIMIT (sort and cut), COUNT, SUM and AVG (totals), GROUP BY and HAVING (totals per group), and INSERT, UPDATE and DELETE (change data). Always use WHERE with UPDATE and DELETE. |
Databases from Zero, part 2 of 6. Previous: What Is a Database? A Beginner's Guide With Real Kenyan Examples · Next: SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples
How do I set up the practice database?
Open MySQL Workbench or an online MySQL 8 sandbox and run the script below. It creates three tables (students, courses and payments) and fills them with sample data. If you are new to tables and rows, read part 1 first.
CREATE DATABASE IF NOT EXISTS qtec_school;
USE qtec_school;
CREATE TABLE courses (
course_id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL,
fee_kes DECIMAL(10,2) NOT NULL
);
CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(100) NOT NULL,
phone VARCHAR(15) UNIQUE,
county VARCHAR(50),
course_id INT,
enrolled_on DATE
);
CREATE TABLE payments (
payment_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
mpesa_code VARCHAR(12) UNIQUE,
amount_kes DECIMAL(10,2) NOT NULL,
paid_on DATETIME NOT NULL,
FOREIGN KEY (student_id) REFERENCES students(student_id)
);
INSERT INTO courses (course_name, fee_kes) VALUES
('Web Development', 10000.00),
('Python Programming', 20000.00),
('Cyber Security', 15000.00);
INSERT INTO students (full_name, phone, county, course_id, enrolled_on) VALUES
('Wanjiku Kamau', '0712345678', 'Nairobi', 1, '2026-01-10'),
('Brian Otieno', '0723456789', 'Kisumu', 2, '2026-01-12'),
('Amina Hassan', '0734567890', 'Mombasa', 3, '2026-02-03'),
('Kevin Mutua', '0745678901', 'Machakos', 2, '2026-02-15'),
('Faith Chebet', '0756789012', 'Uasin Gishu', NULL, '2026-03-01');
INSERT INTO payments (student_id, mpesa_code, amount_kes, paid_on) VALUES
(1, 'SKA4H7XQ2P', 10000.00, '2026-01-10 09:15:00'),
(2, 'SKB2M9RT5L', 10000.00, '2026-01-12 14:30:00'),
(2, 'SKC7P3WN8D', 5000.00, '2026-02-12 10:05:00'),
(3, 'SKD5Q1ZV6H', 15000.00, '2026-02-03 16:45:00');
On fresh tables, the students get IDs 1 to 5 and the payments get IDs 1 to 4, in the order inserted. Kevin and Faith have not paid anything yet. The phone numbers are made up.
How do I read data with SELECT? (Query 1)
SELECT chooses the columns, FROM chooses the table:
SELECT full_name, county
FROM students;
This returns all five students. SELECT * returns every column, which is handy while exploring but best avoided in real applications, where you should ask only for what you need.
How do I filter rows with WHERE? (Query 2)
SELECT full_name, county
FROM students
WHERE course_id = 2;
| full_name | county |
|---|---|
| Brian Otieno | Kisumu |
| Kevin Mutua | Machakos |
You can combine conditions with AND and OR, and use >, <, >=, <= and <> (not equal). One trap: to find missing values you must write WHERE course_id IS NULL, not = NULL. The second version never matches anything. Here IS NULL would return Faith Chebet.
How do I sort and limit results?
Query 3: sort with ORDER BY
SELECT full_name, county
FROM students
ORDER BY full_name;
This lists the students alphabetically: Amina Hassan, Brian Otieno, Faith Chebet, Kevin Mutua, Wanjiku Kamau. Add DESC for reverse order. Without ORDER BY, the database is free to return rows in any order, so never rely on the order you happen to see.
Query 4: get the first few rows with LIMIT
Combine ORDER BY and LIMIT to get "the latest" or "the top" of anything:
SELECT full_name, enrolled_on
FROM students
ORDER BY enrolled_on DESC
LIMIT 3;
| full_name | enrolled_on |
|---|---|
| Faith Chebet | 2026-03-01 |
| Kevin Mutua | 2026-02-15 |
| Amina Hassan | 2026-02-03 |
How do I count and add up values? (Query 5)
Aggregate functions turn many rows into one answer:
SELECT COUNT(*) AS num_payments,
SUM(amount_kes) AS total_kes,
ROUND(AVG(amount_kes), 2) AS average_kes
FROM payments;
| num_payments | total_kes | average_kes |
|---|---|---|
| 4 | 40000.00 | 10000.00 |
AS gives a result column a friendly name. Also know the difference between COUNT(*), which counts rows, and COUNT(column), which skips NULLs. On the students table, COUNT(*) gives 5 but COUNT(course_id) gives 4, because Faith has no course.
How do I get totals per group with GROUP BY and HAVING?
Query 6: totals per group with GROUP BY
How much has each student paid? GROUP BY splits the rows into groups and runs the aggregate once per group:
SELECT student_id, SUM(amount_kes) AS total_paid
FROM payments
GROUP BY student_id
ORDER BY student_id;
| student_id | total_paid |
|---|---|
| 1 | 10000.00 |
| 2 | 15000.00 |
| 3 | 15000.00 |
Brian (student 2) made two payments, so they were added together. Showing names instead of IDs needs a JOIN, which is exactly what part 3 covers.
Query 7: filter groups with HAVING
WHERE filters rows before grouping. HAVING filters the groups after. To find students who paid in instalments:
SELECT student_id,
COUNT(*) AS payments_made,
SUM(amount_kes) AS total_paid
FROM payments
GROUP BY student_id
HAVING COUNT(*) > 1;
| student_id | payments_made | total_paid |
|---|---|---|
| 2 | 2 | 15000.00 |
How do I add, change and delete rows?
Query 8: INSERT a new student
INSERT INTO students (full_name, phone, county, course_id, enrolled_on)
VALUES ('Mercy Wambui', '0767890123', 'Nakuru', 1, '2026-03-10');
We leave out student_id because AUTO_INCREMENT fills it in. Mercy becomes student 6. Text and dates go in single quotes, numbers do not.
Query 9: UPDATE, always with WHERE
Mercy switches to Cyber Security:
UPDATE students
SET course_id = 3
WHERE student_id = 6;
Warning: if you forget the WHERE line, MySQL changes every row in the table, and all six students end up on Cyber Security. There is no undo button. Protect yourself:
- Run a
SELECTwith the sameWHEREfirst, and check it returns only the rows you expect. - Filter by the primary key where you can, not by name.
- Wrap risky changes in a transaction, then check before you commit:
START TRANSACTION;
UPDATE students SET course_id = 3 WHERE student_id = 6;
SELECT student_id, full_name, course_id FROM students;
-- happy? run COMMIT; not happy? run ROLLBACK;
COMMIT;
MySQL Workbench also has a "safe updates" setting, on by default, that blocks UPDATE and DELETE statements that do not filter on a key column.
Query 10: DELETE a row
DELETE FROM students
WHERE student_id = 6;
The same warning applies: DELETE FROM students; with no WHERE empties the table. The foreign key gives you some protection here. Try deleting Wanjiku with DELETE FROM students WHERE student_id = 1; and MySQL refuses with error 1451 ("Cannot delete or update a parent row: a foreign key constraint fails"), because she has a payment pointing at her. That is the database protecting your records.
Practice exercise
- List every payment of KES 10,000 or more, newest first, showing the M-Pesa code, amount and date.
- Show how many students are on each course, ignoring students with no course.
- Kevin Mutua (student 4) has moved to Nairobi. Update his county safely.
Answers
-- 1
SELECT mpesa_code, amount_kes, paid_on
FROM payments
WHERE amount_kes >= 10000
ORDER BY paid_on DESC;
-- 2
SELECT course_id, COUNT(*) AS num_students
FROM students
WHERE course_id IS NOT NULL
GROUP BY course_id
ORDER BY course_id;
-- 3
SELECT full_name, county FROM students WHERE student_id = 4;
UPDATE students SET county = 'Nairobi' WHERE student_id = 4;
Answer 1 returns three rows (Amina's, Brian's first payment and Wanjiku's). Answer 2 returns course 1 with 1 student, course 2 with 2 and course 3 with 1. When you start writing queries inside an app, always pass user input safely. Our guide to SQL injection explains why.
Turn these ten queries into a real skillDBMS 1 covers SQL from the basics to multi-table queries over 10 weeks online, with guided practice and a certificate on completion. Also useful: SQL for Data Analysis (KES 20,000) |
Frequently asked questions
Is SQL case sensitive?SQL keywords are not, so What is the difference between WHERE and HAVING?
Can I undo a DELETE in MySQL?Only if you ran it inside a transaction that you have not committed yet, in which case Do these queries work in PostgreSQL too?Yes. All ten use standard SQL. The table setup differs slightly: PostgreSQL uses identity columns or |
Comments (0)
Search Blog
Categories
Popular Posts
Why Traditional Education is Failing Future Developers: The QTEC Approach
326 views
What Is Frontend Development? A Beginner's Guide
121 views
What Is a Database? A Beginner's Guide With Real Kenyan Examples
35 views
Git and GitHub for Beginners: The Commands You Will Actually Use
33 views
Computer Packages Course in Kenya: Fees, Duration and Certificate (2026)
32 views
You Might Also Like
What Is a Database? A Beginner's Guide With Real Kenyan Examples
Every app you use, from your school portal to M-Pesa, keeps its information some...
Read More →
SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples
Real databases split information across several tables, so sooner or later you n...
Read More →
Database Design for Beginners: Tables, Keys and Normalisation
Most messy databases start life as a spreadsheet that grew too big: names typed...
Read More →