Welcome to Module 2 of Basic Queries! In the previous module, you learned what databases, tables, and queries are. You also discovered that SQL is a language used to communicate with databases.
In this module, you will learn how SQL works, how to write SQL statements, understand important SQL keywords, and create your first database query.
Think of SQL as a language you use to give instructions to a computer database. When you write the correct instruction, the database performs the task you requested.
Syntax means the rules used when writing something. Just as English has grammar rules, SQL has rules that help the database understand your instructions.
For example, look at this SQL statement:
SELECT Name FROM Students;
Let's understand each part:
The complete statement means: "Show me the names of all students stored in the Students table."
Remember: SQL statements must follow the correct syntax. If you make mistakes, the database may display an error instead of performing your request.
An SQL statement is an instruction given to a database to perform a particular task.
Imagine telling your younger brother to bring your school bag. That is an instruction. In the same way, SQL statements give instructions to databases.
Here are some common SQL statements:
Used to retrieve information from a database.
SELECT * FROM Students;
This displays all records in the Students table.
Used to add new information to a database.
INSERT INTO Students (Name, Age)
VALUES ('John', 13);
This adds a new student named John who is 13 years old.
Used to change information already stored in a database.
UPDATE Students
SET Age = 14
WHERE Name = 'John';
This changes John's age to 14.
Used to remove records from a database.
DELETE FROM Students
WHERE Name = 'John';
This removes John's record from the Students table.
Important: UPDATE and DELETE commands can change or remove important information. Always use the correct WHERE condition and check your records before executing these commands.
SQL keywords are special words that have specific meanings in SQL. They tell the database what action to perform.
Some important SQL keywords include:
| SQL Keyword | Meaning |
|---|---|
| SELECT | Retrieves information. |
| FROM | Identifies the table to use. |
| WHERE | Filters information using a condition. |
| INSERT INTO | Adds new records. |
| UPDATE | Changes existing records. |
| DELETE | Removes records. |
| ORDER BY | Arranges information in a particular order. |
| GROUP BY | Groups records with similar values. |
For example, if you want to display students who are 13 years old, you can write:
SELECT * FROM Students
WHERE Age = 13;
Here, SELECT retrieves the information, FROM identifies the table, and WHERE tells the database to display only students whose age is 13.
Before writing SQL queries, you need to understand databases and table names.
Imagine a school called Bright Future Academy. The school uses a database called SchoolDB.
Inside this database, there may be different tables:
Each table stores a different type of information.
For example, the Students table contains student records, while the Teachers table contains information about teachers.
In MySQL, you can select a database using:
USE SchoolDB;
This tells MySQL that you want to work with the SchoolDB database.
You can also ask MySQL to show the available databases:
SHOW DATABASES;
To see the tables inside the selected database, use:
SHOW TABLES;
A database table is organised into rows and columns. Understanding these two terms will help you write better SQL queries.
Consider the following Students table:
| StudentID | Name | Age | Class |
|---|---|---|---|
| 101 | Daniel | 13 | JSS 2 |
| 102 | Mary | 12 | JSS 1 |
| 103 | Peter | 14 | JSS 3 |
| 104 | Aisha | 13 | JSS 2 |
A row contains information about one particular student. For example, the first row contains Daniel's student ID, name, age, and class.
A column contains one category of information. For example, the Age column contains the ages of all students.
A record is a complete set of information about one person or item. Daniel's complete information represents one record.
Quick Tip: Rows go across horizontally, while columns go down vertically.
Now it is time to write your first SQL query!
Imagine you are helping a school administrator find information about students.
The school has a database called SchoolDB and a table called Students. You want to display all the students.
Step 1: Select the database.
USE SchoolDB;
Step 2: Display all student records.
SELECT * FROM Students;
Let's understand the second command:
The database will display every column and every record in the Students table.
What if you only want to see the names of the students?
SELECT Name FROM Students;
This displays only the Name column.
What if you want to see the names and ages of the students?
SELECT Name, Age
FROM Students;
This displays only the Name and Age columns.
Imagine that you are working as a junior database assistant in a school. Your task is to retrieve information from the Students table.
Use the following sample records:
| ID | Name | Age | Class |
|---|---|---|---|
| 201 | John | 13 | JSS 2 |
| 202 | Grace | 12 | JSS 1 |
| 203 | Michael | 14 | JSS 3 |
| 204 | Sarah | 13 | JSS 2 |
| 205 | David | 12 | JSS 1 |
Try writing SQL statements to perform these tasks:
In this module, you have learned:
Answer the following questions to test your understanding:
You have successfully completed Module 2 of Basic Queries! You now understand SQL fundamentals and can write simple SQL statements to retrieve information from a database.
Keep practising. The more SQL commands you write, the better you become!
Module 3: Retrieving Data with SELECT
You will learn how to retrieve specific information, select multiple columns, use aliases, and become more confident working with database records.
CRMNuggets โ Learn. Practice. Get Certified.