SQL ยท Chapter 3 of 42

SQL SELECT

SELECT reads rows from one or more tables. Choose specific columns to fetch, or use `*` for all columns.

Structure: `SELECT columns FROM table;`

Syntax
SELECT column1, column2 FROM table_name;

Selecting columns

List column names separated by commas. `SELECT *` grabs all โ€” great for exploring, but avoid in production.

Computed columns

You can compute values on the fly: `SELECT price * 1.18 AS price_with_tax FROM products;`

Example 1 (sql)
SELECT name, email FROM users;
Output
name | email
Ana  | ana@x.com
Ben  | ben@x.com

Fetch only the columns you need.

Example 2 (sql)
SELECT name, price * 1.18 AS gross FROM products;
Output
name    | gross
Book    | 118.00
Pen     | 23.60

Alias a computed column with AS.

Key points

  • SELECT reads data.
  • List columns explicitly (avoid `*` in production).
  • AS renames a column in the output.
  • Computed expressions are allowed.
๐Ÿ’ก Note: `SELECT *` returns whatever columns exist today โ€” if the schema changes, your queries silently return different results.

๐Ÿ“ Quick Quiz

1. SELECT is used to:

2. Which keyword renames a column in output?

3. `SELECT *` returns: