Riassumere i dati

Query SQL intermedie con l'AI

Jasmin Ludolf

Senior Data Science Content Developer

Funzioni di aggregazione

$$

  • SUM()
  • AVG()
  • MIN()
  • MAX()

$$

  • Qual è il numero medio di voti per film?

Report con statistiche riassuntive

Query SQL intermedie con l'AI

Riassumere i campi

$$

  • Le funzioni di aggregazione riassumono più righe in un unico valore

$$

  • Comprendere i dati nel loro insieme o per gruppo

Prompt: Mostrami il numero totale di voti nei film

SELECT SUM(num_votes) AS total_votes
FROM reviews;
|total_votes|
|-----------|
|419507814  |
Query SQL intermedie con l'AI

Calcolare le medie dei campi

Prompt: Qual è il numero medio di voti per film?

SELECT AVG(num_votes) AS average_votes_per_film
FROM reviews;
|average_votes_per_film|
|----------------------|
|84441.991545893720    |
Query SQL intermedie con l'AI

Arrotondare i risultati

Prompt: Qual è il numero medio arrotondato di voti per film?

SELECT ROUND(AVG(num_votes)) AS avg_votes
FROM reviews;
|avg_votes|
|---------|
|84442    |
ROUND(AVG(num_votes), 1)
84442.0
ROUND(AVG(num_votes), 2)
84441.99
ROUND(AVG(num_votes), -2)
84400
Query SQL intermedie con l'AI

Arrotondare i risultati

Prompt: ...arrotondato a una cifra decimale

SELECT ROUND(AVG(num_votes), 1) AS avg_votes
FROM reviews;
|avg_votes|
|---------|
|84442.0  |

Prompt: ...arrotondato al centinaio più vicino

SELECT ROUND(AVG(num_votes), -2) AS avg_votes
FROM reviews;
|avg_votes|
|---------|
|84400    |
Query SQL intermedie con l'AI

Medie raggruppate

Prompt: Mostrami il budget medio arrotondato per lingua, ordinato dal più alto

SELECT language, ROUND(AVG(budget)) AS average_budget
FROM films
GROUP BY language
ORDER BY average_budget DESC;
|language |average_budget|
|---------|--------------|
|Greek    |              |
|Kannada  |              |    
|Polish   |              |
|Korean   |2741550000    |
...
Query SQL intermedie con l'AI

Minimo e massimo

Prompt: Mostra i budget minimi e massimi per paese, in ordine alfabetico

SELECT country, 
       MIN(budget) AS lowest_budget, 
       MAX(budget) AS highest_budget
FROM films
GROUP BY country
ORDER BY country;
|country    |lowest_budget|highest_budget|
|-----------|-------------|--------------|
|Afghanistan|46000        |46000         |
|Argentina  |800000       |2000000       |
|Aruba      |35000000     |35000000      |
...

$$

$$

  • Identificare intervalli
  • Capire l'ambito dei dati
  • Capire il valore tipico
Query SQL intermedie con l'AI

Funzioni di aggregazione e tipi di dati

Solo campi numerici

  • SUM()
  • AVG()

Vari tipi di dati

  • MIN()
  • MAX()
  • COUNT()

$$

Minimo <-> Massimo

  • Più basso <-> Più alto
  • A <-> Z
  • Più antico <-> Più recente
Query SQL intermedie con l'AI

Minimo e massimo

SELECT MIN(language) AS min_language
FROM films;
|min_language|
|------------|
|Aboriginal  |
SELECT MAX(language) AS max_language
FROM films;
|max_language|
|------------|
|Zulu        |
Query SQL intermedie con l'AI

Best practice SQL: alias

SELECT SUM(budget) AS total_budget
...
SELECT AVG(budget) AS average_budget
...
SELECT country, 
       MIN(budget) AS lowest_budget, 
       MAX(budget) AS highest_budget
...
SELECT AVG(num_user), AVG(num_critic)
FROM reviews;
|avg     |avg     |
|275.6...|141.9...|
Query SQL intermedie con l'AI

Best practice SQL: formattazione

 

  • Scrivi query su più righe
  • Usa l'indentazione
SELECT country, 
       MIN(budget) AS lowest_budget, 
       MAX(budget) AS highest_budget
FROM films
GROUP BY country
ORDER BY country;

Occhiali su carta

Icona SQL

Query SQL intermedie con l'AI

Pronto per analizzare?

Query SQL intermedie con l'AI

Preparing Video For Download...