WRITE Queries (Question 6-10) Questions: For the sample DDL provided BELOW , pro
ID: 2246279 • Letter: W
Question
WRITE Queries (Question 6-10)
Questions: For the sample DDL provided BELOW , provide the SQL that will return:
1) The names of all classes that could be taught (1 pt)
2) The names of classes taught in quarter 51 (1 pt)
3) The unique set of last names of all students, in ascending order. (1 pt)
4) The most common professor last names, ordered in terms of popularity (1 pt)
5) Classes for the "Computer Science" major that are offered in quarter 75 (1 pt)
6) The first and last names of all students that have the same last name as a professor (2 pts)
7) The last names (duplicates allowed) of all students with a first name that starts with an "M" and is 3rd year in school (2 pts)
8) The grades for all students that took a class named "CS 4660", taught by Greg Barish. (2 pts) 9) The set of majors that have been chosen by at least 5 students. (2 pts)
10) The professors that teach classes for the "Computer Science" major (2 pts)
- -- -- - - - -- - - - - - - -- - - -- - - - -- - - -- -- - - - -- - - -- - -- - - -- - - - - - - - -- - -
DROP TABLE IF EXISTS enrolled;
DROP TABLE IF EXISTS schedule;
DROP TABLE IF EXISTS class;
DROP TABLE IF EXISTS student;
DROP TABLE IF EXISTS professor;
DROP TABLE IF EXISTS major;
DROP TABLE IF EXISTS major_classes;
CREATE TABLE class (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL UNIQUE,
PRIMARY KEY (id)
);
CREATE TABLE major (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL UNIQUE,
PRIMARY KEY (id)
);
CREATE TABLE professor (
id int(11) NOT NULL AUTO_INCREMENT,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE student (
id int(11) NOT NULL AUTO_INCREMENT,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
year_in_school int(11),
major_id int(11),
PRIMARY KEY (id),
FOREIGN KEY (major_id) REFERENCES major (id)
);
CREATE TABLE major_classes (
id int(11) NOT NULL AUTO_INCREMENT,
major_id int(11),
class_id int(11),
PRIMARY KEY (id),
FOREIGN KEY (major_id) REFERENCES major (id),
FOREIGN KEY (class_id) REFERENCES class (id)
);
CREATE TABLE schedule (
id int(11) NOT NULL AUTO_INCREMENT,
class_id int(11) NOT NULL,
professor_id int(11) NOT NULL,
quarter int(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (class_id) REFERENCES class (id),
FOREIGN KEY (professor_id) REFERENCES professor (id)
);
CREATE TABLE enrolled (
id int(11) NOT NULL AUTO_INCREMENT,
student_id int(11),
class_offered_id int(11),
grade char(2),
PRIMARY KEY (id),
FOREIGN KEY (student_id) REFERENCES student (id),
FOREIGN KEY (class_offered_id) REFERENCES schedule (id)
);
1- 5 queries answers are below,
1) The names of all classes that could be taught (1 pt)
select name from class;
2) The names of classes taught in quarter 51 (1 pt)
select class.name
from class, schedule
where class.id = schedule.class_id
and schedule.quarter = 51;
3) The unique set of last names of all students, in ascending order. (1 pt)
select distinct last_name from student order by last_name desc;
4) The most common professor last names, ordered in terms of popularity (1 pt)
select first_name, last_name
from professor
where id = (
select professor_id
from schedule
group by professor_id
order by COUNT(*) DESC
limit 1;
);
5) Classes for the "Computer Science" major that are offered in quarter 75 (1 pt)
select class.name
from class, schedule
where class.id = schedule.class_id
and schedule.quarter = 75
and class.id in (
select major_classes.class_id
from major_classes, major
where major.name = "Computer Science"
and major.id = major_classes.major_id
);
Explanation / Answer
6)- Select student.first_name,student.last_name
from student,professor
where student.last_name=professor.last_name;
7-Select student.last_name
from student
where name like 'M%'
AND
year_in_school=3;
8)-Select enrolled.grade from
enrolled,class
where class.name='CS 4660';
9)- select major.name
from major,student
where count(student.major_id)>=5;
10)-
select professor.name
from professor,major,class,schedule,major_classes
where class.name='Computer science'
AND
major.id=major_classes.major_id
AND
class.id=schedule.class_id
AND
professor.id=schedule.professor_id;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.