-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdemo-sql.sql
More file actions
54 lines (37 loc) · 1.06 KB
/
demo-sql.sql
File metadata and controls
54 lines (37 loc) · 1.06 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
-- create a table
CREATE TABLE public.student
(
s_id integer NOT NULL,
s_name character varying(50) ,
s_major character varying(20) ,
s_year integer,
CONSTRAINT student_pkey PRIMARY KEY (s_id)
)
-- adding data
insert into public.student (s_id,s_name,s_major,s_year)
values (1,'s1','GS',2020);
insert into public.student (s_id,s_name,s_major,s_year)
values (2,'s2','IA',2020);
insert into public.student (s_id,s_name,s_major,s_year)
values (3,'s3','CS',2019);
insert into public.student (s_id,s_name,s_major,s_year)
values (4,'s4','GS',2019);
insert into public.student (s_id,s_name,s_major,s_year)
values (5,'s5','GS',2019);
-- querying data
select * from public.student;
select s_name,s_year from public.student
order by s_year ASC;
-- filtering data
select s_major,s_name,s_year from public.student
where s_major = 'GS'
order by s_year ASC
limit 1
-- aggregate functions
select count(s_id) as num_enroll
from public.student
where s_major = 'GS'
-- grouping data
select s_major, count(s_id) as num_enroll
from public.student
group by s_major