Answers to Assignment #4
Total Page:16
File Type:pdf, Size:1020Kb
download instant at www.easysemester.com Assignment #2 String and Aggregate Functions
1. Create a list of all students with middle initials (specifically, with a first name AND then a middle initial) AND remove the middle initial from the first name. Take care: some first names are a single initial only. [HINT: use the INSTR function embedded within other functions). (7 rows).
SELECT first_name Name, SUBSTR(first_name,1,INSTR(first_name,' ') - 1) "Instr Name" FROM student WHERE INSTR(first_name,' ') < INSTR(first_name,'.') AND INSTR(first_name,' ') <> 0 AND INSTR(first_name,'.') <> 0
2. Create a list of courses, sections and their capacity. Produce the result in the following format: (78 rows)
Course and Section CAPACITY ------350 Section 3 ------25 10 Section 2 ------15 20 Section 2 ------15 …
SELECT LPAD(course_no||' Section '||section_no, 20) "Course and Section", RPAD('-',10,'-') " ",LTRIM(capacity) Capacity FROM section
3. Count the number of students with a “v” in their last name. Use INSTR to produce the answer. (16)
SELECT COUNT(*) "Count of V’s" FROM student WHERE INSTR(LOWER(last_name),'v') > 0
4. Show the average cost of a course with no prerequisites (1195)
SELECT AVG(cost) "Average Cost" FROM course WHERE prerequisite IS NULL
5. Display the course number, number of sections and total capacity for courses having more than 3 sections.( 9 rows).
SELECT course_no Course, COUNT(section_no) "Section Count", SUM(capacity) Capacity FROM section GROUP BY course_no HAVING COUNT(section_no) > 3
6. List all instructors and how many sections they teach. (8 rows).
SELECT instructor_id ID, COUNT(*) FROM section GROUP BY Instructor_id
download instant at www.easysemester.com download instant at www.easysemester.com 7. Do #6 but limit it to instructors teaching more than 9 sections. (6 rows).
SELECT instructor_id ID, COUNT(*) FROM section GROUP BY Instructor_id HAVING COUNT(*) > 9
download instant at www.easysemester.com