SELECT Statement in SQL: Selecting All Columns vs Specific Columns
If INSERT INTO puts data in, SELECT is how you get it back out — and it's by far the most frequently used statement in all of SQL. Nearly every application screen, report, and dashboard is ultimately powered by a SELECT statement somewhere underneath. This lesson covers SELECT's basic syntax and the important practical difference between SELECT * and explicitly naming the columns you need.
What Is the SELECT Statement?
SELECT is the DQL (Data Query Language) statement used to retrieve data from one or more tables. In its simplest form, SELECT column1, column2 FROM table_name; returns the specified columns for every row in the table, and SELECT * FROM table_name; returns every column for every row.
What You'll Learn
- Write basic SELECT statements retrieving specific columns.
- Understand the practical difference between SELECT * and named columns.
- Use column aliases with AS for cleaner output.
- Recognize why explicit column selection is the production-grade habit.
Key Terms to Know
- DQL: Data Query Language, the SQL category covering SELECT and its supporting clauses.
- SELECT *: A wildcard that retrieves every column in a table.
- Column alias: A temporary renamed label for a column or expression in query output, set using AS.
- Result set: The table-shaped output returned by a SELECT query.
Basic SELECT Syntax
SELECT column1, column2 FROM table_name; retrieves only the listed columns, in the order specified, for every row currently in the table. The simplest possible query, SELECT * FROM table_name;, retrieves all columns, in the table's defined order, which is convenient for quick, ad-hoc exploration of unfamiliar data.
Why SELECT * Is Risky in Production Code
SELECT * is fine for casual exploration in a SQL client, but it's considered poor practice inside application code and views for several reasons: it retrieves more data than necessary over the network, it silently changes behavior if columns are added or removed from the table later, and it makes queries harder to read since it's unclear exactly which columns the calling code actually depends on.
Explicitly listing needed columns, like SELECT customer_id, full_name, email FROM customers;, keeps queries self-documenting, reduces unnecessary data transfer, and prevents an unrelated schema change from silently breaking application code that expected a specific, fixed set of columns.
Column Aliases with AS
SELECT full_name AS name, email AS contact_email FROM customers; renames columns in the output without changing anything in the underlying table. Aliases are especially useful when a column's actual name is awkward, or when displaying calculated/derived columns, such as SELECT price * quantity AS line_total FROM order_items;, where the alias names a value that doesn't correspond to any single stored column at all.
Visual Summary
Picture a table as a full filing cabinet drawer with many labeled tabs (columns). SELECT * pulls out a complete photocopy of every page from every folder. SELECT column1, column2 is more like asking the clerk for just two specific tabs from every folder, handed to you neatly, without the extra paperwork you didn't ask for.
SELECT * vs Named Columns
| Aspect | SELECT * | Named Columns |
|---|---|---|
| Data transferred | All columns, every time | Only what's actually needed |
| Resilience to schema changes | Output silently changes if columns are added/removed | Stable and predictable |
| Readability | Unclear what the query actually needs | Self-documenting |
| Best for | Quick ad-hoc exploration | Application code, views, production queries |
SQL Example
-- Quick exploration: see everything in the table
SELECT * FROM products;
-- Production-style query: only the columns actually needed
SELECT product_id, product_name, price
FROM products;
-- Using aliases for clarity and derived values
SELECT
product_name AS name,
price,
stock_qty,
(price * stock_qty) AS inventory_value
FROM products;
The first query is a quick, exploratory look at the entire table. The second is the kind of focused query a real application would use, transferring only the three relevant columns. The third introduces an alias for readability and computes inventory_value on the fly, a column that doesn't actually exist in the table at all — it's calculated purely within the SELECT statement.
Real-World Examples
- API endpoints typically use named-column SELECT statements to return exactly the fields the frontend expects, nothing more.
- Admin dashboards often use SELECT * during prototyping, then switch to named columns once the feature is finalized for production.
- Reporting tools use computed aliased columns, like profit_margin or inventory_value, calculated directly inside the SELECT statement.
- Database views are almost always built with explicit named columns rather than SELECT *, to keep the view's output contract stable over time.
Best Practices and Pro Tips
- Treat SELECT * as a tool for quick, temporary exploration only — convert it to explicit columns before the query goes anywhere near production code.
- Use meaningful aliases for any computed or derived column so the output is immediately understandable without needing to re-read the underlying expression.
- When working with very wide tables (many columns), explicit column selection also meaningfully reduces memory and network usage at scale.
Common Mistakes to Avoid
- Leaving SELECT * in production application code, then being surprised when a later schema change (like a new column) breaks downstream code that wasn't expecting it.
- Forgetting that column order in SELECT * follows the table's internal definition order, which isn't guaranteed to match what the application expects positionally.
- Aliasing a column to a name that's also a SQL reserved word without quoting it, causing a syntax error.
- Selecting far more columns than a query actually needs, especially in joins, leading to unnecessary memory and bandwidth usage.
Interview Questions
Q1. What is the main risk of using SELECT * in production code?
SELECT * retrieves every column, including ones the application might not need, and its output silently changes if columns are added, removed, or reordered later, which can break code that relied on a specific, fixed column set.
Q2. What does a column alias do, and how is it written?
A column alias temporarily renames a column or expression in the query's result set, written using AS, such as SELECT full_name AS name FROM customers;. It does not change anything in the underlying table.
Q3. Can a SELECT statement return a value that isn't stored in any column?
Yes, SELECT can return computed expressions, such as price * quantity AS line_total, which calculates a value on the fly from existing columns without that value ever being stored anywhere in the table.
Q4. What category of SQL statement does SELECT belong to?
SELECT belongs to DQL (Data Query Language), the subset of SQL focused on retrieving data, distinct from DML (INSERT/UPDATE/DELETE) and DDL (CREATE/ALTER/DROP).
Practice MCQs
1. Which query retrieves every column from the products table?
- SELECT product_id FROM products;
- SELECT * FROM products;
- SELECT ALL FROM products;
- SELECT COLUMNS FROM products;
Answer: B. SELECT * FROM products;
Explanation: The asterisk wildcard (*) in a SELECT statement retrieves all columns for every row in the specified table.
2. Which is considered the better practice for production application code?
- SELECT * FROM table;
- Selecting only the specific columns needed
- Always selecting more columns than needed just in case
- Using SELECT ALL instead of SELECT *
Answer: B. Selecting only the specific columns needed
Explanation: Explicit column selection is more efficient, stable against schema changes, and easier to read than SELECT *.
3. What keyword is used to rename a column in the query output?
- RENAME
- ALIAS
- AS
- LABEL
Answer: C. AS
Explanation: AS is used to assign a temporary alias to a column or expression in a SELECT statement's result set.
Quick Revision Points
- SELECT is a DQL statement used purely to retrieve data, never to modify it.
- SELECT * returns all columns; named columns return only what's specified, in the order given.
- Column aliases (AS) rename output columns without affecting the underlying table.
- SELECT can return computed expressions that don't correspond to any stored column.
Conclusion
- SELECT is the single most frequently used SQL statement in real-world applications.
- Explicit column selection is a small habit with a meaningfully large payoff in stability and performance.
- Aliases make both renamed and computed columns far easier to read and use downstream.
The SELECT statement is how data is retrieved from a table, and choosing between SELECT * and explicit named columns is a small decision with outsized consequences for performance, readability, and resilience to future schema changes. Column aliases with AS further improve readability, especially for computed values. With basic retrieval covered, the next lesson — SELECT DISTINCT — tackles a common follow-up problem: removing duplicate rows from query results.