Counting and averaging · Conteo y promedio
Summarising with aggregate functions
Sometimes you do not want the rows themselves, but a summary of them. Aggregate functions turn many rows into a single value:
COUNT(*)— how many rowsSUM(col)— the totalAVG(col)— the averageMIN(col)/MAX(col)— the smallest / largest
Resumen con funciones agregadas
A veces no se desean las filas en sí, sino un resumen de ellas. Las funciones agregadas convierten muchas filas en un único valor:
COUNT(*)— cuántas filas haySUM(col)— el totalAVG(col)— el promedioMIN(col)/MAX(col)— el más pequeño / el más grande
SELECT COUNT(*) FROM student;
Naming and rounding the result
Give the result a clear heading with AS. Averages often have long decimals, so wrap them in ROUND(value, 2) to keep 2 decimal places:
You can combine several aggregates in one query.
Asignación de nombres y redondeo del resultado
Asigne un título claro al resultado usando AS. Los promedios suelen tener decimales largos, por lo que envuélvalos en ROUND(value, 2) para mantener 2 decimales:
SELECT COUNT(*) AS n, ROUND(AVG(score), 2) AS avg_score
FROM student;
Puede combinar varias funciones agregadas en una sola consulta.
Common mistakes
COUNT(*)counts rows;AVG(col)averages a column.- Round an average with
ROUND(x, 2).
Errores comunes
COUNT(*)cuenta filas;AVG(col)calcula el promedio de una columna.- Redondee un promedio con
ROUND(x, 2).
In one query, show how many students there are as n, and their average score (to 2 decimal places) as avg_score. · En una sola consulta, muestra cuántos estudiantes hay como n, y su puntuación promedio (con 2 decimales) como avg_score.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Show the lowest score as lowest, the highest as highest, and the total of all scores as total. · Muestra la puntuación más baja como lowest, la más alta como highest, y la suma total de todas las puntuaciones como total.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.