-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab7.sql
More file actions
151 lines (92 loc) · 2.8 KB
/
lab7.sql
File metadata and controls
151 lines (92 loc) · 2.8 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
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
--You can use avg & sum for numeric data:
select sum(salary),avg(salary)
from employees;
--You can not use avg & sum with charcter & date data types:
select sum(first_name), avg(job_id), sum(hire_date)
from employees;
--You can use max & min for numeric , charcter & date data types:
select salary
from employees
order by salary;
select max(salary),min(salary)
from employees;
select max(hire_date),min(hire_date)
from employees;
select max(last_name),min(last_name)
from employees;
--count(*) returns the number of rows in a table including duplicate rows and rows containing null values:
select *
from employees;
select count(*)
from employees;
select count(*)
from employees
where department_id=80;
--count(expr) returns the number of rows with non-null values for expr:
select commission_pct
from employees
where department_id=50;
select count(commission_pct)
from employees
where department_id=50;
select commission_pct
from employees
where department_id=80;
select count(commission_pct)
from employees
where department_id=80;--returns the duplicate rows
--count(distinct expr) returns the number of distinct non-null values of expr:
select count(department_id)
from employees;
select count(distinct department_id)
from employees;
--Group functions ignore null values in the column :
select count(commission_pct)
from employees;
--The NVL function forces group functions to include null values:
select count(nvl(commission_pct,0))
from employees;
--You can divide rows in a table into smaller groups by using the group by clause:
select department_id ,avg(salary)
from employees; --error
--You must include all the columns in the group by clause which are in the select statement:
select department_id ,avg(salary)
from employees
group by department_id;
select department_id ,job_id,avg(salary)
from employees
group by department_id;
select department_id ,job_id,avg(salary)
from employees
group by department_id,job_id;
--The group by columns does not have to be in the select list:
select avg(salary)
from employees
group by department_id;
--You cannot use a column alias in the group by clause:
select department_id n ,avg(salary)
from employees
group by n;
--You can use where & order by with group functions
select department_id n ,job_id ,avg(salary)
from employees
where department_id>40
group by department_id,job_id
order by n;
--You cannot use the where clause to restrict groups:
select department_id n ,avg(salary)
from employees
where avg(salary)>8000
group by department_id
order by n;
--Restricting group results with the having clause:
select department_id n ,avg(salary)
from employees
group by department_id
where avg(salary)>8000
order by n;
select department_id n ,avg(salary)
from employees
group by department_id
having avg(salary)>8000
order by avg(salary);