Skip to main content
Module 1 · Lesson 2 of 42/57 in the course~12 min
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

SyntaxWhat the engine doesWhen to use it
SELECT col1, col2 FROM t;Reads only the listed columnsAlways in apps, APIs, and report queries
SELECT * FROM t;Expands all columns of the current schemaQuick exploration in the Playground
SELECT col * 12 FROM t;Evaluate the expression line by lineCalculations of fees, margins, VAT
SELECT 'Hello' || ' ' || colConcatenate strings on the database sideQuick formatting in ad-hoc queries

2. Progressive & Annotated Examples

SQL
-- Esempio 1: tutte le colonne dei reparti (solo 4 righe, schema piccolo)
SELECT *
FROM departments;
SQL
-- Esempio 2: proiezione esplicita su employees (esclude email e date superflue)
SELECT first_name, last_name, salary
FROM employees;
SQL
-- Esempio 3: espressione aritmetica derivata riga per riga
SELECT name, budget, budget * 0.22 AS estimated_tax
FROM projects;
SQL
-- 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)

SQL Quiz#sql.m1.l2.e5
Ready

What does the query above return, missing the comma between first_name and last_name?

SQL
SELECT first_name last_name FROM employees;
Answer options
SQL Quiz#sql.m1.l2.e6
Ready

If you write SELECT title, pages, pages / 2 FROM books;, does the data saved in the books table change?

Answer options

5. Progressive Practical Laboratory

Warm-Up Stage

Warm-Up: Project the right columns

Extract the name, surname and salary of the employees excluding the other columns.

Exercise#sql.m1.l2.e1
Attempts: 0Loading…

Select only the first_name, last_name, and salary columns from the employees table.

Loading editor…
Show hint

List the three columns separated by commas instead of asterisks: first_name, last_name, salary.

Solution available after 3 attempts

Practice Stage

Practice: Explore the departments

Read only the name of the department and its location from the departments table.

Exercise#sql.m1.l2.e2
Attempts: 0Loading…

Show name and city of all departments from the departments table.

Loading editor…
Show hint

Instead of *, just list name and city after SELECT.

Solution available after 3 attempts

Practice Stage

Practice: Library catalogue

Apply selective projection to the books table of the library dataset.

Exercise#sql.m1.l2.e3
Attempts: 0Loading…

From the books table extract only the title, year of publication and genre.

Loading editor…
Show hint

Add year and genre separated by commas in the SELECT list.

Solution available after 3 attempts

Build Stage

Build: Monthly calculated column

Use an arithmetic operator in the SELECT clause to calculate a derived value row by row.

Exercise#sql.m1.l2.e4
Attempts: 0Loading…

Extract first_name, salary, and the 12-month monthly salary calculated as salary / 12 with alias monthly_salary from the employees table.

Loading editor…
Show hint

Add the third expression: salary / 12 AS monthly_salary.

Solution available after 3 attempts