Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the following tables: student ( sid , sname, sex, age, year, gpa) dept (

ID: 645710 • Letter: C

Question

Create the following tables:

student (sid, sname, sex, age, year, gpa)
dept (dname, numphds)
prof (pname, dname)
course (dname, cno, cname)
major (dname, sid)
section (dname, cno, sectno, pname)
enroll (sid, dname, cno, sectno, grade)

Queries

1. Print the names of professors who work in departments that have fewer than 50 PhD students.
2. Print the names of the students with the lowest GPA.
3. For each Computer Sciences class, print the class number, section number, and the average gpa of the students enrolled in the class.
4. Print the names and section numbers of all classes with more than six students enrolled in them.
5. Print the name(s) and sid(s) of the student(s) enrolled in the most classes.
6. Print the names of departments that have one or more majors who are under 18 years old.
7. Print the names and majors of students who are taking one of the College Geometry courses.
8. For those departments that have no major taking a College Geometry course print the department name and the number of PhD students in the department.
9. Print the names of students who are taking both a Computer Sciences course and a Mathematics course.
10. Print the age difference between the oldest and the youngest Computer sciences major.
11. For each department that has one or more majors with a GPA under 1.0, print the name of the department and the average GPA of its majors.
12. Print the ids, names and GPAs of the students who are currently taking all the Civil Engineering courses.

Explanation / Answer

Answer:

Tables creation:

CREATE TABLE student

(

sid int,

sname varchar(255),

sex character(1),

age int,

year int,

gpa numeric(3,2),

PRIMARY KEY (sid)

);

CREATE TABLE dept

(

dname varchar(255),

numphds int,

PRIMARY KEY (dname)

);

CREATE TABLE prof

(

pname varchar(255),

dname varchar(255),

PRIMARY KEY (pname),

FOREIGN KEY(dname) REFERENCES dept(dname)

);

CREATE TABLE course

(

cno int,

cname varchar(255),

dname varchar(255),

PRIMARY KEY (cno),

FOREIGN KEY(dname) REFERENCES dept(dname)

);

CREATE TABLE major

(

dname varchar(255),

sid int,

FOREIGN KEY(dname) REFERENCES dept(dname),

FOREIGN KEY(sid) REFERENCES student(sid)

);

CREATE TABLE section

(

dname varchar(255),

cno int,

sectno int,

pname varchar(255),

PRIMARY KEY(sectno),

FOREIGN KEY(dname) REFERENCES dept(dname),

FOREIGN KEY(cno) REFERENCES course(cno),

FOREIGN KEY(pname) REFERENCES prof(pname)

);

CREATE TABLE enroll

(

sid int,

dname varchar(255),

cno int,

sectno int,

grade varchar(255),

FOREIGN KEY(sid) REFERENCES student(sid),

FOREIGN KEY(dname) REFERENCES dept(dname),

FOREIGN KEY(cno) REFERENCES course(cno),

FOREIGN KEY(sectno) REFERENCES section(sectno)

);

Inserting values:

INSERT INTO dept(dname,numphds) VALUES ('CSE', 60);

INSERT INTO dept(dname,numphds) VALUES ('MECH', 10);

INSERT INTO prof(pname,dname) VALUES ('John', 'CSE');

INSERT INTO prof(pname,dname) VALUES ('Jack', 'MECH');

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (1,'Alice','M',19,2010,7.00);

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (2,'Angel','F',18,2011,8.00);

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (3,'Bob','M',19,2010,8.00);

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (4,'Lisa','F',18,2011,8.00);

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (5,'Joseph','M',19,2010,7.00);

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (6,'King','M',18,2011,8.00);

INSERT INTO student(sid,sname,sex,age,year,gpa) VALUES (7,'Nick','M',19,2010,7.00);

INSERT INTO course(cno,cname,dname) VALUES (1,'Computer Science','CSE');

INSERT INTO course(cno,cname,dname) VALUES (2,'Mechanics','MECH');

INSERT INTO section(dname,cno,sectno,pname) VALUES ('CSE',1,1,'John');

INSERT INTO section(dname,cno,sectno,pname) VALUES ('CSE',1,2,'John');

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (1,'CSE',1,1,5);

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (2,'CSE',1,1,6);

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (3,'CSE',1,1,5);

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (4,'CSE',1,1,6);

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (5,'CSE',1,1,5);

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (6,'CSE',1,1,6);

INSERT INTO enroll(sid,dname,cno,sectno,grade) VALUES (7,'CSE',1,1,5);

Queries:

1.

SELECT prof.pname

FROM prof

JOIN dept

ON prof.dname=dept.dname

WHERE dept.numphds < 50;

2.

SELECT sname,MIN(gpa) AS LowestGPA

FROM student;

3.

SELECT enroll.cno,enroll.sectno,Avg(student.gpa)

FROM enroll,student

JOIN course

ON course.cname='Computer Science';

4.

SELECT course.cname,enroll.sectno,count(enroll.sid>6)

FROM enroll,course

ON enroll.cno=course.cno;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote