Imagine you are managing a school database. The school has two tables: one contains students in JSS1, while another contains students in JSS2.
Your teacher wants to see the students from both classes in one list. How can you combine the results?
SQL provides special commands that help you combine results, remove duplicate records, and manage information returned by database queries.
In this module, you will learn about UNION, UNION ALL, DISTINCT, INSERT, UPDATE, DELETE, and transactions.
By the end of this module, you should be able to:
Sometimes, information is stored in different tables. You may want to bring the results together so that they can be viewed as one list.
For example, imagine two tables containing student names.
| StudentID | Name |
|---|---|
| 1 | Ade |
| 2 | Bola |
| 3 | Chika |
| StudentID | Name |
|---|---|
| 4 | David |
| 5 | Esther |
| 6 | Farouk |
We can combine the names from both tables using the SQL UNION command.
UNION combines the results of two or more SELECT statements into one result. It removes duplicate rows from the combined results.
SELECT Name FROM JSS1_Students
UNION
SELECT Name FROM JSS2_Students;
This query displays names from both tables in one result.
For UNION to work correctly, the SELECT statements must return the same number of columns, and the corresponding columns must have compatible data types.
The column names in the final result are generally taken from the first SELECT statement.
UNION ALL also combines results from different SELECT statements. However, unlike UNION, it keeps duplicate rows.
SELECT Name FROM JSS1_Students
UNION ALL
SELECT Name FROM JSS2_Students;
This query combines the names from both tables without removing duplicates.
| UNION | UNION ALL |
|---|---|
| Removes duplicate rows. | Keeps duplicate rows. |
| Returns unique combined rows. | Returns all combined rows. |
| May require extra processing to remove duplicates. | Usually avoids duplicate-removal processing. |
Imagine a school database contains the names of students and their classes. Several students belong to the same class.
If you want to see each class only once, you can use DISTINCT.
| Name | Class |
|---|---|
| Ade | JSS1 |
| Bola | JSS1 |
| Chika | JSS2 |
| David | JSS2 |
| Esther | JSS3 |
Use this query:
SELECT DISTINCT Class
FROM Students;
The result contains JSS1, JSS2, and JSS3, with each class appearing once.
DISTINCT is useful when you want to identify unique names, locations, categories, or other values.
Database information changes regularly. A school may admit a new student, or a supermarket may add a new product.
SQL uses the INSERT INTO statement to add new records to a table.
INSERT INTO Students (StudentID, Name, Class, Score)
VALUES (7, 'Grace', 'JSS1', 88);
Explanation:
After running this query successfully, Grace's record will be added to the Students table.
Imagine a teacher discovers that a student's examination score was entered incorrectly. The score needs to be corrected.
SQL uses UPDATE to change information already stored in a database.
UPDATE Students
SET Score = 95
WHERE StudentID = 7;
This query:
Important safety rule: Always check your WHERE condition before running an UPDATE query. Without WHERE, every record in the table may be updated.
Sometimes, a record needs to be removed from a database. For example, an administrator may need to remove a test record that was entered by mistake.
SQL uses DELETE to remove records from a table.
DELETE FROM Students
WHERE StudentID = 7;
This query removes the student record whose StudentID is 7.
Important safety rule: Always confirm the record you want to remove before running DELETE.
Running DELETE without a WHERE condition can remove every record from the table.
A transaction is a group of database operations treated as one unit of work. It helps protect data when several related changes must succeed together.
Imagine a bank transfer. Money must be removed from one account and added to another account. Both operations should succeed together.
SQL provides transaction commands such as START TRANSACTION, COMMIT, and ROLLBACK.
Starts a new transaction.
START TRANSACTION;
COMMIT saves the changes made during the transaction.
COMMIT;
ROLLBACK cancels changes made in the current transaction that have not been committed.
ROLLBACK;
START TRANSACTION;
UPDATE Accounts
SET Balance = Balance - 5000
WHERE AccountID = 1;
UPDATE Accounts
SET Balance = Balance + 5000
WHERE AccountID = 2;
COMMIT;
This example transfers 5,000 units of currency from one account to another. Both updates are saved together when COMMIT succeeds.
In a real banking system, additional checks are required, such as confirming sufficient funds and verifying that both account records exist.
Transaction behaviour depends on the database engine. In MySQL, for example, transactional tables such as InnoDB support transactions.
Assume you have two tables named JSS1_Students and JSS2_Students. Each table contains StudentID and Name columns.
Write SQL queries to complete the following tasks:
In this module, you learned how to combine query results and manage database records.
You are helping a school administrator manage a student database. Write SQL queries to:
Practise these commands in a test database. Do not experiment with UPDATE or DELETE on important records without permission and a backup.
In Module 8, you will bring together everything you have learned in this course through practical SQL query projects. You will practise retrieving, filtering, sorting, calculating, and managing database information.
CRMNuggets โ Learn. Practice. Get Certified.