Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to SQL for Data Analysis ยท Lesson 4 of 9

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 Using the SELECT Statement

Module Overview

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.

Learning Objectives

By the end of this module, you should be able to:

  • Explain what the SELECT statement does.
  • Retrieve all records from a database table.
  • Select specific columns from a table.
  • Retrieve information from multiple columns.
  • Use aliases to rename column headings.
  • Remove duplicate results using DISTINCT.
  • Write simple SQL queries for data analysis.

1. What Is the SELECT Statement?

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:

  • All the students in a school.
  • The names of customers in a supermarket.
  • The prices of products in a store.
  • The names and ages of students in JSS 2.

The SELECT statement helps you retrieve this information.

2. Understanding the Basic SELECT Syntax

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:

  • SELECT: Tells the database that you want to retrieve information.
  • column_name: The name of the column you want to see.
  • FROM: Tells the database where to get the information.
  • table_name: The name of the table containing the information.
  • ; Marks the end of the SQL statement.

3. Understanding Our Practice Database

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.

4. Retrieving All Columns Using SELECT *

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.

5. Selecting a Specific Column

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.

6. Selecting Multiple Columns

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.

7. Understanding Column Aliases

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.

8. Using Aliases Without AS

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.

9. Removing Duplicate Records with DISTINCT

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.

10. Understanding the Difference Between SELECT and DISTINCT

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.

11. Using SELECT in a Supermarket

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
1Rice85000Food
2Beans45000Food
3Cooking Oil12000Groceries
4Milk8000Groceries

Here are some useful queries:

Display all products:

SELECT * FROM Products;

Display only product names:

SELECT product_name
FROM Products;

Display product names and prices:

SELECT product_name, price
FROM Products;

Display unique product categories:

SELECT DISTINCT category
FROM Products;

12. Important Rules When Writing SELECT Queries

  • Write SELECT before the column names.
  • Use commas when selecting multiple columns.
  • Use FROM to identify the table.
  • Make sure column and table names are correct.
  • Use the asterisk (*) when you need all columns.
  • Use DISTINCT when you want unique results.
  • End SQL statements with a semicolon for good practice.

13. Common Mistakes Beginners Make

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.

14. Module Summary

In this module, you have learned:

  • SELECT is used to retrieve information from databases.
  • SELECT * displays all columns in a table.
  • You can select specific columns by writing their names.
  • Multiple columns are separated by commas.
  • AS creates a temporary column alias.
  • DISTINCT removes duplicate combinations from query results.
  • SQL can retrieve information from school and supermarket databases.

15. Knowledge Check

Answer these questions to test your understanding.

  1. What is the purpose of the SELECT statement?
  2. What does the asterisk (*) mean in SQL?
  3. Which keyword tells SQL where to retrieve information from?
  4. Write a query that displays all records in the Students table.
  5. Write a query that displays only student names.
  6. How do you select three columns in one query?
  7. What is a column alias?
  8. What does the AS keyword do?
  9. What is the purpose of DISTINCT?
  10. Write a query that displays unique product categories from a Products table.

16. Practical Assignment

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:

  1. Display all the records in the Products table.
  2. Display only the product names.
  3. Display product names and prices.
  4. Display product names, prices and categories.
  5. Display product names using the alias Item Name.
  6. Display all unique product categories.

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.

Key Terms to Remember

  • SELECT: Retrieves information from a database.
  • FROM: Identifies the table containing the information.
  • Asterisk (*): Represents all columns.
  • Alias: A temporary alternative name for a column or table.
  • AS: Creates an alias.
  • DISTINCT: Removes duplicate combinations from query results.
  • Query: An instruction sent to a database.

Congratulations!

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.

๐Ÿ† 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.
โ†’