-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrouping Data and Computing Aggregates.sql
More file actions
61 lines (45 loc) · 1.38 KB
/
Grouping Data and Computing Aggregates.sql
File metadata and controls
61 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""GROUP BY Function"""
SELECT SUM(salary), department
FROM employees
WHERE 1=1
GROUP BY department;
/*Assignment
Total count of employees working for each department*/
SELECT COUNT(employee_id) total_number_employees, department,
ROUND(AVG(salary)) Average_Salary, MIN(salary), MAX(salary)
FROM employees
WHERE salary > 70000
GROUP BY department
ORDER BY total_number_employees DESC;
/* HAVING Command
is used to filter aggregated data*/
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 35
ORDER BY department;
/* Assignment
How many employees have the same first name */
SELECT COUNT(first_name), first_name
FROM employees
GROUP BY first_name
HAVING COUNT(first_name) >1
/* Assignment
Find the unique departments in the employees table without using the DISTINCT command */
SELECT department
FROM employees
GROUP BY department
/* Assignment
Find all the domain names and the total number of employees that have that
given domain name*/
SELECT SUBSTRING(email, POSITION('@' IN email) +1) domain_name, COUNT(*)
FROM employees
WHERE email IS NOT NULL
GROUP BY domain_name
ORDER BY COUNT(*) DESC
/* Assignment
Display MIN, MAX and AVG salaries broken down by region and gender*/
SELECT gender, region_id, MIN(salary) min_salary, MAX(salary) max_salary, ROUND(AVG(salary)) avg_salary
FROM employees
GROUP BY gender, region_id
ORDER BY gender ASC, region_id ASC