SELECT: reading data from a table, choosing columns, aliases, calculations and DISTINCT
SELECT is the SQL command you'll use more than any other. It asks the database a question and gets back a result set: a table of rows and columns. Every report, dashboard, app screen and data analysis starts with a SELECT. When you open your M-Pesa statement, an online shop's product list or your school portal results, a SELECT query ran behind the scenes.
This unit teaches SELECT thoroughly using the sample Kenyan shop database built into this hub: Customers, Products and Orders. Every example can be run with the Run button.
The sample database
| Table | Columns |
|---|---|
| Customers | CustomerID, Name, City, Phone |
| Products | ProductID, Name, Category, Price |
| Orders | OrderID, CustomerID, ProductID, Quantity, OrderDate |
The basic shape
SELECT column1, column2
FROM table_name;SELECTlists what you want (columns or calculations).FROMsays where the data comes from (a table).- The semicolon
;ends the statement. Many tools accept a single query without it, but it's a good habit.
SQL keywords are not case-sensitive (select works like SELECT), but writing keywords in capitals makes queries easier to read. Table and column names in this database start with capitals.
Select every column
SELECT * FROM Products;* means "all columns". It's great for exploring a table, but in real applications list the columns you need: it's faster, clearer, and won't break if someone adds a column later.
Select specific columns
SELECT Name, Price FROM Products;The result shows columns in the order you list them, not the order in the table:
SELECT Price, Name, Category FROM Products;Aliases: renaming columns in the result
AS gives a column a friendlier name in the output. It doesn't change the table.
SELECT Name AS Product, Price AS "Price (KSh)" FROM Products;Use double quotes for aliases with spaces or special characters. Aliases are very useful for calculated columns, which otherwise get ugly names.
Calculated columns
You can do arithmetic in SELECT: +, -, *, /.
SELECT Name,
Price,
Price * 1.16 AS PriceWithVAT,
Price * 0.9 AS SalePrice
FROM Products;Rounding makes money look right:
SELECT Name, ROUND(Price * 1.16, 2) AS PriceWithVAT FROM Products;SELECT 7 / 2 AS WholeDivision, 7 / 2.0 AS DecimalDivision;Joining text together
The || operator joins (concatenates) text in SQLite, PostgreSQL and Oracle. MySQL uses CONCAT().
SELECT Name || ' (' || City || ')' AS CustomerLabel FROM Customers;DISTINCT: unique values only
Which cities do our customers live in?
SELECT City FROM Customers;Some cities repeat. DISTINCT removes duplicates:
SELECT DISTINCT City FROM Customers;With several columns, DISTINCT removes rows where all listed columns are the same:
SELECT DISTINCT Category FROM Products;LIMIT: just a few rows
Large tables can have millions of rows. LIMIT returns only the first few:
SELECT Name, Price FROM Products LIMIT 3;Different databases spell this differently: SQL Server uses SELECT TOP 3 ..., Oracle uses FETCH FIRST 3 ROWS ONLY. Without ORDER BY (next lesson) "the first 3" can be any 3 rows.
Selecting without a table
You can use SELECT as a calculator or to test functions:
SELECT 2500 * 3 AS Total, UPPER('nairobi') AS Town, DATE('now') AS Today;Comments and formatting
-- This is a single-line comment
/* This is a
multi-line comment */
SELECT Name, -- product name
Price -- in KSh
FROM Products;Put each column on its own line in longer queries. Readable SQL is easier to debug and review.
How the database processes a SELECT
You write SELECT ... FROM ..., but the database logically works in this order:
- FROM: find the table(s)
- WHERE: filter rows
- GROUP BY: make groups
- HAVING: filter groups
- SELECT: compute the columns
- ORDER BY: sort
- LIMIT: cut the rows
This explains things later, such as why you can't use a SELECT alias in WHERE in most databases.
Common errors
| Error | Cause | Fix |
|---|---|---|
no such table: product | Wrong table name | Check spelling: Products |
no such column: Prise | Typo in column name | Check spelling |
near "FROM": syntax error | Missing comma or extra comma before FROM | SELECT Name, Price FROM, no comma before FROM |
| Text without quotes | 'Nairobi' must be in single quotes | Use single quotes for text values |
Think about it: You write SELECT Name Price FROM Products; (forgetting the comma). It runs without an error, but the result has one column called "Price" containing product names. Why?Show answer
Without the comma, SQL reads Price as an alias for Name (the AS keyword is optional). So it shows the Name column renamed to Price. Always check commas between columns.
Summary
SELECT columns FROM tablereads data;*selects all columns.AScreates aliases; calculated columns use arithmetic and functions likeROUND.||joins text in SQLite; watch out for integer division.DISTINCTremoves duplicate rows;LIMITreturns a few rows.- The database processes FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
Check yourself
Which symbol selects all columns?
Show answer
*
Which keyword gives a column a new name in the result?
Show answer
AS
Which keyword removes duplicate rows from the result?
Show answer
DISTINCT
In SQLite, what is 7 / 2?
Show answer
3
Which keyword returns only the first few rows in SQLite and MySQL?
Show answer
LIMIT
Which clause does the database process first: SELECT or FROM?
Show answer
FROM
Exercise
Select only the Name and Price columns from the Products table.