The most common command for reading relational data is SELECT. A query tells the database which columns you want and, later, which rows should appear. Filtering, sorting, grouping, and joins all build on this foundation.
Continue with users. To see everything, write SELECT * FROM users;. SELECT chooses data, FROM users names the source, and the asterisk means every column. In plain language: “return all columns from users.”
| id | name | city | |
|---|---|---|---|
| 1 | Alice | alice@example.com | Tokyo |
| 2 | Bob | bob@example.com | Osaka |
| 3 | Chris | chris@example.com | Tokyo |
| 4 | Diana | diana@example.com | Kyoto |
SELECT * FROM users;Selecting specific columns
You do not always need every column. If a page only displays names and cities, requesting email and id adds no value. List the exact columns after SELECT:
SELECT name, city
FROM users;| name | city |
|---|---|
| Alice | Tokyo |
| Bob | Osaka |
| Chris | Tokyo |
| Diana | Kyoto |
This is clearer than SELECT with an asterisk because the SQL exposes exactly what the application needs. The asterisk is useful for exploring a new table; selecting only necessary fields is clearer in production, especially for wide tables. One column is fine too: SELECT name FROM users;.
Giving result columns aliases
Database column names may suit code but not presentation. An alias with AS temporarily changes a heading in the result:
SELECT name AS user_name, city AS user_city
FROM users;AS does not rename the stored columns. The table still has name and city; only this result displays user_name and user_city. Aliases also clarify calculations and joins where several tables may contain id or name.
SELECT produces a result set
SELECT does not modify the original table. It creates a temporary result set. Selecting name and city does not remove id or email from users. WHERE and ORDER BY will continue shaping the result set.
Before writing a query, ask two questions: which table contains the data, and which columns do I need? “Show every product name and price” naturally becomes products, name, and price.
Shape a result set yourself
Select any columns from users or products and try AS aliases. Run the statement and inspect the real result set.
| id | name | city | |
|---|---|---|---|
| 1 | Alice | [email protected] | Tokyo |
| 2 | Bob | [email protected] | Osaka |
| 3 | Chris | [email protected] | Tokyo |
| 4 | Diana | [email protected] | Kyoto |
Sandbox ready; no real database is connected.KNOWLEDGE QUIZ
Select and name fields for a product report
Write one SELECT against products that returns name, price, and category as product_name, unit_price, and product_category. Do not return id. Acceptance criteria: correct source table, exactly three fields, all three aliases, and a final semicolon.
Optional: portable practice setup
DROP TABLE IF EXISTS products;
CREATE TABLE products (
id INTEGER,
name VARCHAR(100),
price DECIMAL(10, 2),
category VARCHAR(50)
);
INSERT INTO products (id, name, price, category) VALUES
(1, 'Keyboard', 80.00, 'Electronics'),
(2, 'Mouse', 40.00, 'Electronics'),
(3, 'Coffee Mug', 15.00, 'Home'),
(4, 'Backpack', 60.00, 'Fashion');