You are on page 1of 4

SQL> create table student

2 (sid int,name varchar(20),major varchar(10),gpa float,tutorid int,


3 PRIMARY KEY (sid));

Table created.

SQL> insert into student values(101,'Bill','CIS',3.45,102);

1 row created.

SQL> insert into student values(102,'Mary','CIS',3.1,null);

1 row created.

SQL> insert into student values(103,'Sue','Marketing',2.95,102);

1 row created.

SQL> insert into student values(104,'Tom','Finanace',3.5,106);

1 row created.

SQL> insert into student values(105,'Alex','CIS',2.75,106);

1 row created.

SQL> insert into student values(106,'Sam','Marketing',3.25,103);

1 row created.

SQL> insert into student values(107,'Joan','Finance',2.9,102);

1 row created.

SQL> create table course


2 ( sid int,courseno varchar(10),coursename varchar(15),semester
varchar(10),year int,grade char
2),foreign key(sid) references student(sid));

Table created.

SQL> insert into course values(101,'CIS3400','DBMS1','Fall',1987,'B+');

1 row created.

SQL> insert into course values(101,'CIS3100','OOP1','Spring',1999,'A');

1 row created.

SQL> insert into course values(101,'MKT3000','Marketing','Fall',1997,'A');

1 row created.

SQL> insert into course values(102,'CIS3400','DBMS1','Spring',1987,'A');

1 row created.
SQL> insert into course values(102,'CIS3300','Network1','Summer',1987,'B');

1 row created.

SQL> insert into course values(102,'CIS4500','Network2','Fall',1987,'B+');

1 row created.

SQL> insert into course values(103,'MKT3100','Advertising','Spring',1998,'A');

1 row created.

SQL> insert into course values(103,'MKT3000','MArketing','Fall',1997,'A');

1 row created.

SQL> insert into course values(103,'MKT4100','Marketing','Summer',1998,'A');

1 row created.

SQL> select * from student;

SID NAME MAJOR GPA TUTORID


---------- -------------------- ---------- ---------- ----------
101 Bill CIS 3.45 102
102 Mary CIS 3.1
103 Sue Marketing 2.95 102
104 Tom Finanace 3.5 106
105 Alex CIS 2.75 106
106 Sam Marketing 3.25 103
107 Joan Finance 2.9 102

7 rows selected.

SQL> select * from course;

SID COURSENO COURSENAME SEMESTER YEAR GR


---------- ---------- --------------- ---------- ---------- --
101 CIS3400 DBMS1 Fall 1987 B+
101 CIS3100 OOP1 Spring 1999 A
101 MKT3000 Marketing Fall 1997 A
102 CIS3400 DBMS1 Spring 1987 A
102 CIS3300 Network1 Summer 1987 B
102 CIS4500 Network2 Fall 1987 B+
103 MKT3100 Advertising Spring 1998 A
103 MKT3000 Marketing Fall 1997 A
103 MKT4100 Marketing Summer 1998 A

9 rows selected.

SQL> select avg(gpa) from student;

AVG(GPA)
----------
3.12857143

SQL> select count(*) from student;


COUNT(*)
----------
7

SQL> select count(distinct sid) from course;


COUNT
----------
3

SQL> select major,avg(gpa) from student


2 where major='Finance' or major='CIS' group by major;

MAJOR AVG(GPA)
---------- ----------
CIS 3.1
Finance 3.2

SQL> select name from student where gpa=(select max(gpa) from student);

NAME
--------------------
Tom

SQL> select major,avg(gpa) as average


2 from student
3 group by major;

MAJOR AVERAGE
---------- ----------
CIS 3.1
Finance 3.2
Marketing 3.1

SQL> select distinct courseno,year from course order by year;

COURSENO YEAR
---------- ----------
CIS3300 1987
CIS3400 1987
CIS4500 1987
CIS3400 1997
MKT3000 1997
MKT3100 1998
MKT4100 1998
CIS3100 1999

8 rows selected.

SQL> select distinct courseno,grade from course order by grade;

COURSENO GR
---------- --
CIS3100 A
CIS3400 A
MKT3000 A
MKT3100 A
MKT4100 A
CIS3300 B
CIS3400 B+
CIS4500 B+

8 rows selected.

SQL> select sum(gpa) from student;

SUM(GPA)
----------
21.9