Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Filtering and Sorting Data with SELECT, WHERE, and ORDER BY

Recommended resources

Welcome back! In our previous lesson, we explored the "blueprint" of a relational database—the schema. You learned how tables are structured with primary and foreign keys to create relationships and ensure data integrity. Now that you understand the architecture, it's time to learn the language used to communicate with these databases: Structured Query Language, or SQL.

This lesson is your first hands-on introduction to SQL. We will focus on the three most fundamental commands you will use in nearly every data task: SELECT, WHERE, and ORDER BY. By the end of our session, you'll be able to ask a database for specific columns of information, filter the results to find only the rows that matter to you, and sort the output in a meaningful way. This is the foundational skill for any data professional.

The Anatomy of a Basic Query

At its core, a SQL query is a question you ask a database. The most basic questions have three parts:

  1. What data do you want to see? (Which columns?)
  2. Where is this data located? (Which table?)
  3. What conditions must the data meet? (Which rows?)

SQL provides specific clauses to answer each of these questions. Let's start with the first two.


Step 1: Selecting Columns with SELECT and FROM

The SELECT statement is used to specify the columns you want your query to return. The FROM clause specifies the table where these columns reside.

Imagine you're at a library. FROM is like choosing which book to open (e.g., FROM Movies), and SELECT is like choosing which information to read from each page (e.g., SELECT Title, Director).

To get a feel for the syntax and try it yourself, we'll use the interactive tutorial from SQLBolt. It provides a simple, hands-on environment to write your first queries.

SQLBolt - Learn SQL - SQL Lesson 1: SELECT queries 101

This first SQLBolt lesson will introduce you to the fundamental SELECT and FROM clauses. You'll learn how to retrieve specific columns by name and how to use the * wildcard to get all columns.

Read the explanatory text, which starts with the introduction. Then, complete all five tasks in the exercise section at the bottom. The goal is to get comfortable selecting different columns from the Movies table.

As you saw in the exercise, you have two main options:

  • SELECT Title, Director FROM Movies; fetches only the Title and Director columns.
  • SELECT * FROM Movies; is a useful shorthand to fetch all columns from the table.

Step 2: Filtering Rows with WHERE

Retrieving all the rows from a table is often impractical, especially when tables contain millions or even billions of records. The WHERE clause is the tool you'll use to filter these rows and retrieve only the ones that meet specific conditions.

For example, you might want to find all movies released after the year 2000, or only the movies directed by a specific person. The WHERE clause makes this possible.

Let's return to SQLBolt for another interactive lesson, this time focusing on constraints.

SQLBolt - Learn SQL - SQL Lesson 2: Queries with constraints (Pt. 1)

This lesson introduces the WHERE clause for filtering. It also provides a handy reference table of the different operators you can use for filtering numbers and text.

First, read the brief explanation of the WHERE clause. Then, study the table of operators you can use for filtering. Pay attention to standard comparison operators (=, >), range operators (BETWEEN), and list operators (IN). Finally, complete the exercises at the bottom to practice writing filtered queries.

The WHERE clause is incredibly powerful. By combining conditions with AND and OR, you can build precise filters for your data. For example: WHERE Year > 2005 AND Director = 'Pete Docter';.


Step 3: Sorting Your Results with ORDER BY

You've selected your columns and filtered your rows. The final step is often to present the data in a logical order. The ORDER BY clause lets you sort your result set by one or more columns.

By default, ORDER BY sorts data in ascending order (ASC). If you want to sort in descending order, you must explicitly add the DESC keyword. For example, to get a list of movies sorted from newest to oldest, you would use ORDER BY Year DESC;.


Putting It All Together in Practice

Now that you've seen the individual components, let's watch someone put them all together in a real database environment. The following video from Adam Finer provides a great walkthrough of writing basic SELECT statements, applying filters with WHERE, and sorting the results with ORDER BY.

Learn Basic SQL in 15 Minutes Business Intelligence For Beginners SQL Tutorial For Beginners 13

This video demonstrates the core concepts you've just learned in a real SQL client. It reinforces the syntax for SELECT, WHERE, and ORDER BY, and also introduces a few other useful operators.

Please watch the following segments: SELECT and FROM: Watch from the beginning to see how to select all columns (*) versus specific columns, and how to use the AS keyword to create column aliases. WHERE: The next section, from filtering rows, is the most detailed. It covers filtering on numbers, text (LIKE), multiple values (IN), ranges (BETWEEN), and checking for empty values (IS NULL). ORDER BY: Finally, watch the segment on sorting results to see ORDER BY in action with both ascending (the default) and descending (DESC) order.


Conclusion

In this lesson, you wrote your first SQL queries and learned the essential commands for retrieving and organizing data. These three clauses form the backbone of almost every query you will write as a data engineer.

Here are your key takeaways:

  • SELECT specifies the columns you want to see. SELECT * is a shortcut for all columns.
  • FROM specifies the table you are querying.
  • WHERE filters the rows based on one or more conditions, using operators like =, >, LIKE, and IN.
  • ORDER BY sorts the final result set, with ASC for ascending and DESC for descending order.

You have taken a significant first step from understanding data structure to actively engaging with data. In the next lesson, we will build upon this foundation. You'll learn how to go beyond simply retrieving rows and start calculating summaries of your data using aggregate functions and the GROUP BY clause.