Question: Create a table named 'course' with the fields: id , course _ name, dept _ name. Insert 6 records ( 2 for the English department,

Create a table named 'course' with the fields: id, course_name, dept_name.
Insert 6 records (2 for the English department, 3 for the Math department, and 1 for a different department of your choice)
Create a function that returns the count of the number of courses in each department.
Query the number of courses in the Math department.
Please include explanations if possible.
Code is below:
create database chapter5;
use chapter5;
CREATE TABLE student
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred int);
INSERT INTO student (id, name, dept_name, tot_cred) values ('44553', 'Peltier', 'Physics', 56);
INSERT INTO student (id, name, dept_name, tot_cred) values ('45678', 'Levy', 'Physics', 46);
INSERT INTO student (id, name, dept_name, tot_cred) values ('54321', 'Williams', 'Comp. Sci.',54);
INSERT INTO student (id, name, dept_name, tot_cred) values ('55739', 'Sanchez', 'Music', 38);
INSERT INTO student (id, name, dept_name, tot_cred) values ('70557', 'Snow', 'Physics', 0);
INSERT INTO student (id, name, dept_name, tot_cred) values ('55740', 'Jacobs', 'Music', 42);
SELECT * FROM student;
DELIMITER $$
CREATE FUNCTION dept_count (dept_name varchar(20))
RETURNS int
DETERMINISTIC
BEGIN
DECLARE d_count int;
SELECT count(*) INTO d_count FROM student WHERE student.dept_name = dept_name;
RETURN (d_count);
END; $$
DELIMITER ;
SELECT dept_count('Music');

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!