Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Basic Queries in SQL ยท Lesson 4 of 10

Module Three

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
4

MODULE 3: RETRIEVING DATA WITH SELECT

Module Overview

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.

Lesson 1: Introduction to the SELECT Statement

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:

  • SELECT: Tells the database that you want to retrieve information.
  • *: Means all columns.
  • FROM: Identifies the table where the information is stored.
  • Students: The name of the database table.
  • ; Marks the end of the SQL statement.

The SELECT command does not normally change the information in your database. It simply displays the information you requested.

Lesson 2: Retrieving All Records Using SELECT *

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 *?

  • When you want to see all information in a table.
  • When you are learning about a new database table.
  • When you need to inspect the available columns and records.

Professional Tip: In large databases, selecting only the columns you need can be more efficient than retrieving everything.

Lesson 3: Selecting Specific Columns

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.

Lesson 4: Understanding the FROM Clause

The FROM clause tells SQL which table contains the information you want to retrieve.

Imagine a school database contains these tables:

  • Students
  • Teachers
  • Subjects
  • Results

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.

Lesson 5: Using Column Aliases

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.

Lesson 6: Practical SELECT Query Exercises

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

Exercise 1: Display All Student Records

Write a query to display every column in the Students table.

SELECT * FROM Students;

Exercise 2: Display Only Student Names

Write a query to display only the names of all students.

SELECT Name FROM Students;

Exercise 3: Display Names and Ages

Write a query to display the names and ages of all students.

SELECT Name, Age
FROM Students;

Exercise 4: Display Student Names and Cities

Write a query to display the names and cities of all students.

SELECT Name, City
FROM Students;

Exercise 5: Use Column Aliases

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;

Module Summary

In this module, you have learned:

  • The SELECT statement retrieves information from a database.
  • The asterisk (*) selects all columns in a table.
  • You can select specific columns by writing their names.
  • Commas separate multiple column names in a SELECT statement.
  • The FROM clause identifies the table containing the information.
  • Column aliases provide temporary alternative names for query results.
  • SELECT queries allow you to view information without changing the original records.

Module Assessment

Answer the following questions to test your understanding of this module.

  1. What is the purpose of the SELECT statement?
  2. What does the asterisk (*) mean in SQL?
  3. Which SQL keyword identifies the table from which information is retrieved?
  4. Write an SQL query to display all records from a table called Employees.
  5. Write an SQL query to display only the Name column from a table called Students.
  6. How do you select two or more columns in an SQL query?
  7. What is a column alias?
  8. Does using an alias permanently change the original column name?
  9. Write a query to display Name and Age from a table called Students.
  10. Write a query to display the Name column as Customer_Name from a table called Customers.

Practical Challenge

You are helping a supermarket in Ibadan organise its product information. The supermarket has a database table called Products with these columns:

  • ProductID
  • ProductName
  • Price
  • Category
  • Quantity

Write SQL queries to perform the following tasks:

  1. Display all product information.
  2. Display only the product names.
  3. Display product names and prices.
  4. Display product names and quantities.
  5. Display ProductName as Item_Name and Price as Item_Price.

Try writing the queries yourself before checking your answers with your instructor or SQL practice environment.

Congratulations!

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!

Coming Up Next

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.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’