CHAPTER 08
Beginner
Sorting, Limiting, and Searching Data
Updated: May 16, 2026
6 min read
# CHAPTER 8
Sorting, Limiting, and Searching Data
1. Introduction
In Chapter 7, we learned how to extract raw data usingSELECT and WHERE. However, when you search for "Laptops" on Amazon, the database doesn't just vomit a random list of products onto your screen. It displays exactly 20 products per page (LIMIT), sorted by the Highest Price (ORDER BY), and matches laptops that contain the word "Gaming" somewhere in the title (LIKE). In this chapter, we will learn how to format our raw data into user-friendly layouts.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Sort data alphabetically or numerically using
ORDER BY.
-
Constrain the number of returned rows using
LIMIT.
-
Understand pagination using
OFFSET.
-
Perform fuzzy text searches using the
LIKEoperator and%wildcards.
3. Sorting Data (ORDER BY)
By default, MySQL returns rows in the exact order they were inserted into the hard drive. To sort them logically, we append the ORDER BY clause to the very end of our query.
- ASC (Ascending): A to Z, or 1 to 100. (This is the default).
- DESC (Descending): Z to A, or 100 to 1.
sql
4. Limiting Results (LIMIT)
If a table has 5 million rows, running a SELECT query might crash your terminal as it tries to print them all. We use LIMIT to restrict the maximum number of rows returned.
sql
5. Pagination (OFFSET)
When you click "Page 2" on a web store, the backend database uses OFFSET to skip the first batch of products.
sql
6. Fuzzy Searching (LIKE and Wildcards)
The = operator is strictly exact. WHERE name = 'John' will NOT find "Johnathan".
To perform fuzzy text searches (like a search bar on a website), we use the LIKE operator combined with the % wildcard.
The % symbol means "any number of characters can go here".
sql
7. Mini Project: Product Search System
You are building the backend search engine for an E-commerce store. A user searches for the word "Phone", clicks "Sort by Lowest Price", and wants to see the first 5 results.
sql
8. The Danger of Leading Wildcards
CRITICAL PERFORMANCE WARNING: When you queryLIKE 'Jo%', MySQL uses its internal Indexes (like the index of a book) to instantly jump to the J's. It is incredibly fast.
When you query LIKE '%Phone%' (putting a % at the very beginning), MySQL *cannot* use the Index. It is forced to perform a Full Table Scan, physically reading every single letter of every single row in the database. On a table with 10 million rows, a leading wildcard search will cause the server to hang and potentially crash.
9. Best Practices
- Clause Order Matters: SQL clauses must be written in a very specific order, or the code will crash. The golden rule of order is:
-
1.
SELECT
-
2.
FROM
-
3.
WHERE
-
4.
ORDER BY
-
5.
LIMIT
10. Exercises
-
1.
Write a query to find the 3 youngest (most recently hired) employees in the
employeestable. (Hint: sort the date descending).
- 2. Write a query to find all users whose email address starts with "admin".
11. MCQ Quiz with Answers
Question 1
If you want to sort a numerical column from the largest number to the smallest number, which keyword must follow the ORDER BY clause?
Question 2
What is the mathematical and performance problem with executing WHERE name LIKE '%Smith' on a massive database?
12. Interview Questions
- Q: Explain how a web application implements "Pagination" (Page 1, Page 2, Page 3) on the backend using SQL commands.
-
Q: Describe the performance implications of utilizing the
LIKEoperator with leading wildcards versus trailing wildcards.
13. FAQs
Q: Can I sort by multiple columns at once? A: Yes!ORDER BY department ASC, salary DESC; This will first group everyone alphabetically by department. Then, *within* each department, it will sort the employees by the highest salary to the lowest!
14. Summary
Raw data is useless to an end-user. By chaining theORDER BY and LIMIT clauses, you transform chaotic data blocks into structured, paginated "Top 10" lists. By mastering the LIKE operator, you empower your applications with flexible search engine capabilities.
15. Next Chapter Recommendation
OurWHERE clauses have been relatively simple so far. But what if we need to find employees who work in Sales AND make over $80,000, OR work in IT but do NOT have a company car? In Chapter 9: SQL Operators and Conditions, we will master complex boolean logic.