Sub topic 2
Choosing Columns Instead of Using `*`
Prerequisites and the basic choice
This chapter assumes you understand tables, rows, columns, and the basic shape of a SQL query. A table is a collection of related rows. Each row is one record, and each column stores one attribute of that record.
A SELECT statement chooses the columns that appear in the result. The FROM clause identifies the table to read:
SELECT first_name, last_name, department
FROM employees;This query returns three columns for every row in employees. The column order in the SELECT list becomes the column order in the result.
You can use * to request every column:
SELECT *
FROM employees;The asterisk, or star, is convenient when exploring an unfamiliar table. It is usually less suitable for application queries, reports, and saved views because it hides which data the query needs.
| Approach | Useful for | Main concern |
|---|---|---|
| `SELECT *` | Quick inspection and temporary exploration | Returns unwanted columns and can change when the table changes |
| Named columns | Reports, applications, and reusable queries | Requires you to name the needed columns |
Why explicit columns are safer
An explicit column list makes the query easier to read. Someone reviewing the query can see its intended output without inspecting the table definition.
It also reduces the amount of data transferred from the database. This matters when a table contains large text fields, binary data, or private information that the caller does not need.
A query using * can also change behavior after a schema change. If another column is added to employees, the query may suddenly return an extra column. Code that expects a fixed result shape can then fail or process the wrong value.
A query's result is a new table shape
A SELECT query does not have to return the table's original shape. It creates a result with only the requested columns, in the requested order.
SELECT salary, first_name, employee_id
FROM employees;Even if employee_id is the first column in the base table, it is the third column in this result. Selecting a column does not move or change the stored value. It only controls the result presented by the query.
You can qualify a column with its table name:
SELECT employees.first_name, employees.department
FROM employees;Qualification is especially useful when a query later reads from more than one table and both tables contain a column with the same name. It also documents where each selected value comes from.
Practice
Start with * while learning the table's structure, then replace it with the smallest useful list. For example, a directory might need names and departments but not salaries or internal identifiers:
SELECT first_name, last_name, department
FROM employees;This choice improves clarity and limits accidental exposure of data. It also gives the result a stable, documented shape.
Exercise
Why is SELECT first_name, last_name FROM employees usually preferable to SELECT * FROM employees in application code?
Show answer
It states the required result columns, avoids transferring unnecessary data, reduces accidental exposure of private fields, and remains stable when new columns are added to the table.