Question

Perform SQL queries in the given data structure. write an SQL query that gives the number...

Perform SQL queries in the given data structure.

  1. write an SQL query that gives the number of courses taken for every student in the student table.
  2. For each instructor, show the instructor name and the number of sections that have been taught by that instructor. You do not need to include instructors who have never taught a section. List in order of decreasing the number of sections taught.
  3. Give the number of semester/year combinations in which sections have been offered.

create table classroom

      (building         varchar(15),

      room_number           varchar(7),

      capacity         numeric(4,0),

      primary key (building, room_number)

      );

create table department

      (dept_name        varchar(20),

      building         varchar(15),

      budget                   numeric(12,2) check (budget > 0),

      primary key (dept_name)

      );

create table course

      (course_id        varchar(8),

      title                  varchar(50),

      dept_name        varchar(20),

      credits          numeric(2,0) check (credits > 0),

      primary key (course_id),

      foreign key (dept_name) references department

            on delete set null

      );

create table instructor

      (ID               varchar(5),

      name             varchar(20) not null,

      dept_name        varchar(20),

      salary                 numeric(8,2) check (salary > 29000),

      primary key (ID),

      foreign key (dept_name) references department

            on delete set null

      );

create table section

      (course_id        varchar(8),

         sec_id              varchar(8),

      semester         varchar(6)

            check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),

      year             numeric(4,0) check (year > 1701 and year < 2100),

      building         varchar(15),

      room_number           varchar(7),

      time_slot_id          varchar(4),

      primary key (course_id, sec_id, semester, year),

      foreign key (course_id) references course

            on delete cascade,

      foreign key (building, room_number) references classroom

            on delete set null

      );

create table teaches

      (ID               varchar(5),

      course_id        varchar(8),

      sec_id                 varchar(8),

      semester         varchar(6),

      year             numeric(4,0),

      primary key (ID, course_id, sec_id, semester, year),

      foreign key (course_id,sec_id, semester, year) references section

            on delete cascade,

      foreign key (ID) references instructor

            on delete cascade

      );

create table student

      (ID               varchar(5),

      name             varchar(20) not null,

      dept_name        varchar(20),

      tot_cred         numeric(3,0) check (tot_cred >= 0),

      primary key (ID),

      foreign key (dept_name) references department

            on delete set null

      );

create table takes

      (ID               varchar(5),

      course_id        varchar(8),

      sec_id                 varchar(8),

      semester         varchar(6),

      year             numeric(4,0),

      grade                    varchar(2),

      primary key (ID, course_id, sec_id, semester, year),

      foreign key (course_id,sec_id, semester, year) references section

            on delete cascade,

      foreign key (ID) references student

            on delete cascade

      );

create table advisor

      (s_ID             varchar(5),

      i_ID             varchar(5),

      primary key (s_ID),

      foreign key (i_ID) references instructor (ID)

            on delete set null,

      foreign key (s_ID) references student (ID)

            on delete cascade

      );

create table time_slot

      (time_slot_id          varchar(4),

      day              varchar(1),

      start_hr         numeric(2) check (start_hr >= 0 and start_hr < 24),

      start_min        numeric(2) check (start_min >= 0 and start_min < 60),

      end_hr                 numeric(2) check (end_hr >= 0 and end_hr < 24),

      end_min          numeric(2) check (end_min >= 0 and end_min < 60),

      primary key (time_slot_id, day, start_hr, start_min)

      );

create table prereq

      (course_id        varchar(8),

      prereq_id        varchar(8),

      primary key (course_id, prereq_id),

      foreign key (course_id) references course

            on delete cascade,

      foreign key (prereq_id) references course

      );

Homework Answers

Answer #1

1. write an SQL query that gives the number of courses taken for every student in the student table.

select student.ID, count(course.course_id) from student, takes, course where student.ID = takes.ID and course.course_id = takes.course_id group by student.ID;

2. For each instructor, show the instructor name and the number of sections that have been taught by that instructor. You do not need to include instructors who have never taught a section. List in order of decreasing the number of sections taught.

select instructor.name, count(section.sec_id) from instructor, teaches, section where instructor.ID = teaches.ID and teaches.sec_id and section.sec_id and instructor.ID IN (select teaches.ID from teaches);

3. Give the number of semester/year combinations in which sections have been offered.

The column names and table names are case sensitive. Please be careful on that.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is my code but I am having trouble with the SECTION and REGISTRATION table. The...
This is my code but I am having trouble with the SECTION and REGISTRATION table. The error for section says course id isn't a column and with registration, it says there are no foreign key but that is all that is in the registration table. CREATE TABLE STUDENT(StudentID integer, StudentName char(25) NOT NULL, primary key(StudentName)) CREATE TABLE FACULTY(FacultyID integer, FacultyName char(25) NOT NULL, primary key(FacultyID)) CREATE TABLE COURSE(CourseID integer, CourseName char(15) NOT NULL, primary key(CourseID)) CREATE TABLE QUALIFIED(DateQualified date, FacultyID...
SQL Data: you can just copy paste it into mysql to. CREATE DATABASE University;USE University; CREATE...
SQL Data: you can just copy paste it into mysql to. CREATE DATABASE University;USE University; CREATE TABLE Student (  sid INT PRIMARY KEY,  name VARCHAR(20) NOT NULL,  address VARCHAR(20) NOT NULL,  major CHAR(2)); CREATE TABLE Professor (  pid INT PRIMARY KEY,  name VARCHAR(20) NOT NULL,  department VARCHAR(10) NOT NULL); CREATE TABLE Course (  cid INT PRIMARY KEY,  title VARCHAR(20) NOT NULL UNIQUE,  credits INT NOT NULL,  area VARCHAR(5) NOT NULL); CREATE TABLE Transcript (  sid INT,  cid INT,  pid INT,  semester VARCHAR(9),  year YEAR,  grade CHAR(1) NOT NULL,  PRIMARY KEY (sid, cid, semester, year),  FOREIGN KEY (sid) REFERENCES Student...
The queries listed below must be implemented with a set algebra operation. 1)Find the department name...
The queries listed below must be implemented with a set algebra operation. 1)Find the department name and code for all departments that only offered subjects that worth 6 credits. Note that a subject offered by a department means a lecturer of the department has been assigned to teach the subject. CREATE TABLE LPOSITION(    position       VARCHAR(20)   NOT NULL,   /* Position occupied       */    salary_level   CHAR   NOT NULL,       /* Salary level   */    CONSTRAINT LPosition_PKey PRIMARY KEY (position)...
Created the database and all the tables in phpMyadmin. In addition, write all the sql statements...
Created the database and all the tables in phpMyadmin. In addition, write all the sql statements used in perforing the tasks in a word document and submit that for grading. Lab 1: CREATE STATEMENT This statement is used to create a database or a table. CREATE DATABASE This statement is used to create a database Syntax: CREATE DATABASE database_name; OR CREATE SCHEMA database_name; Example: CREATE DATABASE Nyumbani; OR CREATE SCHEMA Nyumbani ; CREATE TABLE This SQL statement is used to...
how would the data insert MYSQL query for below table look like: CREATE TABLE Appointments( AppointmentID...
how would the data insert MYSQL query for below table look like: CREATE TABLE Appointments( AppointmentID int NOT NULL, Date Time datetime, Details varchar(100), Remarks varchar(200), POwnerID int, TenantID int, SerAgtID int, PRIMARY KEY (AppointmentID), FOREIGN KEY (POwnerID) REFERENCES Property Owners (POwnerID), FOREIGN KEY (TenantID) REFERENCES Tenants (TenantID), FOREIGN KEY (SerAgtID) REFERENCES Service Agents (SerAgtID));
1) Add to a relational table DEPARTMENT information about the total number of employees in each...
1) Add to a relational table DEPARTMENT information about the total number of employees in each department. Note, that if a department has not employee then for such a department the total number of employees must be set to zero (0). The total number of employees must be a positive number no more than 999. Use SELECT statement to list the contents of an extended relational table DEPARTMENT in the descending order of the total number of employees. Finally, remove...
1. For the Pet Database described in the previous practical, write an SQL statement with a...
1. For the Pet Database described in the previous practical, write an SQL statement with a subquery to list all the people in the Owner table who own more than one pet. should be a collum of the total number of pets owned, owner id, first name and last name -- DROP TABLE PetAndOwner, Pet, PetType, Owner; CREATE TABLE PetType ( petTypeId VARCHAR(10) PRIMARY KEY, animalType VARCHAR(20), breed VARCHAR(20) ); CREATE TABLE Owner ( ownerId VARCHAR(10) PRIMARY KEY, firstName VARCHAR(20),...
Write a script to create the following tables with attributes as specified(SQL) Customer table with Customer’s...
Write a script to create the following tables with attributes as specified(SQL) Customer table with Customer’s id, name, address, city as varchar, customer’s date of birth as date type and zip code number where the customers id is the primary key in the table, name and date of birth are mandatory. Id has a 10-character limit, name, address and city have a 50-character limit, zip has a 5-character limit Product Table with Product id, description and finish as varchar, price...
SQL DATABASE Task 4 [1.5 marks] Create Index (0.5 marks) Currently, the database only contains a...
SQL DATABASE Task 4 [1.5 marks] Create Index (0.5 marks) Currently, the database only contains a small number of records. However, the data contained within it is expected to grow significantly in the future. Creating indexes on commonly searched columns is a way performance issues can be minimized. Write a command to create an index on student_name column of the student table. Create view – 1 mark Write a command to create a view to list the student ID and...
SQL Write the queries necessary to obtain the required information 1) Use sub query and “in”...
SQL Write the queries necessary to obtain the required information 1) Use sub query and “in” keyword to print the salesreps (ids) who have taken order for the companies ‘Zetacorp’ or ‘JCP Inc.’ . Duplicate rows are not allowed 2) Use sub query to find the id and the name of every sales rep that represents at least one customer with a credit limit of greater than $5000. 3) Use sub query and keyword “exists” to list the id and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT