Sign in to save your place.

MODULE 6 OF 12

SELECT, WHERE & ORDER BY

Query data with SELECT, filter with WHERE, and sort with ORDER BY.

SELECT

SELECT reads data from tables. Use SELECT * to get all columns, or list specific columns: SELECT Id, Name, Email.

WHERE

Filter rows with WHERE: = (equals), <> (not equal), > < >= <=. Use LIKE for pattern matching (e.g. Name LIKE 'A%' for names starting with A). Combine conditions with AND, OR, or IN (value1, value2, ...).

ORDER BY & TOP N

ORDER BY column ASC (ascending, default) or DESC (descending). Use TOP N to return only the first N rows (e.g. SELECT TOP 10 * FROM Orders ORDER BY OrderDate DESC).

Key Takeaway

SELECT specifies columns, WHERE filters rows, ORDER BY sorts the result. TOP N limits how many rows you get.

Query flow

SELECT (what columns) → FROM (which table) → WHERE (which rows) → ORDER BY (sort order) → TOP (limit rows).

SELECT examples

-- All columns
SELECT * FROM Customers;

-- Specific columns, filtered and sorted
SELECT Id, Name, Email FROM Customers
WHERE Country = 'UK' AND Name LIKE 'A%'
ORDER BY Name ASC;

-- Top 5 most recent orders
SELECT TOP 5 * FROM Orders ORDER BY OrderDate DESC;

Practice

Write queries: (1) SELECT only Name and Email from Customers. (2) WHERE Status = 'Active' and Country IN ('US','UK'). (3) ORDER BY CreatedAt DESC with TOP 10.

Quiz

1. What does ORDER BY ... DESC do?

Sorts ascending
Sorts descending (high to low, Z to A)
Deletes the column
Filters rows

Correct! DESC sorts in descending order.

ORDER BY ... DESC sorts the result in descending order (largest first, Z to A).

2. What is WHERE used for?

Filtering which rows are returned
Choosing which columns to return
Sorting the result
Creating a new table

Correct! WHERE filters rows.

WHERE filters which rows are included in the result; SELECT chooses columns.

October 2026: TOP, ORDER BY, and NULL

TOP without ORDER BY returns an arbitrary set. Add ORDER BY when you mean "the first five." WHERE city = NULL matches nothing. Use IS NULL.

PostgreSQL spells the limiter as LIMIT. SQL Server uses TOP or OFFSET / FETCH. Do not mix the two spellings in one script.

SELECT TOP (5) name, amount FROM sales ORDER BY amount DESC;

Try this. Return the five largest amounts with TOP (5) and an ORDER BY. Then find rows whose city is missing.

Knowledge check

Which predicate finds a missing city?