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

Module Five

๐Ÿ“– 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.
7

MODULE 5: SORTING AND LIMITING QUERY RESULTS

Module Introduction

Imagine you have a school database containing the names and scores of 500 students. How can you arrange the students from the highest score to the lowest score? How can you display only the first five students?

SQL provides commands that help us arrange information and control how many records appear in our results. In this module, you will learn how to use ORDER BY, ASC, DESC, LIMIT, and OFFSET.

Learning Objectives

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

  • Explain what sorting means in SQL.
  • Use ORDER BY to arrange query results.
  • Sort records in ascending and descending order.
  • Sort data using more than one column.
  • Use LIMIT to control the number of records displayed.
  • Use OFFSET to skip records in query results.

Lesson 1: Understanding Sorting in SQL

Sorting means arranging information in a particular order. For example, you can arrange students alphabetically by name or arrange their examination scores from the highest to the lowest.

SQL uses the ORDER BY command to sort records.

Example: Student Table

StudentID Name Score
1Ade75
2Bola90
3Chika65
4David85

To arrange students according to their scores, use:

SELECT * FROM Students
ORDER BY Score;

This query arranges the students by score. When no direction is specified, SQL uses ascending order by default.

Lesson 2: Ascending Order (ASC)

Ascending order means arranging information from the smallest to the largest, or alphabetically from A to Z.

Examples include:

  • Numbers: 1, 2, 3, 4, 5
  • Scores: 40, 60, 75, 90
  • Names: Ade, Bola, Chika, David

Use the ASC keyword to arrange records in ascending order.

SELECT * FROM Students
ORDER BY Score ASC;

The query displays students from the lowest score to the highest score.

Lesson 3: Descending Order (DESC)

Descending order means arranging information from the largest to the smallest, or alphabetically from Z to A.

Examples include:

  • Numbers: 100, 90, 80, 70
  • Scores: 95, 85, 75, 65
  • Names: David, Chika, Bola, Ade

Use the DESC keyword to arrange records in descending order.

SELECT * FROM Students
ORDER BY Score DESC;

This query displays students from the highest score to the lowest score. It can be useful when preparing a list of top-performing students.

Lesson 4: Sorting Using Multiple Columns

Sometimes, you may want to sort information using more than one column. For example, you may want to arrange students by class and then by their scores.

Consider this query:

SELECT * FROM Students
ORDER BY Class ASC, Score DESC;

This query:

  1. Arranges students by class in ascending order.
  2. Within each class, arranges students from the highest score to the lowest.

SQL uses the second sorting column when records have the same value in the first sorting column.

Lesson 5: Limiting Results with LIMIT

Imagine a supermarket database contains 10,000 products. You only want to display the first five products. You do not need to display all 10,000 records.

SQL provides the LIMIT keyword to control the number of records returned by a query.

Example

SELECT * FROM Products
LIMIT 5;

This query displays only five records from the Products table.

Finding the Five Highest Scores

SELECT * FROM Students
ORDER BY Score DESC
LIMIT 5;

This query sorts students from the highest score to the lowest and displays only the first five students.

Lesson 6: Understanding OFFSET

OFFSET tells SQL how many records to skip before displaying the results. It is often used with LIMIT when displaying information in pages.

For example:

SELECT * FROM Students
LIMIT 5 OFFSET 5;

This query skips the first five records and displays the next five records.

Understanding Pagination

Pagination means dividing a large collection of records into smaller pages. Websites often use pagination to make information easier to browse.

  • Page 1: LIMIT 5 OFFSET 0
  • Page 2: LIMIT 5 OFFSET 5
  • Page 3: LIMIT 5 OFFSET 10
  • Page 4: LIMIT 5 OFFSET 15

Each page displays five records, while OFFSET determines where the next page begins.

Lesson 7: Practical SQL Examples

Example 1: Arrange Students Alphabetically

SELECT * FROM Students
ORDER BY Name ASC;

Example 2: Display the Lowest Scores First

SELECT * FROM Students
ORDER BY Score ASC;

Example 3: Display the Three Best Students

SELECT * FROM Students
ORDER BY Score DESC
LIMIT 3;

Example 4: Display the Most Expensive Products

SELECT * FROM Products
ORDER BY Price DESC
LIMIT 5;

Example 5: Display Products from Cheapest to Most Expensive

SELECT * FROM Products
ORDER BY Price ASC;

Lesson 8: Practical Activities

Assume you have a table called Students with these columns:

  • StudentID
  • Name
  • Class
  • Age
  • Score

Write SQL queries to complete the following tasks:

  1. Display all students alphabetically by name.
  2. Display students from the highest score to the lowest score.
  3. Display only the first five students.
  4. Display the three students with the highest scores.
  5. Display students from the youngest to the oldest.
  6. Display the next five students after skipping the first five records.

Module Summary

In this module, you have learned how to arrange and control SQL query results.

  • ORDER BY: Arranges records in a particular order.
  • ASC: Sorts records from smallest to largest or A to Z.
  • DESC: Sorts records from largest to smallest or Z to A.
  • LIMIT: Controls the number of records returned.
  • OFFSET: Skips a specified number of records.

Assessment Questions

  1. What is the purpose of ORDER BY in SQL?
  2. Which keyword sorts records from the smallest to the largest?
  3. What does DESC mean?
  4. Which SQL keyword limits the number of records displayed?
  5. What does OFFSET do?
  6. Write a query that displays students from the highest score to the lowest.
  7. Write a query that displays only the first ten products.
  8. What happens when you use ORDER BY without specifying ASC or DESC?
  9. Can SQL sort records using more than one column?
  10. What is pagination?

Practical Challenge

Create a simple SQL query that displays the five most expensive products in a supermarket database.

Hint: Use SELECT, ORDER BY, DESC, and LIMIT.

What Is Next?

In Module 6, you will learn about Basic SQL Functions, including COUNT(), SUM(), AVG(), MIN(), and MAX(). These functions help you calculate useful information from database records.

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