0% found this document useful (0 votes)
11 views1 page

Select Aggregate Functions

The document provides SQL query examples for using LIMIT, GROUP BY, and aggregate functions such as COUNT, SUM, AVG, MAX, and MIN. It includes specific queries to calculate total employees, total salary, average salary, highest and lowest salaries, and aggregate salaries by department with conditions using HAVING. Additionally, it demonstrates how to count distinct values in a column.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Select Aggregate Functions

The document provides SQL query examples for using LIMIT, GROUP BY, and aggregate functions such as COUNT, SUM, AVG, MAX, and MIN. It includes specific queries to calculate total employees, total salary, average salary, highest and lowest salaries, and aggregate salaries by department with conditions using HAVING. Additionally, it demonstrates how to count distinct values in a column.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

LIMIT

SELECT column1 FROM table_name LIMIT number_of_rows;


GROUP BY
SELECT column1, COUNT(column2) FROM table_name GROUP BY column1;

AGGREGATE FUNCTIONS
ALIAS
SELECT column_name AS alias_name FROM table_name;

1. COUNT: Count the total number of employees.


sql

SELECT COUNT(*) AS total_employees FROM employees;

2. SUM: Calculate the total salary of all employees.

sql
SELECT SUM(salary) AS total_salary FROM employees;

3. AVG: Find the average salary of employees.

SELECT AVG(salary) AS average_salary FROM employees;

4. MAX: Find the highest salary among employees.

sql
SELECT MAX(salary) AS highest_salary FROM employees;

5. MIN: Find the lowest salary among employees.

SELECT MIN(salary) AS lowest_salary FROM employees;

6. Aggregate with GROUP BY: Calculate the total salary for each department.

SELECT department, SUM(salary) AS total_salary


FROM employees
GROUP BY department;

7. HAVING Clause with Aggregates: Show departments with total salaries above
100,000.
sql

SELECT department, SUM(salary) AS total_salary


FROM employees
GROUP BY department
HAVING total_salary > 100000;

8. COUNT Distinct: Count the number of unique departments.


sql

SELECT COUNT(DISTINCT department) AS unique_departments FROM employees;

You might also like