Why this matters
In SQL interview tests, the first questions are almost always about selecting, filtering and sorting rows. These few keywords do a large share of everyday analyst work, so practise them until they feel natural.
The example table
Imagine an invented table called sales for a chain of provision shops in the north:
| sale_id | shop | town | item | qty | amount | |---|---|---|---|---|---| | 1 | Shop A | Tamale | Rice | 2 | 180 | | 2 | Shop B | Wa | Oil | 1 | 95 |
SELECT and FROM
SELECT names the columns you want; FROM names the table.
SELECT town, item, amount
FROM sales;SELECT * returns every column. It is handy for a quick look, but naming columns keeps results small and clear.
WHERE: filtering rows
WHERE keeps only rows that meet a condition.
SELECT item, amount
FROM sales
WHERE town = 'Tamale' AND amount >= 100;Text values go inside single quotes. You can combine conditions with AND and OR, and use comparisons such as =, <>, <, >, <= and >=. Other useful filters:
IN ('Tamale', 'Wa')matches any value in a list.BETWEEN 50 AND 200matches a range, including both ends.