SELECT Statement
Control exactly which columns appear in your output
SELECT: Choosing Your Columns
Every SQL query starts with SELECT — the verb that tells the database which columns to return. You can pick specific fields, create aliases, or compute entirely new columns on the fly. Here's what you can do:
SELECT name, price FROM products returns only those two fields. This keeps results focused, improves performance, and avoids accidentally exposing sensitive columns like passwords.* grabs every column. Convenient for quick exploration, but wasteful and risky in real applications where you want tight control over what data gets returned.SELECT price AS original_price changes how the column appears in the result without touching the actual table. Useful for creating clear, report-friendly labels in your output.SELECT price * 1.1 AS price_with_tax produces a column that doesn't exist in the table. You can use math, string functions, or any valid expression.How SELECT Works
The foundation of every SQL query
- SELECT picks which columns appear in your output
- Column order in SELECT = column order in result
- SELECT * returns all columns — exploration only
- AS creates aliases that rename output columns
Warnings & Gotchas
Mistakes that catch everyone
- SELECT * hides schema changes — broken code waits silently
- Aliases don't change the actual table — only the output label
- Forgetting commas between column names → syntax error
- Column order matters for UNION but not for readability
Power Patterns
Techniques that level up your queries
- SELECT DISTINCT removes duplicate rows
- Computed columns:
price * quantity AS total - String concatenation:
first || ' ' || last AS full_name - Conditional output:
CASE WHEN ... END AS label
Critical Rules
Memorize these
- Always name specific columns in production code
- AS aliases are for display — they don't modify data
- SELECT runs after FROM, WHERE, GROUP BY, HAVING
- Expressions in SELECT can reference any column in scope
Naming & Aliasing
Best practices for readable queries
- Use snake_case for aliases:
total_revenue, notTotalRevenue - Keep aliases short but descriptive
- Alias computed columns always — unnamed columns cause confusion
- Wrap aliases in double quotes if they contain spaces or keywords
Interview Angle
What interviewers look for
- Can you explain SELECT execution order?
- "When would you use SELECT * vs named columns?"
- Show awareness of computed columns and CASE
- Demonstrate alias discipline in complex queries
SELECT *Selects all columns — convenient for exploration, avoid in productionSELECT col1, col2Selects specific columns by nameAS aliasRenames a column in the output onlyExpressionsYou can use math, string functions, etc. in SELECT| id | name | category | price | stock |
|---|---|---|---|---|
| P101 | Wireless Mouse | Electronics | 24.99 | 150 |
| P102 | Office Desk | Furniture | 189 | 25 |
| P103 | LED Desk Lamp | Furniture | 45.5 | 80 |
| P104 | USB-C Cable | Electronics | 12.99 | 500 |
| P105 | Bluetooth Speaker | Electronics | 79 | 60 |
| P106 | Office Chair | Furniture | 95 | 15 |
| P107 | Coffee Maker | Appliances | 89.99 | 40 |
| P108 | Notebook Set | Stationery | 8.5 | 200 |
| P109 | Wireless Headphones | Electronics | 159 | 30 |
| P110 | Standing Desk | Furniture | 425 | 8 |
Worked Example
Show product names and their prices with a 10% discount applied.
| name | original_price | discounted_price |
|---|---|---|
| Wireless Mouse | 24.99 | 22.49 |
| Office Desk | 189 | 170.1 |
| LED Desk Lamp | 45.5 | 40.95 |
| USB-C Cable | 12.99 | 11.69 |
| Bluetooth Speaker | 79 | 71.1 |
Using SELECT * everywhere
Grabbing all columns is tempting but wasteful. It slows queries on large tables, makes results harder to read, and can accidentally expose sensitive data.
Name the columns you actually need. Only use * for quick exploration.
AS is optional, but clarity is not
SQL lets you write SELECT price discounted (without AS). While valid, it is confusing to read and easy to misparse.
Always use AS explicitly: SELECT price AS discounted.
Key Concepts
Pro Tip
SELECT is the verb of SQL. You cannot retrieve any data without it. Mastering column selection, aliases, and computed expressions gives you complete control over query output.
When to Use
API responses, CSV exports, dashboard feeds, report generation — they all start with choosing the right columns.
Write a query that shows the name, category, and stock for all products. Rename "stock" to "units_available".