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.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Résumer ce chapitre
Expliquer le code dans file
Expliquer pourquoi file ne résout pas la tâche
Awesome!
Completion rate improved to 4.17
SQL Count, Sum, Avg
Glissez pour afficher le 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.
Solution
Merci pour vos commentaires !
Awesome!
Completion rate improved to 4.17single