diff --git a/a.sql b/a.sql index e279c74..e62681a 100644 --- a/a.sql +++ b/a.sql @@ -1,4 +1,4 @@ --- What are the names (first name, last name) of all the students? +--What are the names (first name, last name) of all the students? [72] select firstName, lastName from Students \ No newline at end of file diff --git a/b.sql b/b.sql index a5db20f..2dab992 100644 --- a/b.sql +++ b/b.sql @@ -1,3 +1,5 @@ +--Same as in a) but produce a sorted listing. Sort first by last name and then by first name. + select firstName, lastName from Students order by lastName, firstName \ No newline at end of file diff --git a/c.sql b/c.sql index 1a4b02f..94ea555 100644 --- a/c.sql +++ b/c.sql @@ -1,3 +1,5 @@ +--Which students were born in 1975? + select firstName, lastName from Students where pNbr like '1975%' \ No newline at end of file diff --git a/d.sql b/d.sql index ed901c3..952ec1a 100644 --- a/d.sql +++ b/d.sql @@ -1,3 +1,6 @@ +--What are the names of the female students, and which are their person numbers? +--The next-to-last digit in the person number is even for females[26] + select firstName, pNbr from Students where substring(pNbr, LENGTH(pNbr) -1, 1) % 2 = 0 \ No newline at end of file diff --git a/e.sql b/e.sql index 3c37edf..4954385 100644 --- a/e.sql +++ b/e.sql @@ -1,2 +1,4 @@ -select distinct pNbr +--How many students are registered in the database? + +select count(*) from Students \ No newline at end of file diff --git a/f.sql b/f.sql index 83cc0ed..0855300 100644 --- a/f.sql +++ b/f.sql @@ -1,3 +1,6 @@ +--Which courses are offered by the department of Mathematics +--(course codes FMAxxx)? [24] + select courseName from Courses where courseCode like '%FMA%' \ No newline at end of file diff --git a/g.sql b/g.sql index 4b56fd6..fef07cb 100644 --- a/g.sql +++ b/g.sql @@ -1,3 +1,5 @@ +--Which courses give more than five credits? [21] + select courseName from Courses where credits > 5 \ No newline at end of file diff --git a/h.sql b/h.sql index 6dbec39..03bcd97 100644 --- a/h.sql +++ b/h.sql @@ -1,3 +1,6 @@ +--Which courses (course codes only) have been taken by the student with person +--num-ber 790101-1234? [7] + select courseCode from TakenCourses where pNbr = '790101-1234' \ No newline at end of file diff --git a/i.sql b/i.sql index 36256ef..80149aa 100644 --- a/i.sql +++ b/i.sql @@ -1,3 +1,5 @@ +--What are the names of these courses, and how many credits do they give? + select courseName, credits, courseCode, pNbr from Courses join Students on Students.pNbr diff --git a/j.sql b/j.sql index 82e5a28..707adcb 100644 --- a/j.sql +++ b/j.sql @@ -1,3 +1,5 @@ +--How many credits has the student taken? + select sum(credits) as totalSum from Courses join Students on Students.pNbr diff --git a/k.sql b/k.sql index bd15b22..3e356b1 100644 --- a/k.sql +++ b/k.sql @@ -1,3 +1,5 @@ +--Which is the student’s grade average on the courses that he has taken? + select avg(TakenCourses.grade) as averageGrade, Students.pNbr from TakenCourses join Students on TakenCourses.pNbr = Students.pNbr diff --git a/l.sql b/l.sql index d23539e..95c0c48 100644 --- a/l.sql +++ b/l.sql @@ -1,3 +1,5 @@ +--Same questions as in questions h)-k), but for the student Eva Alm. [5] + select firstName, lastName, avg(TakenCourses.grade) as averageGrade, TakenCourses.courseCode, Courses.courseName, sum(Courses.credits) as totalCredits from TakenCourses join Students on TakenCourses.pNbr = Students.pNbr diff --git a/m.sql b/m.sql index f873fd8..4e01540 100644 --- a/m.sql +++ b/m.sql @@ -1,4 +1,6 @@ -select distinct firstName +--Which students have taken 0 credits? [16] + +select firstName, lastName from Students left join TakenCourses on Students.pNbr = TakenCourses.pNbr where TakenCourses.pNbr is null \ No newline at end of file diff --git a/n.sql b/n.sql index d6172ce..c979285 100644 --- a/n.sql +++ b/n.sql @@ -1,3 +1,6 @@ +--Which student has the highest grade average? Advice: define and use a view that +--gives the person number and grade average for each student. + DROP VIEW IF EXISTS StudentsAverages; CREATE VIEW StudentsAverages AS diff --git a/o.sql b/o.sql index 1d82c19..48d7f0d 100644 --- a/o.sql +++ b/o.sql @@ -1,3 +1,6 @@ +--List the person number and total number of credits for all students. +--Students with no credits should be included in the list! [72] + DROP VIEW IF EXISTS StudentsCredits; CREATE VIEW StudentsCredits AS diff --git a/p.sql b/p.sql index eb58a4a..80c1b23 100644 --- a/p.sql +++ b/p.sql @@ -1,7 +1,9 @@ +--Same question as in question o) but with names instead of person numbers. + DROP VIEW IF EXISTS StudentsCredits; CREATE VIEW StudentsCredits AS -SELECT Students.firstName, sum(credits) as totalCredits +SELECT Students.firstName, Students.lastName, sum(credits) as totalCredits FROM Students JOIN TakenCourses ON Students.pNbr = TakenCourses.pNbr JOIN Courses ON TakenCourses.courseCode = Courses.courseCode diff --git a/q.sql b/q.sql index 960e784..45b6dc2 100644 --- a/q.sql +++ b/q.sql @@ -1,3 +1,5 @@ +--Is there more than one student with the same name? If so, who are these students? [7] + SELECT firstName, lastName, count(*) as nbrOfStudents FROM Students GROUP BY firstName, lastName