01 / 21

SELECT, WHERE
& ORDER BY

Master the foundational SQL clauses to select columns, filter rows, and sort results from any database table.

Business Analytics

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

SELECT
Choose which columns to retrieve from a table.
WHERE
Filter rows based on a condition so only matching records appear.
ORDER BY
Sort the results in ascending or descending order.

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
1Laptop999Electronics
2Desk Chair275Furniture
3Monitor450Electronics
4Notebook12Supplies
5Keyboard85Electronics

The SELECT Statement

SELECT column1, column2
FROM table_name;
1
SELECT tells the database which columns you want returned.
2
FROM specifies which table to pull the data from.
3
Use * (asterisk) to select all columns at once.

Filtering with WHERE

SELECT name, price
FROM Products
WHERE price > 100;

WHERE Comparison Operators

Operator Meaning Example
=Equal toprice = 450
<> or !=Not equal tocategory <> 'Supplies'
> , >=Greater than (or equal)price >= 100
< , <=Less than (or equal)price < 500
BETWEENInclusive rangeprice BETWEEN 50 AND 300
LIKEPattern matchname LIKE 'Key%'
INMatches a listcategory IN ('Electronics','Supplies')

Sorting with ORDER BY

SELECT name, price
FROM Products
ORDER BY price DESC;
ASC
Ascending order: smallest to largest, A to Z. This is the default.
DESC
Descending order: largest to smallest, Z to A. Must be written explicitly.

The SQL Execution Pipeline

FROM Pick the table WHERE Filter rows SELECT Pick columns ORDER BY Sort results Logical order of execution (not the order you write them!)

SELECT vs WHERE: Key Difference

Aspect SELECT WHERE
Operates onColumnsRows
PurposeChoose which fields appearChoose which records appear
AnalogyHighlighting spreadsheet columnsHiding spreadsheet rows
Optional?Required in every queryOptional: omit to return all rows

How to Write a Query

1
Identify the table: Which table holds the data you need? Write the FROM clause.
2
Filter rows: Do you need every row, or only some? Add a WHERE condition if needed.
3
Choose columns: List the columns you want in the SELECT clause (or use * for all).
4
Sort if needed: Add ORDER BY with ASC or DESC at the end.
5
End with a semicolon (;) to mark the end of your statement.

Example 1: Basic SELECT

Goal: Retrieve the name and price of every product.

SELECT name, price
FROM Products;
name price
Laptop999
Desk Chair275
Monitor450
Notebook12
Keyboard85

Example 2: WHERE Filter

Goal: Find all Electronics that cost more than 100.

SELECT name, price
FROM Products
WHERE category = 'Electronics'
AND price > 100;
name price
Laptop999
Monitor450

Example 3: Full Query

Goal: List all products under 500, sorted by price from highest to lowest.

SELECT name, price, category
FROM Products
WHERE price < 500
ORDER BY price DESC;
name price category
Monitor450Electronics
Desk Chair275Furniture
Keyboard85Electronics
Notebook12Supplies

Common Mistakes

✕ Wrong Quote Marks
Text values use single quotes: 'Electronics'. Double quotes are for column/table names in some systems.
✕ Missing Comma
Forgetting the comma between columns: SELECT name price throws an error. It should be name, price.
✕ WHERE After ORDER BY
Clause order matters in syntax: SELECT ... FROM ... WHERE ... ORDER BY. Swapping WHERE and ORDER BY causes errors.

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?