I was given the following task:
Part1:
You are to design a schema (some tables) in which a student will have id, name; a course will have id, name; A schedule will have roomId, starting time; A room will have id, location, capacity.
I built the following: Students (varchar(10) PK id, first, last, courseId) --btw I think it's better to add an enumertion (identity) column and call this column + id column under PK - so there could be more than one row for each student (each for another course taken)
Courses (varchar(10) PK id, name)
Scheduler (FK to Courses CourseId, FK to Courses RoomId, date day, time(7) hour) -- What dataTypes are suitable here?
Rooms (PK id, location, capacity)
Do you think of a better design ?
Part2: You're to write the following queries a. return a list of all students and how many courses each of them took? order from highest to lowest.
select s.id as STUDENT_ID,
s.first as FIRST_NAME,
s.last as LAST_NAME,
count (s.CourseId) as COURSE_COUNT
from dbo.Students as s
Group by s.id
order by count(s.CourseId) desc
--STUDENT_ID, FIRST_NAME, LAST_NAME, COURSE_COUNT
b. Which is the course with the highest number of students?
Create view q2 as
select top 1
c.name as COURSE_NAME,
count(s.id) as STUDENT_COUNT
from dbo.Students as s
JOIN dbo.Courses as c
ON (s.CourseId = c.id)
Group by c.name
order by count(s.id) desc
--COURSE_NAME, STUDENT_COUNT
c. Which rooms has 2 or more courses overlapping?
Create view q3 as
select sc.room as CLASSROOM_ID,
r.location as LOC,
COUNT(sc.courseId) as OVERLAP_COURSE_COUNT
from dbo.Schedule as sc
JOIN dbo.Rooms as r
ON (sc.room = r.room)
Group by sc.room, sc.day, sc.hour
HAVING COUNT(sc.courseId) > 1
--CLASSROOM_ID, LOC (Location), OVERLAP_COURSE_COUNT
d. Which are the courses with are not scheduled in the Schedule ?
Create view q4 as
select c.id as COURSE_ID,
c.name as NAME
from dbo.Courses as c
where c.id not in
(select sc.courseId
from dbo.Schedule as sc)
-- COURSE_ID, NAME
-- not in (course_id from dbo.Schedule)
Do you agree with these queries? Any better way to implement? I would try to execute them but would appreciate any comments.