Querying Data with SELECT
How to read data with SELECT: choosing columns, filtering rows with WHERE, handling NULL, sorting, and the order a query actually runs in.
The shape of a query
SELECT reads data from tables. It's the SQL statement you'll write most, and it never changes the data, so it's safe to experiment with.
A query is built from up to four main clauses, always written in this order:
SELECT name, city -- which columns
FROM sales.customers -- which table
WHERE city = 'Accra' -- which rows
ORDER BY name; -- what order
The examples in this lesson use the sample tables from the first lesson.
Choosing columns
List the columns you want after SELECT, separated by commas:
SELECT name, city
FROM sales.customers;
NAME CITY
---------- ----------
Kofi Accra
Ama Kumasi
Yaw Accra
Esi
Kwame Tamale
5 rows selected.
SELECT * returns every column. It's handy for a quick look, but in scripts and applications, name the columns you need: it's clearer, and it won't break when someone adds a column to the table.
Calculations and column aliases
You can calculate new values from columns. An alias, given with AS, names the result:
SELECT order_id, amount, amount * 0.15 AS vat
FROM sales.orders
WHERE order_id <= 103;
ORDER_ID AMOUNT VAT
---------- ---------- ----------
101 250 37.5
102 75.5 11.325
103 1200 180
3 rows selected.
The calculation only changes what you see. The data in the table stays the same.
Removing duplicates with DISTINCT
DISTINCT shows each different value once. Here, Accra appears only once even though two customers live there:
SELECT DISTINCT city
FROM sales.customers
ORDER BY city;
CITY
----------
Accra
Kumasi
Tamale
4 rows selected.
The fourth row is Esi's empty city, shown as a blank line. Empty values have their own rules, covered below.
Filtering rows with WHERE
WHERE keeps only the rows where a condition is true. The database checks the condition against every row:
SELECT order_id, amount
FROM sales.orders
WHERE amount > 100;
ORDER_ID AMOUNT
---------- ----------
101 250
103 1200
105 310
3 rows selected.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | city = 'Accra' |
<> or != | Not equal to | status <> 'CANCELLED' |
>, < | Greater than, less than | amount > 100 |
>=, <= | Greater than or equal to, less than or equal to | order_id <= 103 |
IN (…) | Matches any value in a list | status IN ('PENDING', 'CANCELLED') |
BETWEEN … AND … | Within a range, including both ends | amount BETWEEN 50 AND 300 |
LIKE | Matches a text pattern | name LIKE 'K%' |
IS NULL | Has no value | city IS NULL |
SELECT order_id, amount, status
FROM sales.orders
WHERE amount BETWEEN 50 AND 300;
ORDER_ID AMOUNT STATUS
---------- ---------- ----------
101 250 SHIPPED
102 75.5 SHIPPED
106 95 PENDING
3 rows selected.
Combining conditions with AND, OR, and NOT
AND: both conditions must be true.OR: at least one condition must be true.NOT: reverses a condition, as inNOT INorNOT LIKE.
AND is applied before OR, which can surprise you. Use parentheses to make your meaning clear:
-- AND runs first: all SHIPPED orders, plus PENDING orders over 100
SELECT order_id FROM sales.orders
WHERE status = 'SHIPPED' OR status = 'PENDING' AND amount > 100;
101, 102, 103, 105 (4 rows)
-- Parentheses: SHIPPED or PENDING orders, as long as they're over 100
SELECT order_id FROM sales.orders
WHERE (status = 'SHIPPED' OR status = 'PENDING') AND amount > 100;
101, 103, 105 (3 rows)
Matching patterns with LIKE
LIKE uses two wildcards: % matches any number of characters, including none, and _ matches exactly one character.
SELECT name FROM sales.customers WHERE name LIKE 'K%'; -- starts with K: Kofi, Kwame
SELECT name FROM sales.customers WHERE name LIKE '_ma'; -- 3 letters ending in "ma": Ama
SELECT name FROM sales.customers WHERE name LIKE 'k%'; -- no rows: text comparisons are case-sensitive
SELECT name FROM sales.customers WHERE UPPER(name) LIKE 'K%'; -- ignores case: Kofi, Kwame
Filtering on dates
Write a date as DATE 'YYYY-MM-DD':
SELECT order_id, order_date
FROM sales.orders
WHERE order_date >= DATE '2026-03-01';
ORDER_ID ORDER_DAT
---------- ---------
105 01-MAR-26
106 05-MAR-26
2 rows selected.
SQL*Plus shows dates in a short default format such as 01-MAR-26. To choose the format, use TO_CHAR, covered below.
NULL: when there's no value
NULL means a value is missing or unknown, like Esi's city. It isn't zero, and it isn't a blank space. Because NULL is unknown, comparing anything with it, even = NULL, is never true:
SELECT name FROM sales.customers WHERE city = NULL;
no rows selected
SELECT name FROM sales.customers WHERE city IS NULL;
NAME
----------
Esi
1 row selected.
To show a replacement value instead of NULL, use NVL (or the standard COALESCE, which works on every platform):
SELECT name, NVL(city, 'Unknown') AS city
FROM sales.customers
WHERE customer_id IN (3, 4);
NAME CITY
---------- ----------
Yaw Accra
Esi Unknown
2 rows selected.
Sorting with ORDER BY
ORDER BY sorts the result. ASC (ascending, the default) sorts from smallest to largest; DESC sorts from largest to smallest:
SELECT order_id, amount
FROM sales.orders
ORDER BY amount DESC;
ORDER_ID AMOUNT
---------- ----------
103 1200
105 310
101 250
106 95
102 75.5
104 40
6 rows selected.
- Sort by several columns by listing them:
ORDER BY city, namesorts by city, then by name within each city. - Oracle puts NULLs last when sorting ascending, and first when sorting descending. Add
NULLS FIRSTorNULLS LASTto choose.
Limiting the number of rows
FETCH FIRST returns only the first few rows. Combined with ORDER BY, it answers "top N" questions, such as "What are the three biggest orders?":
SELECT order_id, amount
FROM sales.orders
ORDER BY amount DESC
FETCH FIRST 3 ROWS ONLY;
ORDER_ID AMOUNT
---------- ----------
103 1200
105 310
101 250
3 rows selected.
The order a query runs in
You write SELECT first, but the database runs it almost last. It starts with the table, filters the rows, and only then picks the columns:
This explains a common error. A column alias is created in step 3, so WHERE (step 2) can't use it yet, but ORDER BY (step 4) can:
SELECT order_id, amount * 0.15 AS vat FROM sales.orders WHERE vat > 30;
ORA-00904: "VAT": invalid identifier
SELECT order_id, amount * 0.15 AS vat FROM sales.orders WHERE amount * 0.15 > 30 ORDER BY vat;
works: orders 101, 105, and 103
Useful functions
Functions take a value and return a new one. To try a function without a table, select from DUAL, a one-row table that Oracle provides for exactly this:
SELECT UPPER('kofi') AS name FROM dual;
NAME
----
KOFI
| Function | What it does | Example | Result |
|---|---|---|---|
UPPER, LOWER | Changes text to upper or lower case | UPPER('kofi') | KOFI |
LENGTH | Counts the characters in text | LENGTH('Accra') | 5 |
|| | Joins two pieces of text | 'Kofi' || ' from ' || 'Accra' | Kofi from Accra |
ROUND | Rounds a number | ROUND(11.325, 2) | 11.33 |
NVL | Replaces NULL with a value | NVL(NULL, 'Unknown') | Unknown |
SYSDATE | The current date and time | SYSDATE | Today's date |
TO_CHAR | Formats a date or number as text | TO_CHAR(DATE '2026-01-10', 'YYYY-MM') | 2026-01 |
In practice: the same SELECT, DBA style
DBAs use exactly the same SELECT, WHERE, and ORDER BY every day, on the database's own views of itself. Two questions Kofi asks often:
-- Who is running something right now?
SELECT username, status, machine, program
FROM v$session
WHERE username IS NOT NULL
AND status = 'ACTIVE'
ORDER BY username;
-- Which tablespaces are more than 80% full?
SELECT tablespace_name, ROUND(used_percent, 1) AS pct_used
FROM dba_tablespace_usage_metrics
WHERE used_percent > 80
ORDER BY used_percent DESC;
V$ views show what the database is doing right now, and DBA_ views describe everything stored in it. Both are queried like any table, so every skill in this lesson applies directly.
Key takeaways
- A query reads data and never changes it:
SELECTpicks columns,FROMthe table,WHEREthe rows, andORDER BYthe order. WHEREsupports comparisons,IN,BETWEEN,LIKE, and conditions joined withANDandOR. Use parentheses when you mix them.- NULL means unknown. Test for it with
IS NULL, never= NULL. - Rows have no guaranteed order unless you use
ORDER BY. AddFETCH FIRSTfor top-N queries. - The database runs
FROMandWHEREbeforeSELECT, which is whyWHEREcan't use a column alias.