SELECT Specific Columns
Selecting only the columns you need is a core best practice. It reduces data transfer, improves performance, and makes your code robust against schema changes. Learn to select columns precisely, rename them, and compute new ones.
Column Selection Patterns
- List columns by name separated by commas
- Use aliases (AS) to rename output columns
- Compute new columns with expressions: price * 1.18 AS price_with_tax
- Concatenate in SELECT: CONCAT(first_name, ' ', last_name) AS full_name
- Avoid SELECT * — always name columns explicitly in applications
Column Selection Examples
-- Basic column selection from products table
SELECT name, price, stock FROM products;
-- Result:
-- name | price | stock
-- Laptop | 999.99 | 50
-- Phone | 599.99 | 120
-- Headphones | 89.99 | 200
-- Alias columns with AS
SELECT
name AS product_name,
price AS unit_price,
stock AS units_in_stock
FROM products;
-- Computed column
SELECT
name,
price,
stock,
price * stock AS total_inventory_value
FROM products;
-- String concatenation (different syntax per DB)
-- MySQL:
SELECT CONCAT(name, ' — $', price) AS label FROM products;
-- PostgreSQL:
SELECT name || ' — $' || price AS label FROM products;Quick Quiz
Tip
Tip
Practice SELECT Specific Columns in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
SQL doesn't run top-to-bottom. FROM executes first.
Common Mistake
Warning
A common mistake with SELECT Specific Columns is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready sql code.
Practice Task
Note
Practice Task — (1) Write a working example of SELECT Specific Columns from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Key Takeaways
- Selecting only the columns you need is a core best practice.
- List columns by name separated by commas
- Use aliases (AS) to rename output columns
- Compute new columns with expressions: price * 1.18 AS price_with_tax