முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
eLearner.app
தொகுதி 2 · பாடம் 4 இன் 4பாடத்திட்டத்தில் 8/57~8 min
தொகுதி பாடங்கள் (4/4)

எண்ணுதல் மற்றும் சுருக்கம்: COUNT, AVG, MAX

So far every SELECT returned one row for each row of the table. Aggregate functions do the opposite: they read many rows and return a single value that summarises them.

The most common ones are:

  • COUNT(*) — how many rows there are.
  • AVG(column) — the average of numeric values (ignores NULL).
  • MAX(column) / MIN(column) — the maximum / minimum value.
  • SUM(column) — the sum of numeric values.
SQL
SELECT COUNT(*),
       AVG(salary),
       MAX(salary)
FROM employees;

When the aggregate is applied to the whole table (without GROUP BY, which you will see in the next module) the result is a single row, regardless of how many rows the source table has.

Combining with WHERE

WHERE filters before aggregation: the aggregate only works on the rows that passed the filter.

SQL
-- Stipendio medio dei soli dipendenti del Marketing (department_id = 3):
SELECT AVG(salary)
FROM employees
WHERE department_id = 3;

Try it

உடற்பயிற்சி#sql.m2.l4.e1
முயற்சிகள்: 0ஏற்றுகிறது…

How many employees have been hired since January 1, 2020? Return a single number (one row, one column).

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

The function that counts rows is COUNT(*).

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Review exercise

உடற்பயிற்சி#sql.m2.l4.e2
முயற்சிகள்: 0ஏற்றுகிறது…

In a single query, compute the average (AVG) and maximum (MAX) salary of employees in Marketing (department_id = 3). One row, two columns.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

You can put several aggregate functions in the same SELECT, separated by commas.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்