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.
By the end of this module, you should be able to:
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.
| StudentID | Name | Score |
|---|---|---|
| 1 | Ade | 75 |
| 2 | Bola | 90 |
| 3 | Chika | 65 |
| 4 | David | 85 |
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.
Ascending order means arranging information from the smallest to the largest, or alphabetically from A to Z.
Examples include:
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.
Descending order means arranging information from the largest to the smallest, or alphabetically from Z to A.
Examples include:
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.
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:
SQL uses the second sorting column when records have the same value in the first sorting column.
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.
SELECT * FROM Products
LIMIT 5;
This query displays only five records from the Products table.
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.
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.
Pagination means dividing a large collection of records into smaller pages. Websites often use pagination to make information easier to browse.
Each page displays five records, while OFFSET determines where the next page begins.
SELECT * FROM Students
ORDER BY Name ASC;
SELECT * FROM Students
ORDER BY Score ASC;
SELECT * FROM Students
ORDER BY Score DESC
LIMIT 3;
SELECT * FROM Products
ORDER BY Price DESC
LIMIT 5;
SELECT * FROM Products
ORDER BY Price ASC;
Assume you have a table called Students with these columns:
Write SQL queries to complete the following tasks:
In this module, you have learned how to arrange and control SQL query results.
Create a simple SQL query that displays the five most expensive products in a supermarket database.
Hint: Use SELECT, ORDER BY, DESC, and LIMIT.
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.