The SQL SUM function is used to calculate the total sum of a numeric column. It is particularly useful in scenarios where you need to determine the total amount of sales, revenue, or any other numeric data stored in a database.
SELECT SUM(salary) AS TotalSalary
FROM employees;
This query calculates the total salary paid to all employees in the company. The result is stored in a column named 'TotalSalary'.
Console Output:
TotalSalary: 150000
The GROUP BY clause is used in collaboration with the SUM function to aggregate data across multiple records and group the results by one or more columns.
SELECT department, SUM(salary) AS TotalSalary
FROM employees
GROUP BY department;
This query calculates the total salary for each department in the company. The results are grouped by the department name.
Console Output:
Sales: 50000
Marketing: 40000
HR: 30000
The WHERE clause is used to filter records before applying the SUM function, allowing you to calculate totals based on specific conditions.
SELECT SUM(salary) AS TotalSalary
FROM employees
WHERE department = 'Sales';
This query calculates the total salary for employees in the Sales department only.
Console Output:
TotalSalary: 50000
The DISTINCT keyword can be used with the SUM function to calculate the total of unique values, avoiding duplicates in the calculation.
SELECT SUM(DISTINCT salary) AS TotalUniqueSalary
FROM employees;
This query calculates the total of unique salary values across all employees, ensuring no duplicate salaries are summed.
Console Output:
TotalUniqueSalary: 130000
The HAVING clause is used to filter records that are aggregated by the GROUP BY clause, allowing you to specify conditions on the aggregated data.
SELECT department, SUM(salary) AS TotalSalary
FROM employees
GROUP BY department
HAVING SUM(salary) > 30000;
This query calculates the total salary for each department but only returns departments where the total salary exceeds 30,000.
Console Output:
Sales: 50000
Marketing: 40000
Using SUM with JOINs allows you to calculate totals across related tables, providing more comprehensive insights by combining data from multiple sources.
SELECT e.department, SUM(o.amount) AS TotalSales
FROM employees e
JOIN orders o ON e.employee_id = o.employee_id
GROUP BY e.department;
This query calculates the total sales amount for each department by joining the employees and orders tables on employee_id.
Console Output:
Sales: 70000
Marketing: 50000
Subqueries allow you to use the result of one query as a condition in another query, providing a powerful way to calculate totals based on complex criteria.
SELECT department, SUM(salary) AS TotalSalary
FROM employees
WHERE department IN (SELECT department FROM departments WHERE location = 'New York')
GROUP BY department;
This query calculates the total salary for departments located in New York, using a subquery to filter departments by location.
Console Output:
Sales: 50000
Calculating the sum of multiple columns can provide insights into complex datasets, such as total expenses, revenue, and profit margins.
SELECT SUM(salary + bonus) AS TotalCompensation
FROM employees;
This query calculates the total compensation for employees by summing up their salary and bonus amounts.
Console Output:
TotalCompensation: 200000
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies