Interrogare un database

SQL intermedio

Jasmin Ludolf

Data Science Content Developer, DataCamp

Roadmap del corso

 

  • Interrogare i database
  • Contare e visualizzare record specifici
  • Capire esecuzione e stile delle query
  • Filtrare
  • Funzioni di aggregazione
  • Ordinare e raggruppare

Logo PostgreSQL

SQL intermedio

Il nostro database films

Schema con le quattro tabelle del database films, inclusi nomi dei campi e tipi di dato

SQL intermedio

COUNT()

  • COUNT()
  • Conta i record con un valore in un campo
  • Usa un alias per chiarezza
SELECT COUNT(birthdate) AS count_birthdates
FROM people;
|count_birthdates|
|----------------|
|6152            |
SQL intermedio

COUNT() su più campi

SELECT COUNT(name) AS count_names, COUNT(birthdate) AS count_birthdates
FROM people;
|count_names|count_birthdates|
|-----------|----------------|
|6397       |6152            |
SQL intermedio

Usare * con COUNT()

  • COUNT(field_name) conta i valori in un campo
  • COUNT(*) conta i record in una tabella
  • * rappresenta tutti i campi
SELECT COUNT(*) AS total_records
FROM people;
|total_records|
|-------------|
|8397         |
SQL intermedio

DISTINCT

  • DISTINCT rimuove i duplicati e restituisce solo valori unici
SELECT language
FROM films;
|language |
|---------|
|Danish   |
|Danish   |
|Greek    |
|Greek    |
|Greek    |
  • Quali lingue ci sono nella tabella films?

 

SELECT DISTINCT language
FROM films;
|language |
|---------|
|Danish   |
|Greek    |
SQL intermedio

COUNT() con DISTINCT

  • Combina COUNT() con DISTINCT per contare i valori unici
SELECT COUNT(DISTINCT birthdate) AS count_distinct_birthdates
FROM people;
|count_distinct_birthdates|
|-------------------------|
|5398                     |
  • COUNT() include i duplicati
  • DISTINCT esclude i duplicati
SQL intermedio

Ayo berlatih!

SQL intermedio

Preparing Video For Download...