> For the complete documentation index, see [llms.txt](https://learn.ziomark.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.ziomark.xyz/sql/parte-2-query-di-selezione.md).

# Parte 2: Query di Selezione

Benvenuti alla seconda lezione del nostro corso di SQL su ZioMark's HUB! Dopo aver introdotto i fondamenti di SQL nella prima lezione, ora approfondiremo le query di selezione, esplorando come recuperare dati specifici da un database in modo più efficiente e potente.

### Filtrare i Risultati con `WHERE`

La clausola `WHERE` è fondamentale in SQL per filtrare i record in base a una condizione specifica. Questo permette di visualizzare solo i dati che rispondono a determinati criteri.

#### Sintassi

```sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
```

#### Esempio

Supponiamo di avere una tabella `Employees` con i seguenti dati:

| ID | Name    | Age | Department |
| -- | ------- | --- | ---------- |
| 1  | Alice   | 23  | HR         |
| 2  | Bob     | 30  | IT         |
| 3  | Charlie | 34  | IT         |
| 4  | David   | 29  | HR         |

Per selezionare solo gli impiegati del dipartimento IT, useremmo:

```sql
SELECT * FROM Employees
WHERE Department = 'IT';
```

### Ordinare i Risultati con `ORDER BY`

La clausola `ORDER BY` permette di ordinare i risultati di una query SQL. Puoi ordinare i dati in ordine ascendente (ASC) o discendente (DESC).

#### Sintassi

```sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
```

#### Esempio

Per ordinare gli impiegati per età in ordine crescente:

```sql
SELECT Name, Age FROM Employees
ORDER BY Age ASC;
```

### Utilizzo di Funzioni Aggregato

Le funzioni aggregato come `COUNT`, `MAX`, `MIN`, `SUM`, e `AVG` sono usate per eseguire calcoli sui valori di una colonna e restituire un singolo valore.

#### Esempio

Calcolare l'età media degli impiegati:

```sql
SELECT AVG(Age) AS AverageAge FROM Employees;
```

### Esercizio Pratico

Ora che hai appreso come filtrare e ordinare i dati, nonché utilizzare funzioni aggregato, applichiamo queste conoscenze in un esercizio pratico.

#### Scenario

Hai una tabella `Products` con le seguenti colonne: `ProductID`, `ProductName`, `Price`, e `Category`.

| ProductID | ProductName | Price | Category  |
| --------- | ----------- | ----- | --------- |
| 1         | Table       | 85    | Furniture |
| 2         | Chair       | 45    | Furniture |
| 3         | Lamp        | 22    | Lighting  |
| 4         | Desk        | 120   | Furniture |

#### Compito

1. Scrivi una query per trovare tutti i prodotti della categoria "Furniture".
2. Ordina questi prodotti per prezzo in ordine decrescente.
3. Calcola il prezzo medio dei prodotti nella categoria "Furniture".

#### Soluzioni

1. Selezionare prodotti della categoria Furniture:

   ```sql
   SELECT * FROM Products
   WHERE Category = 'Furniture';
   ```
2. Ordinarli per prezzo decrescente:

   ```sql
   SELECT * FROM Products
   WHERE Category = 'Furniture'
   ORDER BY Price DESC;
   ```
3. Calcolare il prezzo medio:

   ```sql
   SELECT AVG(Price) AS AveragePrice FROM Products
   WHERE Category = 'Furniture';
   ```

### Conclusione

Con queste competenze, sei ora in grado di eseguire query complesse e ottenere esattamente i dati di cui hai bisogno dal tuo database.

Nella prossima lezione, esploreremo come unire le tabelle per ottenere informazioni ancora più dettagliate. Continua a praticare con questi esempi per consolidare le tue abilità in SQL!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://learn.ziomark.xyz/sql/parte-2-query-di-selezione.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
