w3resource

PostgreSQL Aggregate Functions and Group By: Get the total salaries payable to employees


2. Write a query to get the total salaries payable to employees.

Sample Solution:

Code:

-- Calculate the total sum of salaries for all employees in the database
SELECT SUM(salary) 
FROM employees;

Explanation:

  • This SQL query is designed to retrieve the sum of salaries for all employees stored in the database.
  • The SUM() function is an aggregate function in SQL that calculates the sum of values in a column.
  • salary is presumably a column in the employees table that holds the salary information for each employee.
  • The query calculates the sum of all values in the salary column for all rows in the employees table.
  • The result will be a single value representing the total sum of all salaries in the table.

Sample table: employees


Output:

pg_exercises=# SELECT SUM(salary)
pg_exercises-# FROM employees;
    sum
-----------
 691400.00
(1 row)

Relational Algebra Expression:

Relational Algebra Expression: Get the total salaries payable to employees.

Relational Algebra Tree:

Relational Algebra Tree: Get the total salaries payable to employees.

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query to find the number of jobs available in the employees table.
Next: Write a query to get the minimum salary from employees table.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/postgresql-exercises/aggregate-function-and-groupby/aggregate-function-and-groupby-exercise-2.php