Open company.db in DB Browser for SQLite and go to the Execute SQL tab. Run each query below, one at a time, and compare with the results shown.
Choosing columns
SELECT * returns every column. Name the columns you want instead, separated by commas:
SELECT username, full_name, department FROM users;
Fewer columns make results easier to read, and on big tables, faster.
Filtering rows with WHERE
WHERE keeps only the rows that match a condition:
SELECT username, full_name, department
FROM users
WHERE department = 'Finance';
username full_name department
budi Budi Santoso Finance
gita Gita Permata Finance
rina Rina Maharani Finance
Text goes in single quotes. Numbers do not. Conditions you will use:
| Condition | Meaning |
|---|---|
=, != |
equal, not equal |
>, <, >=, <= |
greater, smaller |
AND, OR, NOT |
combine conditions |
LIKE '203.0.113.%' |
text pattern; % means “anything” |
IS NULL, IS NOT NULL |
the value is empty, or not |
Sorting and limiting
ORDER BY sorts; DESC means newest or biggest first. LIMIT keeps only the first few rows:
SELECT ts, user_id, source_ip
FROM logins
WHERE success = 0
ORDER BY ts DESC
LIMIT 5;
ts user_id source_ip
2026-11-22 02:07:14 11 203.0.113.50
2026-11-22 01:59:09 6 203.0.113.50
2026-11-22 01:58:08 1 203.0.113.50
2026-11-22 01:57:07 11 203.0.113.50
2026-11-22 01:56:06 9 203.0.113.50
“The five most recent failed logins” is one of the first questions in almost any investigation. All five come from the same outside address, against different users.
Patterns and time ranges
Did anything from that address succeed? LIKE matches the whole 203.0.113. range:
SELECT *
FROM logins
WHERE source_ip LIKE '203.0.113.%' AND success = 1;
One row: login 28, user 11, at 2026-11-22 02:07:39, with no device (device_id is empty, meaning a remote VPN login). Lesson 3 shows how to turn user 11 into a name.
Times in this database are written as text in the form YYYY-MM-DD HH:MM:SS. Because the biggest unit comes first, sorting and comparing them as text gives the right order. So a time range is just two comparisons:
SELECT ts, user_id, source_ip, success
FROM logins
WHERE ts >= '2026-11-22 00:00:00' AND ts < '2026-11-22 06:00:00'
ORDER BY ts;
That is everything that happened between midnight and 6 a.m. on the night of the 22nd.
Empty values: NULL
Some columns can be empty. In SQL, empty is called NULL, and it needs its own test: = NULL never matches. Find the remote logins (no device):
SELECT ts, user_id, source_ip
FROM logins
WHERE device_id IS NULL AND success = 1;
What to notice
SELECT columns FROM table WHERE condition ORDER BY column LIMIT nis the shape of most questions.- Text needs single quotes;
%inLIKEmatches anything. - Times written biggest-unit-first sort and compare correctly as text.
- Empty values are
NULLand needIS NULL, not= NULL.