SQL Count, Sum, Avg
Similar to the previous chapter, SUM, AVG, and COUNT functions are aggregate functions that return the sum of field, average value, and count. SUM and AVG can be applied only for numbers, and COUNT - for fields of any data type.
1SELECT AVG(price) FROM songs
If you want to find the number of records with some condition, you can apply COUNT to the *.
This query outputs the number of records with the release date in the 20th century:
12SELECT COUNT(*) FROM songs WHERE year <= 2000
Aggregate functions and Distinct
If you want to apply aggregate function to the unique values, write DISTINCT inside this function and before the column name:
1SELECT COUNT(DISTINCT song) FROM songs
Swipe to start coding
Now you have to write a query that helps you to calculate the total number of singers. Remember that some singers have two or more records in the table, but you have to pick these singers only once.
Soluzione
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Riassuma questo capitolo
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 4.17
SQL Count, Sum, Avg
Scorri per mostrare il menu
Similar to the previous chapter, SUM, AVG, and COUNT functions are aggregate functions that return the sum of field, average value, and count. SUM and AVG can be applied only for numbers, and COUNT - for fields of any data type.
1SELECT AVG(price) FROM songs
If you want to find the number of records with some condition, you can apply COUNT to the *.
This query outputs the number of records with the release date in the 20th century:
12SELECT COUNT(*) FROM songs WHERE year <= 2000
Aggregate functions and Distinct
If you want to apply aggregate function to the unique values, write DISTINCT inside this function and before the column name:
1SELECT COUNT(DISTINCT song) FROM songs
Swipe to start coding
Now you have to write a query that helps you to calculate the total number of singers. Remember that some singers have two or more records in the table, but you have to pick these singers only once.
Soluzione
Grazie per i tuoi commenti!
Awesome!
Completion rate improved to 4.17single