SELECT, WHERE
& ORDER BY
Master the foundational SQL clauses to select columns, filter rows, and sort results from any database table.
What Is SQL?
SQL (Structured Query Language) is the standard language for communicating with relational databases. When you pull a sales report, filter a customer list, or rank products, SQL is almost always working behind the scenes to get the data.
The Big Three Clauses
Meet the Products Table
We will use this sample table throughout the lesson. Each row is a product record with four columns.
| id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 999 | Electronics |
| 2 | Desk Chair | 275 | Furniture |
| 3 | Monitor | 450 | Electronics |
| 4 | Notebook | 12 | Supplies |
| 5 | Keyboard | 85 | Electronics |
The SELECT Statement
FROM table_name;
Filtering with WHERE
FROM Products
WHERE price > 100;
WHERE Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | price = 450 |
| <> or != | Not equal to | category <> 'Supplies' |
| > , >= | Greater than (or equal) | price >= 100 |
| < , <= | Less than (or equal) | price < 500 |
| BETWEEN | Inclusive range | price BETWEEN 50 AND 300 |
| LIKE | Pattern match | name LIKE 'Key%' |
| IN | Matches a list | category IN ('Electronics','Supplies') |
Sorting with ORDER BY
FROM Products
ORDER BY price DESC;
The SQL Execution Pipeline
SELECT vs WHERE: Key Difference
| Aspect | SELECT | WHERE |
|---|---|---|
| Operates on | Columns | Rows |
| Purpose | Choose which fields appear | Choose which records appear |
| Analogy | Highlighting spreadsheet columns | Hiding spreadsheet rows |
| Optional? | Required in every query | Optional: omit to return all rows |
How to Write a Query
Example 1: Basic SELECT
Goal: Retrieve the name and price of every product.
FROM Products;
| name | price |
|---|---|
| Laptop | 999 |
| Desk Chair | 275 |
| Monitor | 450 |
| Notebook | 12 |
| Keyboard | 85 |
Example 2: WHERE Filter
Goal: Find all Electronics that cost more than 100.
FROM Products
WHERE category = 'Electronics'
AND price > 100;
| name | price |
|---|---|
| Laptop | 999 |
| Monitor | 450 |
Example 3: Full Query
Goal: List all products under 500, sorted by price from highest to lowest.
FROM Products
WHERE price < 500
ORDER BY price DESC;
| name | price | category |
|---|---|---|
| Monitor | 450 | Electronics |
| Desk Chair | 275 | Furniture |
| Keyboard | 85 | Electronics |
| Notebook | 12 | Supplies |
Common Mistakes
Summary Cheat Sheet
| Clause | What It Does | Required? | Position |
|---|---|---|---|
| SELECT | Picks columns to display | Yes | 1st (in writing) |
| FROM | Names the source table | Yes | 2nd |
| WHERE | Filters rows by condition | No | 3rd |
| ORDER BY | Sorts the result set | No | Last |
Question 1 of 5
Which SQL clause is used to filter rows based on a condition?
Question 2 of 5
What does the following query return?
SELECT * FROM Products ORDER BY price ASC;
Question 3 of 5
Given the Products table, which query returns only the names of items in the 'Furniture' category?
Question 4 of 5
What is the correct writing order of SQL clauses in a query?
Question 5 of 5
A query returns 100 rows. You add ORDER BY revenue DESC. How many rows does the result now contain?