Module lessons (1/4)
Introduction to SQL
SQL (Structured Query Language) is the standard language with which you query, filter, and update relational databases such as PostgreSQL, MySQL, and SQLite. Its strength is the declarative approach: you describe the result you need and let the database engine choose the fastest reading plan.
Warm-Up: Your first department query
Read the complete list of company departments with identifier, name and city.
Select the id, name, and city columns from the departments table.
Show hint
Separate the names of the three columns with a comma: SELECT id, name, city.
Solution available after 3 attempts
1. Mental Model: Tables, Keys and Logical Order
Imagine each table as a strict spreadsheet where each column has a unique name and a data type declared (INTEGER, TEXT, NUMERIC, DATE, BOOLEAN):
| Element | Role in the database | Example in employees |
|---|---|---|
| Table | Container of entities of the same type | employees, departments, projects |
| Row (tuple) | A single concrete record | A hired person (Anna Bianchi) |
| Column (attribute) | A homogeneous property for each row | salary (NUMERIC(10, 2)), hired_on (DATE) |
Primary Key (id) | Unique identifier of the row in the table | employees.id = 1 |
Foreign Key (*_id) | A reference to the primary key of another table | employees.department_id → departments.id |
NULL | No known value (not 0, not "") | Andrea Costa has department_id = NULL |
2. Progressive & Annotated Examples
-- Esempio 1: leggere tutte le colonne e righe di una piccola tabella anagrafica
SELECT id, name, city
FROM departments;-- Esempio 2: proiettare solo le colonne utili di employees evitando SELECT *
SELECT first_name, last_name, salary
FROM employees;-- Esempio 3: esplorare i progetti aziendali con budget numerico esatto
SELECT name, budget
FROM projects;-- Esempio 4: combinare filtro e proiezione logica (il motore valuta FROM, poi WHERE, poi SELECT)
SELECT title, year, genre
FROM books
WHERE available = TRUE;3. Interactive Laboratory: How the SQL Engine Thinks
Even if you write SELECT at the beginning of the query, the PostgreSQL engine first reads the source (FROM), then filters (WHERE), then selects the columns (SELECT) and only at the end sorts or cuts (ORDER BY, LIMIT). Explore the sequence step by step:
4. Diagnostic Verification (Quiz)
What is the main difference between a relational table and an in-memory array?
In the query SELECT name, city FROM departments;, what does SELECT mean?
5. Progressive Practical Laboratory
Explore: Switch datasets to the Library
Query the authors table of the library catalog to extract name and country of origin.
Extract name and country from the authors table of the dataset library.
Show hint
Add country column next to name in the SELECT clause.
Solution available after 3 attempts
Practice: Corporate projects and budgets
Query the projects table to read the project name and allocated budget.
Show name and budget of all projects from the projects table.
Show hint
Replace? with name, budget.
Solution available after 3 attempts