Imagine you are working as a data analyst in a school in Ibadan. The school has thousands of student records stored in a database. Your principal asks you to find the names of all students in the school.
Would you open every record and search manually? No! You can use SQL to retrieve the information within seconds.
In this module, you will learn how to use the SELECT statement to retrieve information from database tables. You will also learn how to select specific columns, display all records, use column aliases and remove duplicate results.
By the end of this module, you should be able to:
SELECT is an SQL command used to retrieve or display information stored in a database.
Think of SELECT as asking a question. You are telling the database what information you want to see.
For example, you may want to know:
The SELECT statement helps you retrieve this information.
Syntax means the correct arrangement of words and symbols when writing a computer command.
The basic structure of a SELECT query is:
SELECT column_name
FROM table_name;
Let's understand each part:
Throughout this module, we will use a school database containing student information.
Imagine the Students table contains the following records:
| student_id | student_name | age | class | state |
|---|---|---|---|---|
| 1 | Samuel | 13 | JSS 2 | Oyo |
| 2 | Mary | 12 | JSS 1 | Lagos |
| 3 | Peter | 14 | JSS 3 | Oyo |
| 4 | Esther | 13 | JSS 2 | Osun |
| 5 | David | 12 | JSS 1 | Oyo |
We will use this table to practise different SELECT queries.
What if you want to see everything stored in the Students table? You can use the asterisk symbol (*).
Example:
SELECT * FROM Students;
The asterisk means all columns. This command tells MySQL to display all columns and all records in the Students table.
The result will include student IDs, names, ages, classes and states.
Remember: SELECT * is useful when you want to inspect an entire table, especially when learning or testing.
Sometimes you do not need all the information in a table. You may only want to see the names of students.
Use this query:
SELECT student_name
FROM Students;
This command displays only the student_name column.
For example, the result may look like this:
| student_name |
|---|
| Samuel |
| Mary |
| Peter |
| Esther |
| David |
Notice that the other columns are not displayed.
You can retrieve more than one column in a single query.
Imagine the school principal wants to see students' names, ages and classes but does not need their state of origin.
Use this query:
SELECT student_name, age, class
FROM Students;
The database will display only the three requested columns.
Important: Separate column names with commas. Do not forget the FROM keyword.
Sometimes column names in a database are difficult to understand. You may want to give them simpler names when displaying results.
SQL allows you to do this using an alias. An alias is a temporary alternative name given to a column in a query result.
For example:
SELECT student_name AS Name,
age AS Age,
class AS Class
FROM Students;
The result will display the headings Name, Age and Class instead of the original column names.
The AS keyword tells SQL that you want to give a column a different name in the result.
Remember, an alias does not permanently rename the column in the database.
In MySQL, you can also create an alias without writing AS.
SELECT student_name Name
FROM Students;
This query produces a result with the heading Name. However, using AS often makes queries easier to read.
Imagine a school has 500 students. Some students come from the same state.
If you retrieve the state column, you may see Oyo repeated many times.
Example:
SELECT state
FROM Students;
The result may contain repeated values such as Oyo, Lagos, Oyo, Osun and Oyo.
What if you only want to see each state once? This is where DISTINCT becomes useful.
SELECT DISTINCT state
FROM Students;
DISTINCT removes duplicate combinations of selected values from the query result.
The result will look like this:
| state |
|---|
| Oyo |
| Lagos |
| Osun |
This is useful when finding unique product categories, customer locations or student classes.
| Command | What It Does |
|---|---|
| SELECT state FROM Students; | Displays all state values, including duplicates. |
| SELECT DISTINCT state FROM Students; | Displays each unique state value once. |
SQL is not only used in schools. Supermarkets also use databases to organise product information.
Imagine a supermarket has a Products table:
| product_id | product_name | price | category |
|---|---|---|---|
| 1 | Rice | 85000 | Food |
| 2 | Beans | 45000 | Food |
| 3 | Cooking Oil | 12000 | Groceries |
| 4 | Milk | 8000 | Groceries |
Here are some useful queries:
SELECT * FROM Products;
SELECT product_name
FROM Products;
SELECT product_name, price
FROM Products;
SELECT DISTINCT category
FROM Products;
Mistake 1: Forgetting FROM
SELECT student_name;
This query is incomplete if you want to retrieve the student_name column from the Students table.
Correct version:
SELECT student_name
FROM Students;
Mistake 2: Forgetting commas
SELECT student_name age class
FROM Students;
Correct version:
SELECT student_name, age, class
FROM Students;
Mistake 3: Using the wrong table name
If your table is called Students, writing SELECT from Teachers will not retrieve the student records.
In this module, you have learned:
Answer these questions to test your understanding.
Imagine you are working as a junior data analyst for a supermarket in Ibadan.
The supermarket manager wants you to retrieve information from the Products table.
Write SQL queries to complete these tasks:
Challenge: Write a query that displays the product name, price and category, but gives the column headings the names Product, Selling Price and Product Category.
You have completed Module 3. You can now retrieve information from a database, select specific columns, rename column headings and remove duplicate results.
In Module 4, you will learn how to filter and search for specific records using the WHERE clause and SQL operators.
CRMNuggets โ Learn. Practice. Get Certified.