DEV Community

Cover image for SQL AGGREGATE FUNCTIONS
Markme Dev
Markme Dev

Posted on

SQL AGGREGATE FUNCTIONS

DAY 2
Hello Everyone 👋,

It's me again markme today our topic is aggregate functions in SQL and we're going to walkthrough to some of it such as

  • AVG()
  • SUM()
  • COUNT()
  • MIN()
  • MAX()

and then we're going to use that into query after we discuss it one by one, so let's get start.

## AVG() FUNCTION

  • AVG() or average function is getting the average of all records in a selected column.take note that avg fuction is only accepting numbers not string and varchar.

Then this is how you use AVG() in a query

SELECT AVG(salary) FROM users

Explanation: it will get the average salary of all users in the table that's the output.


## SUM() FUNCTION

  • SUM() sum function is used to calculate the sum of values in a column.

Then this is how you use SUM() in a query

SELECT SUM(salary) FROM users;

Explanation: In essence, the query fetches the total sum of all salaries stored in the "salary" column of the "users".


## COUNT() FUNCTION

  • COUNT() count function is used to count the number of rows that meet a specified condition.

Then this is how you use COUNT() in a query

SELECT COUNT(salary) FROM users;

Explanation: In this sample query we're getting the total rows where the salary value is not null or absence. like if there is no value it will not count as 1 it will go to the next row then check the value.


## MIN() FUNCTION

  • MIN() function is used to retrieve the minimum value from a column.

Then this is how you use MIN() in a query

SELECT MIN(salary) FROM users;

Explanation: In this query it will get the minimum value in the salary column it will check all the row who's the lowest value then show it in the output.


## MAX() FUNCTION

  • MAX() function is used to retrieve the maximum value from a column.

Then this is how you use MAX() in a query

SELECT MAX(salary) FROM users;

Explanation: In this query it will get the maximum value in the salary column it will check all the row who's the highest value then show it in the output.

That's the some example of aggregate function when we are going to apply it in sql. its good to use this aggregate and combine it with group clause.

Now that you got a glimpse of the aggregate function try to apply that also in your end and explore much more deeper.

"Consistency is the key to mastery."

If you guys have comments or concern about this topic please comment down below. Thanks for reading.

  • MARKME

Top comments (0)