Module lessons (2/4)
SELECT … FROM
Every read query in SQL arises from the fundamental pair SELECT … FROM …: FROM chooses where to draw the rows from, while SELECT decides which columns (or calculated expressions) to extract in the result.
1. Mental Model: Projection vs Copy to Disk
| Syntax | What the engine does | When to use it |
|---|---|---|
SELECT col1, col2 FROM t; | Reads only the listed columns | Always in apps, APIs, and report queries |
SELECT * FROM t; | Expands all columns of the current schema | Quick exploration in the Playground |
SELECT col * 12 FROM t; | Evaluate the expression line by line | Calculations of fees, margins, VAT |
SELECT 'Hello' || ' ' || col | Concatenate strings on the database side | Quick formatting in ad-hoc queries |
2. Progressive & Annotated Examples
-- Esempio 1: tutte le colonne dei reparti (solo 4 righe, schema piccolo)
SELECT *
FROM departments;-- Esempio 2: proiezione esplicita su employees (esclude email e date superflue)
SELECT first_name, last_name, salary
FROM employees;-- Esempio 3: espressione aritmetica derivata riga per riga
SELECT name, budget, budget * 0.22 AS estimated_tax
FROM projects;-- Esempio 4: combinazione di colonne testuali su un altro dataset (library)
SELECT title, genre, pages
FROM books;3. Interactive Laboratory: Projection Pipeline
Notice how FROM loads all columns of the table and SELECT projects only the subset required in step 5 of the SQL pipeline:
4. Diagnostic Verification (Quiz)
What does the query above return, missing the comma between first_name and last_name?
SELECT first_name last_name FROM employees;If you write SELECT title, pages, pages / 2 FROM books;, does the data saved in the books table change?
5. Progressive Practical Laboratory
Warm-Up: Project the right columns
Extract the name, surname and salary of the employees excluding the other columns.
Select only the first_name, last_name, and salary columns from the employees table.
Show hint
List the three columns separated by commas instead of asterisks: first_name, last_name, salary.
Solution available after 3 attempts
Practice: Explore the departments
Read only the name of the department and its location from the departments table.
Show name and city of all departments from the departments table.
Show hint
Instead of *, just list name and city after SELECT.
Solution available after 3 attempts
Practice: Library catalogue
Apply selective projection to the books table of the library dataset.
From the books table extract only the title, year of publication and genre.
Show hint
Add year and genre separated by commas in the SELECT list.
Solution available after 3 attempts
Build: Monthly calculated column
Use an arithmetic operator in the SELECT clause to calculate a derived value row by row.
Extract first_name, salary, and the 12-month monthly salary calculated as salary / 12 with alias monthly_salary from the employees table.
Show hint
Add the third expression: salary / 12 AS monthly_salary.
Solution available after 3 attempts