A Quick Guide to SQL — Chapter 2: Filtering and Sorting Data

Sajjad Hadi
3 min readJun 8, 2023

--

This image is generated by AI for the Chapter 2 of A Quick Guide to SQL.

In the previous chapter we learned about tha basics of SQL. In this lesson, we will dive deeper into SQL by exploring how to filter and sort data using various techniques. Filtering allows us to extract specific data based on conditions, while sorting enables us to arrange the retrieved data in a desired order. Let’s get started!

Chapters of This Series

  1. Chapter 1: Introduction and Basic Syntax
  2. Chapter 2: Filtering and Sorting Data
  3. Chapter 3: Manipulating Data
  4. Chapter 4: Querying Multiple Tables with JOIN
  5. Chapter 5: Aggregating Data
  6. Chapter 6: Modifying Data
  7. Chapter 7: Advanced SQL Concepts
  8. Chapter 8: Modifying Table Structure

1. Review of the SELECT Statement

Before we proceed, let’s review the basic structure of the SELECT statement:

SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • SELECT: Specifies the columns to be retrieved from the table.
  • FROM: Specifies the table from which the data will be retrieved.
  • WHERE: Optional clause used to filter the data based on specified conditions.

2. Using Comparison Operators in WHERE

Clause SQL provides various comparison operators such as greater than (>), less than (<), equal to (=), and not equal to (!= or <>). Let’s retrieve employees whose age is greater than 30:

SELECT * FROM employees WHERE age > 30;

3. Combining Multiple Conditions using AND and OR Operators

To filter data based on multiple conditions, we can use the logical operators AND and OR. For example, let’s retrieve employees who are either in the age range of 25–30 or have a salary greater than $50,000:

SELECT * FROM employees WHERE (age >= 25 AND age <= 30) OR salary > 50000;

The parentheses help group the conditions and ensure the logical operators are applied correctly.

4. Sorting Retrieved Data using ORDER BY Clause

To sort the retrieved data in a specific order, we use the ORDER BY clause. Let’s retrieve employees’ data sorted by their salary in ascending order:

SELECT * FROM employees ORDER BY salary ASC;

The ASC keyword specifies ascending order. If not explicitly mentioned, ASC is the default sorting order. To sort in descending order, we use the DESC keyword.

5. Sorting by Multiple Columns

In some cases, we may need to sort data by multiple columns. Let’s retrieve employees’ data sorted first by salary in descending order and then by age in ascending order:

SELECT * FROM employees ORDER BY salary DESC, age ASC;

Here, we provide multiple columns separated by commas in the ORDER BY clause, indicating the order of sorting for each column.

6. Limiting the Number of Retrieved Rows using LIMIT

To limit the number of rows retrieved, we can use the LIMIT clause. Let’s retrieve the top 5 highest-paid employees:

SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

The LIMIT clause restricts the number of rows returned to the specified value (in this case, 5).

7. Conclusion

In this lesson, we explored how to filter and sort data in SQL. We reviewed the SELECT statement and learned how to use comparison operators in the WHERE clause to filter data. We also saw how to combine multiple conditions using the AND and OR operators. Additionally, we discovered the ORDER BY clause to sort retrieved data, and even learned how to sort by multiple columns. Finally, we saw how to limit the number of rows returned using the LIMIT clause. With these techniques, you can efficiently filter and sort data to meet specific requirements in SQL.

If you found this course helpful and would like to explore more free courses, I invite you to follow my account on Medium and connect with me on LinkedIn. I regularly share valuable content on these platforms.

--

--