Welcome to Module 5 of Introduction to SQL for Data Analysis.
In the previous module, you learned how to filter database records using
the WHERE clause.
Imagine you are working in a supermarket in Ibadan. The manager gives you a list of 500 products and asks you to arrange them from the cheapest to the most expensive.
You don't need to arrange the products manually. SQL can do it for you! In this module, you will learn how to sort and organise information stored in a database.
Data sorting means arranging information in a particular order so that it becomes easier to read, understand and analyse.
You can sort information in different ways:
SQL uses the ORDER BY clause to arrange query results.
The ORDER BY clause tells SQL which column to use when arranging
records.
SELECT column_name
FROM table_name
ORDER BY column_name;
Let's use a school database to understand how it works.
| student_id | student_name | age | score |
|---|---|---|---|
| 1 | Samuel | 13 | 75 |
| 2 | Mary | 12 | 90 |
| 3 | Peter | 14 | 65 |
| 4 | Esther | 13 | 85 |
| 5 | David | 12 | 70 |
To arrange students alphabetically by their names, use:
SELECT *
FROM Students
ORDER BY student_name;
SQL will arrange the names alphabetically, starting with David, Esther, Mary, Peter and Samuel.
When you do not specify a sorting direction, SQL uses ascending order by default.
ASC means ascending order. It arranges values from the smallest
to the largest or alphabetically from A to Z.
Examples include:
SELECT *
FROM Students
ORDER BY age ASC;
The youngest students will appear first, followed by older students.
SELECT *
FROM Products
ORDER BY price ASC;
This query displays the cheapest products first.
SELECT student_name, score
FROM Students
ORDER BY score ASC;
This displays students from the lowest examination score to the highest.
DESC means descending order. It arranges values from the largest
to the smallest or alphabetically from Z to A.
Examples include:
SELECT *
FROM Students
ORDER BY score DESC;
This query displays students with the highest scores first.
SELECT *
FROM Products
ORDER BY price DESC;
This helps a supermarket manager identify the most expensive products.
SELECT *
FROM Students
ORDER BY age DESC;
This displays older students before younger students.
Sometimes, sorting by only one column is not enough. You may want to arrange records using two or more columns.
For example, a school may want to arrange students by class and then alphabetically by their names.
SELECT *
FROM Students
ORDER BY class ASC, student_name ASC;
SQL first arranges students according to their classes. Within each class, it arranges their names alphabetically.
Imagine two students have the same examination score. You can sort by score first and then by name.
SELECT student_name, score
FROM Students
ORDER BY score DESC, student_name ASC;
This arranges students by their highest scores first. If two students have the same score, their names are arranged alphabetically.
You can combine the WHERE clause and the ORDER BY
clause in one SQL query.
The WHERE clause filters the records, while
ORDER BY arranges the records that remain.
Find students who scored more than 70 and arrange them from the highest score to the lowest.
SELECT *
FROM Students
WHERE score > 70
ORDER BY score DESC;
Find supermarket products that cost less than โฆ5,000 and arrange them from the cheapest to the most expensive.
SELECT *
FROM Products
WHERE price < 5000
ORDER BY price ASC;
Important: In a basic SQL query, write
WHERE before ORDER BY.
Imagine a supermarket has 10,000 products, but the manager only wants to see the first five most expensive products.
You can use the LIMIT clause to control how many records
SQL returns.
SELECT column_name
FROM table_name
LIMIT number;
SELECT *
FROM Students
LIMIT 5;
This query displays a maximum of five records.
SELECT *
FROM Products
ORDER BY price DESC
LIMIT 3;
SQL first sorts products from the highest price to the lowest and then displays only three records.
SELECT student_name, score
FROM Students
ORDER BY score DESC
LIMIT 5;
This is useful when identifying the top five students in a class.
Sometimes, you want to skip a number of records before displaying the next set of results.
SQL uses OFFSET for this purpose.
SELECT column_name
FROM table_name
LIMIT number OFFSET number;
SELECT *
FROM Students
LIMIT 5 OFFSET 5;
This skips the first five records and returns the next five records, if they are available.
OFFSET is useful when building websites that display database records across different pages.
For example, the second page can use:
SELECT *
FROM Students
LIMIT 10 OFFSET 10;
This technique is called pagination. It helps websites display large amounts of information in smaller sections.
Sometimes, a database contains repeated values.
The DISTINCT keyword helps you display only unique values
in your query results.
Imagine a school has students from different states, and several students come from the same state.
To display each state only once, write:
SELECT DISTINCT state
FROM Students;
If 20 students come from Oyo State, SQL will display Oyo only once in this result.
Find the different product categories available in a supermarket.
SELECT DISTINCT category
FROM Products;
This can display categories such as Food, Drinks, Electronics and Clothing without repeating them.
Remember: DISTINCT removes duplicate combinations of the
selected columns, not necessarily duplicate records from the original table.
One of the most useful things about SQL is that you can combine different commands to answer more detailed questions.
Find the three students with the highest examination scores.
SELECT student_name, score
FROM Students
ORDER BY score DESC
LIMIT 3;
Find products that cost less than โฆ10,000, arrange them from cheapest to most expensive, and display only five products.
SELECT *
FROM Products
WHERE price < 10000
ORDER BY price ASC
LIMIT 5;
Find the five highest-scoring students in JSS 2.
SELECT student_name, score
FROM Students
WHERE class = 'JSS 2'
ORDER BY score DESC
LIMIT 5;
| Incorrect Approach | Correct Approach |
|---|---|
| ORDER price BY | ORDER BY price |
| ORDER BY price HIGH | ORDER BY price DESC |
| ORDER BY student_name A-Z | ORDER BY student_name ASC |
| LIMIT five | LIMIT 5 |
| ORDER BY before WHERE | WHERE before ORDER BY |
Remember:
ASC means ascending order.DESC means descending order.ORDER BY sorts query results.LIMIT controls the number of records returned.OFFSET skips a specified number of records.DISTINCT returns unique values or combinations.You have been invited to help a school in Ibadan organise its student database.
The Students table contains these columns:
Write SQL queries to complete the following tasks:
-- 1. Arrange students alphabetically
SELECT *
FROM Students
ORDER BY student_name ASC;
-- 2. Youngest students first
SELECT *
FROM Students
ORDER BY age ASC;
-- 3. Highest scores first
SELECT *
FROM Students
ORDER BY score DESC;
-- 4. Display five students
SELECT *
FROM Students
LIMIT 5;
-- 5. Find the top three students
SELECT student_name, score
FROM Students
ORDER BY score DESC
LIMIT 3;
-- 6. Students who scored above 60
SELECT *
FROM Students
WHERE score > 60
ORDER BY score DESC;
-- 7. Display different states
SELECT DISTINCT state
FROM Students;
-- 8. Skip the first five records
SELECT *
FROM Students
LIMIT 5 OFFSET 5;
Question 1: Which SQL clause is used to sort records?
Question 2: What does ASC mean?
Question 3: Which keyword sorts records from highest to lowest?
Question 4: Which SQL clause limits the number of records returned?
Question 5: Which keyword helps remove duplicate values from query results?
Question 6: What does OFFSET do?
Question 7: Which query finds the three most expensive products?
Question 8: Which clause should appear before ORDER BY in a basic filtered query?
Question 9: What happens when you write ORDER BY student_name ASC?
Question 10: Which query displays different product categories without repeating them?
In this module, you learned how to organise database records and control the results returned by SQL queries.
ORDER BY arranges records.ASC sorts from smallest to largest or A to Z.DESC sorts from largest to smallest or Z to A.WHERE filters records before sorting.LIMIT controls how many records are displayed.OFFSET skips records.DISTINCT displays unique values or combinations.You can now organise thousands of database records and quickly identify the information you need.
In the next module, you will learn how SQL can calculate totals, averages, minimum values, maximum values and the number of records in a database.
You will explore these important SQL functions:
COUNT() โ Counts records.SUM() โ Adds numerical values.AVG() โ Calculates averages.MIN() โ Finds the smallest value.MAX() โ Finds the largest value.GROUP BY โ Groups records with matching values.CRMNuggets โ Learn. Practice. Get Certified.