Querying Data: SELECT and WHERE · Lesson 4 of 30
SQL SELECT Statement
SELECT is the statement you'll write more than any other: it reads data from one or more tables and returns a result set – a temporary table of rows and columns. In this lesson you'll choose columns, calculate new ones, give them readable names and remove duplicates.
In this lesson
- Selecting specific columns, and why to avoid
SELECT * - Calculated columns and expressions
- Column aliases with
AS - Removing duplicates with
DISTINCT
Choosing columns
SELECT name, price
FROM products;
+---------------------+---------+ | name | price | +---------------------+---------+ | Wireless Mouse | 599.00 | | Mechanical Keyboard | 2499.00 | | USB-C Hub | 1299.00 | | Clean Code | 450.00 | | SQL in 10 Minutes | 350.00 | | Gel Pen Pack | 120.00 | | A5 Notebook | 90.00 | | Laptop Stand | 1499.00 | | Desk Lamp | 899.00 | | JavaScript Guide | 550.00 | +---------------------+---------+
List the columns you want, separated by commas, in the order you want them shown. SELECT * returns every column – useful for exploring, but avoid it in application code:
- It sends data you don't need over the network.
- Adding a column to the table silently changes your results.
- It hides which columns the code actually depends on, and prevents some index optimisations.
Calculated columns
A SELECT list can contain expressions, not just column names:
SELECT name,
price,
stock,
price * stock AS stock_value,
ROUND(price * 1.18, 2) AS price_with_gst
FROM products
WHERE category_id = 1;
+---------------------+---------+-------+-------------+----------------+ | name | price | stock | stock_value | price_with_gst | +---------------------+---------+-------+-------------+----------------+ | Wireless Mouse | 599.00 | 120 | 71880.00 | 706.82 | | Mechanical Keyboard | 2499.00 | 35 | 87465.00 | 2948.82 | | USB-C Hub | 1299.00 | 0 | 0.00 | 1532.82 | | Laptop Stand | 1499.00 | 25 | 37475.00 | 1768.82 | +---------------------+---------+-------+-------------+----------------+
The calculation runs for each row. The original table is unchanged – SELECT only reads.
Aliases
AS renames a column in the result. If the alias contains spaces, quote it with backticks (MySQL) or double quotes (standard SQL). Tables can have aliases too – essential once you join tables:
SELECT c.name AS customer,
c.city AS `home city`,
c.signup_date AS joined
FROM customers AS c
WHERE c.customer_id <= 3;
+------------+-----------+------------+ | customer | home city | joined | +------------+-----------+------------+ | Asha Verma | Pune | 2025-01-15 | | Ravi Kumar | Delhi | 2025-02-03 | | Meera Nair | Kochi | 2025-02-20 | +------------+-----------+------------+
Common mistake: using an alias in WHERE
SELECT price * stock AS stock_value FROM products WHERE stock_value > 10000; fails with "Unknown column 'stock_value'". WHERE runs before SELECT, so the alias doesn't exist yet. Repeat the expression in WHERE, or use the alias in ORDER BY (which runs after SELECT).
DISTINCT: remove duplicate rows
SELECT city FROM customers;
+-----------+ | city | +-----------+ | Pune | | Delhi | | Kochi | | Bengaluru | | Delhi | | Mumbai | | NULL | | Pune | +-----------+
SELECT DISTINCT city FROM customers;
+-----------+ | city | +-----------+ | Pune | | Delhi | | Kochi | | Bengaluru | | Mumbai | | NULL | +-----------+
DISTINCT applies to the whole row of selected columns, not just the first column. NULL counts as one distinct value.
SELECT DISTINCT department, salary >= 100000 AS earns_six_figures
FROM employees
ORDER BY department;
+-------------+-------------------+ | department | earns_six_figures | +-------------+-------------------+ | Engineering | 0 | | Engineering | 1 | | HR | 0 | | Management | 1 | | Sales | 0 | | Sales | 1 | +-------------+-------------------+
How SQL reads a SELECT
You write a query in one order, but the database processes it in another. Knowing the real order explains most "why doesn't this work?" errors:
Logical order of a SELECT query
- FROM / JOINpick the tables and combine them
- WHEREkeep only matching rows
- GROUP BY → HAVINGgroup rows, filter groups (chapter 4)
- SELECTcompute the columns and aliases
- DISTINCTremove duplicate rows
- ORDER BY → LIMITsort, then take the first rows
Practice
Try it yourself
Show each employee's name, monthly salary (salary ÷ 12, rounded) and department, calling the columns
employee,monthlyanddept, for the Sales department.Show solution
SELECT name AS employee, ROUND(salary / 12) AS monthly, department AS dept FROM employees WHERE department = 'Sales';+------------+---------+-------+ | employee | monthly | dept | +------------+---------+-------+ | Farhan Ali | 11667 | Sales | | Lata Joshi | 7083 | Sales | | Tom Mathew | 6500 | Sales | +------------+---------+-------+
List the different order statuses that exist.
Show solution
SELECT DISTINCT status FROM orders ORDER BY status;+-----------+ | status | +-----------+ | cancelled | | delivered | | pending | | shipped | +-----------+
Quick check
Why is SELECT * discouraged in application code?
It fetches unneeded data, breaks silently when columns change and hides the code's real dependencies.
Can you use a SELECT alias in WHERE?
No – WHERE is evaluated before SELECT. You can use it in ORDER BY.
Does DISTINCT apply to one column or the whole row?
The whole row of selected columns.
Summary
- SELECT lists the columns or expressions to return; FROM names the table.
- AS gives readable aliases to columns and tables.
- DISTINCT removes duplicate rows; queries run FROM → WHERE → SELECT → ORDER BY.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.