Welcome to Module 3 of Basic Queries! You have already learned what databases are, how SQL works, and how to write simple SQL statements.
Now, you are going to learn one of the most important SQL commands: SELECT.
Imagine your school has 1,000 students, and your teacher wants to see their names, ages, or examination results. Instead of checking every student's paper file, the teacher can use the SELECT command to retrieve the required information from the school database.
In this module, you will learn how to display all records, select specific columns, use aliases, and retrieve information from database tables.
The SELECT statement is an SQL command used to retrieve or display information stored in a database.
Think of SELECT as asking your computer: "Please show me this information."
For example, imagine you are working with a school database that contains a table called Students.
You want to see all the students registered in the school. You can write:
SELECT * FROM Students;
This command tells the database to display all the information stored in the Students table.
Let's understand the command:
The SELECT command does not normally change the information in your database. It simply displays the information you requested.
Sometimes, you may want to see everything stored in a database table. SQL makes this possible using the asterisk (*) symbol.
The asterisk means all columns in the selected table.
Imagine the Students table contains the following records:
| StudentID | Name | Age | Class | Gender |
|---|---|---|---|---|
| 101 | Daniel | 13 | JSS 2 | Male |
| 102 | Mary | 12 | JSS 1 | Female |
| 103 | Peter | 14 | JSS 3 | Male |
| 104 | Aisha | 13 | JSS 2 | Female |
To display all the records, write:
SELECT * FROM Students;
The database will display all five columns and all four student records.
When should you use SELECT *?
Professional Tip: In large databases, selecting only the columns you need can be more efficient than retrieving everything.
What if you do not want to see all the information in a table? What if you only want the names of the students?
You can tell SQL exactly which columns you want to retrieve.
For example:
SELECT Name FROM Students;
This command displays only the Name column.
You can also select more than one column by separating their names with commas.
Example 1: Display student names and ages.
SELECT Name, Age
FROM Students;
Example 2: Display student names and classes.
SELECT Name, Class
FROM Students;
Example 3: Display student IDs and names.
SELECT StudentID, Name
FROM Students;
Remember that the column names you write must match the names used in your database table.
The FROM clause tells SQL which table contains the information you want to retrieve.
Imagine a school database contains these tables:
If you want to retrieve student information, you use the Students table. If you want teacher information, you use the Teachers table.
Example 1: Retrieve student names.
SELECT Name
FROM Students;
Example 2: Retrieve teacher names.
SELECT TeacherName
FROM Teachers;
Example 3: Retrieve examination scores.
SELECT Score
FROM Results;
The FROM clause helps SQL know where to find the information you need. Without specifying the correct table, the database may not understand where to retrieve the information.
Sometimes, column names in a database may look confusing or too technical. You may want to give a column a simpler name when displaying your results.
SQL allows you to do this using something called an alias.
An alias is a temporary name given to a column or table when displaying query results. It does not permanently change the original database name.
Example 1:
SELECT Name AS Student_Name
FROM Students;
In this example, the Name column will appear as Student_Name in the query result.
Example 2: Give the Age column a new display name.
SELECT Age AS Student_Age
FROM Students;
Example 3: Rename multiple columns in the results.
SELECT Name AS Student_Name,
Age AS Student_Age,
Class AS School_Class
FROM Students;
You can also use spaces in aliases by placing the new name inside quotation marks or backticks, depending on the database system.
SELECT Name AS 'Student Full Name'
FROM Students;
Remember: An alias changes how the column appears in your query result, not the original column name stored in the database.
Now it is time to practise what you have learned. Imagine you have been employed as a junior database assistant at Bright Future Academy.
The school has a database called SchoolDB and a table called Students. The table contains the following information:
| StudentID | Name | Age | Class | City |
|---|---|---|---|---|
| 201 | John | 13 | JSS 2 | Ibadan |
| 202 | Grace | 12 | JSS 1 | Lagos |
| 203 | Michael | 14 | JSS 3 | Abuja |
| 204 | Sarah | 13 | JSS 2 | Ibadan |
| 205 | David | 12 | JSS 1 | Lagos |
Write a query to display every column in the Students table.
SELECT * FROM Students;
Write a query to display only the names of all students.
SELECT Name FROM Students;
Write a query to display the names and ages of all students.
SELECT Name, Age
FROM Students;
Write a query to display the names and cities of all students.
SELECT Name, City
FROM Students;
Write a query that displays the Name column as Student_Name and the Age column as Student_Age.
SELECT Name AS Student_Name,
Age AS Student_Age
FROM Students;
In this module, you have learned:
Answer the following questions to test your understanding of this module.
You are helping a supermarket in Ibadan organise its product information. The supermarket has a database table called Products with these columns:
Write SQL queries to perform the following tasks:
Try writing the queries yourself before checking your answers with your instructor or SQL practice environment.
You have successfully completed Module 3 of Basic Queries. You now understand how to retrieve information from database tables using the SELECT statement.
Keep practising your SQL commands. Remember, every database expert started as a beginner!
Module 4: Filtering Data with WHERE
In the next module, you will learn how to find specific information from a database instead of displaying every record. You will discover how to use conditions and comparison operators to search for exactly what you need.
CRMNuggets โ Learn. Practice. Get Certified.