SQL Basics — SELECT, WHERE & Filtering
Master the fundamental SQL statements every data analyst uses every day. These are the building blocks of every SQL query you will ever write.
The SELECT Statement
SELECT is the first word in almost every SQL query. It tells the database which columns you want to retrieve. Understanding it deeply is the foundation of all SQL.
- SELECT specifies which columns to return — columns are comma-separated
- SELECT * returns ALL columns — avoid in production but fine for exploration
- AS creates an alias — a readable name for any column or expression
- Calculated columns (e.g. price * quantity) can be created directly in SELECT
WHERE — Filtering Rows
WHERE filters which rows are included in results. Only rows where the condition evaluates to TRUE are returned. This is the most important SQL concept for analysts — most questions require filtering data.
| Operator | Meaning | Example |
|---|---|---|
= | Equals exactly | city = 'Mumbai' |
!= or <> | Not equal | status != 'cancelled' |
>, <, >=, <= | Comparison | amount > 5000 |
IN | Match any in list | city IN ('Mumbai','Delhi') |
NOT IN | Not in list | status NOT IN ('returned') |
BETWEEN | Range (inclusive) | date BETWEEN '2024-01-01' AND '2024-03-31' |
LIKE | Pattern match | name LIKE 'Raj%' |
IS NULL | Check for missing | email IS NULL |
IS NOT NULL | Check not missing | phone IS NOT NULL |
ORDER BY and LIMIT
ORDER BY sorts results. LIMIT restricts how many rows you get back. Together they power “find the top N” — one of the most common interview patterns.
DISTINCT — Remove Duplicates
Handling NULL Values
NULL means a value is missing or unknown. It is not zero, not empty string — it’s the absence of data. Understanding NULL is critical for real-world data analyst work.
Column & Table Aliases
Practice Questions — SQL Basics
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE c.city = ‘Mumbai’ AND o.order_amount > 5000;
WHERE product_name LIKE ‘%phone%’;
— PostgreSQL (case insensitive):
WHERE product_name ILIKE ‘%phone%’
SELECT * FROM products ORDER BY price DESC LIMIT 5;
— Per category (window function):
WITH ranked AS (SELECT *, RANK() OVER(PARTITION BY category ORDER BY price DESC) rn FROM products)
SELECT * FROM ranked WHERE rn <= 5;
Ready to practice SQL with a mentor?
Book a free 1-on-1 SQL mock interview. Real questions, live feedback, written report.
Book Free Session