Vous êtes sur la page 1sur 2

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
-------------------350 Section 3
10 Section 2
20 Section 2

-------------------------------------

CAPACITY
---------------------------------------25
15
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 Vs"
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
7. Do #6 but limit it to instructors teaching more than 9 sections. (6 rows).
SELECT
FROM
GROUP
HAVING

instructor_id ID, COUNT(*)


section
BY Instructor_id
COUNT(*) > 9

Vous aimerez peut-être aussi