-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_by_notes.sql
202 lines (154 loc) · 4.9 KB
/
group_by_notes.sql
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
USE employees;
select first_name
FROM employees
GROUP BY first_name;
# SELECT first_name, last_name FROM employees GROUP BY first_name; #[n]
#grouping by first names...doesnt make sense to add last_name
select first_name, AVG(LENGTH(last_name))
FROM employees
GROUP BY first_name;
#[y]
#find common name
# select concat(first_name, ' ', last_name) from employees;[x];
SELECT CONCAT(first_name, ' ', last_name)
FROM employees
GROUP BY first_name, last_name;
SELECT CONCAT(first_name, ' ', last_name), COUNT(*)
FROM employees
GROUP BY first_name, last_name;
SELECT CONCAT(first_name, ' ', last_name), COUNT(*)
FROM employees
GROUP BY first_name, last_name
ORDER BY COUNT(*) DESC;
#min, max, avg, total
SELECT MIN(salary), MAX(salary), AVG(salary), SUM(salary)
FROM salaries;
#COUNT(number of whatever...salary not the sum);
##instructors lecture below///
# GROUP BY LECTURE
# The "GROUP BY" clause is used to aggregate a table into various groups of shared characteristics.
#
# Grouping data is often accompanied with the use of aggregate functions to derive additional information
# about the various groupings.
#
# Most common aggregate functions are:
#
# MIN()
# MAX()
# SUM()
# AVG()
# COUNT()
#
# One or multiple columns may be grouped together.
#
# No table columns may exist in a SELECT clause that are not contained in the GROUP BY clause.
#
# The GROUP BY clause is after an optional where clause and before an optional order by clause.
#
# The order of a result set may be by an aggregate column but an aggregate column cannot be referenced in a where clause.
#
# A having clause is a way to use aggregate results as if in a where clause.
select first_name, AVG(LENGTH(last_name))
from employees
GROUP BY first_name;
# --------- Grouping by a single column
# What are the titles?
SELECT *
FROM titles;
# What are the unique titles?
SELECT DISTINCT title
FROM titles;
# What are the groups of like titles?
SELECT title
FROM titles
GROUP BY title;
# How many employees have held each title?
SELECT title, COUNT(*)
FROM titles
GROUP BY title;
# What gender are all the employees?
SELECT gender
FROM employees;
# all genders of all employees listed
# How many unique genders are recorded in this company?
SELECT DISTINCT gender
FROM employees;
# all distinct genders in company
# What the groups created for each gender?
SELECT gender
FROM employees # creating groups of employees by gender
GROUP BY gender;
# How many employees are within each gender designation?
SELECT gender, COUNT(*)
FROM employees # how many are in each gender group?
GROUP BY gender;
# How many people share the same hire date?
SELECT hire_date, COUNT(*) AS `No of Employees Hired`
FROM employees
GROUP BY hire_date
ORDER BY COUNT(*) DESC;
select MIN(salary)
from salaries;
# How many salaries are at a given amount?
SELECT salary, COUNT(*)
FROM salaries
GROUP BY salary
ORDER BY COUNT(*) DESC;
# --------- Grouping by multiple columns
# How many employees share the same birthday?
SELECT birth_date, COUNT(*)
FROM employees
GROUP BY birth_date;
# How many male and female employees share the same birthday?
SELECT gender, birth_date, COUNT(*)
FROM employees
GROUP BY gender, birth_date
ORDER BY birth_date;
# COUNT function
# Find total number of employees
SELECT COUNT(*)
FROM employees;
# Find how many employees share the same name
# =========== AGGREGATE FUNCTIONS
# 1 Find the most common name
# 2 Find the most common female and male name
# 3 Find total number of female and male employees
# 4 Find the highest employee number of each unique first name
# 5 Select the first and last hire date for each first name
# 6 Find the minimum, maximum, average, and total number of salaries
# 1 Find the most common name
SELECT first_name, last_name, COUNT(*)
FROM employees
GROUP BY first_name, last_name
ORDER BY COUNT(*) DESC;
# 2 Find the most common female and male name
SELECT first_name, last_name, gender, COUNT(*)
FROM employees
GROUP BY first_name, last_name, gender
ORDER BY COUNT(*) DESC;
# 3 Find total number of female and male employees
SELECT gender, COUNT(*)
FROM employees
GROUP BY gender;
# 4 Find the highest employee number of each unique first name
SELECT MAX(emp_no), first_name
FROM employees
GROUP BY first_name;
# Find the min, max, avg, and count of each employee's salaries
SELECT emp_no, MIN(salary), MAX(salary), AVG(salary), COUNT(*)
FROM salaries
GROUP BY emp_no;
# MIN and MAX
# Select the first and last hire date for each first name
SELECT first_name, MIN(hire_date) AS 'First Hired', MAX(hire_date) AS 'Last Hired'
FROM employees
GROUP BY first_name;
DESCRIBE salaries;
# Find the minimum, maximum, average, and total number of salaries
SELECT COUNT(salary), MIN(salary), MAX(salary), AVG(salary), SUM(salary)
FROM salaries;
# Find the min, max, avg, total, and count of each employee's salaries
SELECT emp_no, COUNT(salary), MIN(salary), MAX(salary), AVG(salary), SUM(salary)
FROM salaries
GROUP BY emp_no
ORDER BY MAX(salary) DESC;