SELECT
SELECT
Definition
SELECT retrieves data from one or more tables — it's the single most-used SQL statement, and the entry point for reading anything back out of a database.
How It Works
Running example — a products(id, name, category, price, stock) table, where stock can be NULL (not yet counted):
| id | name | category | price | stock |
|---|---|---|---|---|
| 1 | ProLaptop | Electronics | 85000 | 12 |
| 2 | Notebook | Stationery | 40 | NULL |
| 3 | Python Basics | Books | 350 | 5 |
| 4 | ProPhone | Electronics | 45000 | 0 |
sqlSELECT name, price FROM products; SELECT * FROM products; -- every column SELECT name, price * 1.18 AS price_with_tax FROM products; -- computed expression
SELECT can return literal columns, computed expressions, or * (every column). The column list can also include arithmetic, string concatenation, or function calls — anything that produces one value per row.
Edge Cases and Pitfalls
SELECT *is convenient for exploring data interactively but risky in application code: it silently changes shape if the table's columns change later, and returns more data over the network than might be needed.- The order of columns in the output exactly matches the order written in the
SELECTlist — it has nothing to do with the table's physical column order. SELECTalone with noFROMis valid in some dialects for evaluating a plain expression (SELECT 2 + 2;) — useful for testing an expression without touching any table.
Key Takeaways
SELECT column_list FROM tableis the basic shape of every read query.- The column list can include real columns, computed expressions, and aliases.
- Prefer explicit column lists over
SELECT *in real application code.