SQL Fundamentals
The SELECT Statement
The foundation of every SQL query β retrieve exactly the data you need from your database.
- Targeted Retrieval β select specific columns instead of entire tables
- Computed Columns β calculate values on the fly with expressions
- Clean Output β alias columns for readable, report-ready results
SELECT is how you ask your database questions.
What is SELECT?
DfSELECT Statement
A SQL command that retrieves data from one or more tables. It specifies which columns to return and optionally filters, sorts, and limits the results.
SELECT column1, column2
FROM table_name;
| Component | Description | Required |
|---|---|---|
SELECT | Columns to retrieve | Yes |
column1, column2 | Specific columns or * for all | Yes |
FROM | Table to query | Yes |
table_name | Source table | Yes |
Select All Columns
SELECT * FROM customers;
| id | first_name | last_name | city | |
|---|---|---|---|---|
| 1 | Alice | Johnson | alice@email.com | New York |
| 2 | Bob | Smith | bob@email.com | Los Angeles |
| 3 | Carol | Williams | carol@email.com | Chicago |
Avoid SELECT * in production queries. It returns all columns, increases network traffic, consumes more memory, and breaks if the schema changes.
Select Specific Columns
SELECT first_name, last_name, email
FROM customers;
| first_name | last_name | |
|---|---|---|
| Alice | Johnson | alice@email.com |
| Bob | Smith | bob@email.com |
-- SLOW: Returns all columns
SELECT * FROM customers WHERE city = 'New York';
-- FAST: Returns only needed columns
SELECT first_name, last_name, email
FROM customers
WHERE city = 'New York';
Expressions in SELECT
DfComputed Columns
You can create new columns by applying expressions to existing data. These computed columns appear in the result set with the alias you provide.
SELECT
name,
price,
price * 1.08 AS price_with_tax,
price * stock AS inventory_value,
ROUND(price * 0.9, 2) AS discounted
FROM products;
| name | price | price_with_tax | inventory_value | discounted |
|---|---|---|---|---|
| Mouse | 29.99 | 32.39 | 4498.50 | 26.99 |
| Keyboard | 89.99 | 97.19 | 6749.25 | 80.99 |
Common Functions
-- String functions
SELECT
UPPER(first_name) AS upper_name,
LOWER(last_name) AS lower_name,
LENGTH(email) AS email_length,
CONCAT(first_name, ' ', last_name) AS full_name
FROM customers;
-- Math functions
SELECT
price,
ROUND(price, 0) AS rounded,
CEIL(price) AS ceiling,
FLOOR(price) AS floor_price,
ABS(-price) AS absolute
FROM products;
-- Date functions
SELECT
hire_date,
YEAR(hire_date) AS hire_year,
MONTH(hire_date) AS hire_month,
DATEDIFF(CURRENT_DATE, hire_date) AS days_employed
FROM employees;
Column Aliases
SELECT
first_name AS "First Name",
last_name AS "Last Name",
salary AS annual_salary,
salary * 12 AS monthly_salary
FROM employees;
| First Name | Last Name | annual_salary | monthly_salary |
|---|---|---|---|
| Alice | Johnson | 75000 | 6250 |
| Bob | Smith | 62000 | 5166.67 |
Use double quotes for aliases with spaces. Without quotes, aliases cannot contain spaces or special characters.
SELECT Without a Table
-- Perform calculations without any table
SELECT 1 + 1 AS result; -- 2
SELECT CURRENT_DATE AS today; -- 2024-01-15
SELECT 'Hello, World!' AS greeting; -- Hello, World!
SELECT RANDOM(); -- Random number
SQL Execution Order
DfExecution Order
SQL queries execute in a specific order: FROM β WHERE β GROUP BY β HAVING β SELECT β ORDER BY β LIMIT. Understanding this helps you write correct queries.
| Step | Clause | What It Does |
|---|---|---|
| 1 | FROM | Identify source tables |
| 2 | WHERE | Filter rows |
| 3 | GROUP BY | Group rows |
| 4 | HAVING | Filter groups |
| 5 | SELECT | Choose columns |
| 6 | ORDER BY | Sort results |
| 7 | LIMIT | Restrict row count |
-- What you write:
SELECT first_name, salary
FROM employees
WHERE department = 'Engineering'
ORDER BY salary DESC
LIMIT 5;
-- How it executes:
-- 1. FROM employees β All employee rows
-- 2. WHERE department='Engineering' β Only engineers
-- 3. SELECT first_name, salary β Just those two columns
-- 4. ORDER BY salary DESC β Highest salary first
-- 5. LIMIT 5 β Top 5 only
Performance Tips
| Tip | Bad Example | Good Example | Impact |
|---|---|---|---|
| Use specific columns | SELECT * | SELECT name, price | Faster |
| Add WHERE clause | No filter | WHERE id > 1000 | Fewer rows |
| Use LIMIT | Unlimited results | LIMIT 100 | Smaller result |
Always select only the columns you need. This reduces memory usage, network transfer, and allows the database to use covering indexes.
Common Mistakes
-- BAD: SELECT * in production
SELECT * FROM customers;
-- GOOD: Specific columns
SELECT id, first_name, last_name, email FROM customers;
-- BAD: Missing WHERE clause
SELECT * FROM customers;
-- GOOD: Filter with WHERE
SELECT * FROM customers WHERE is_active = TRUE;
-- BAD: No LIMIT for large tables
SELECT * FROM logs;
-- GOOD: Limit results
SELECT * FROM logs ORDER BY created_at DESC LIMIT 100;
Practice Exercises
Exercise 1: Write a query to select name and price from products, and calculate a 10% discount as discounted_price.
SELECT
name,
price,
price * 0.9 AS discounted_price
FROM products;
Exercise 2: Select all employees with their annual salary (salary * 12).
SELECT
first_name,
last_name,
salary,
salary * 12 AS annual_salary
FROM employees;
Exercise 3: Select customers with formatted names and email length.
SELECT
UPPER(first_name) AS first_name,
UPPER(last_name) AS last_name,
CONCAT(first_name, ' ', last_name) AS full_name,
LENGTH(email) AS email_length
FROM customers;
Key Takeaways
- SELECT retrieves data from a database β it's the most common SQL command
- Use specific column names instead of * in production for better performance
- Expressions compute new values β calculations, formatting, and transformations
- Aliases (AS) make output cleaner β rename columns for readability
- SQL executes in a specific order β FROM β WHERE β SELECT β ORDER BY