MYSQL Not all inserted values being displayed - mysql

I should be able to show customer name, pet name, procedures, costs and total costs in one SQL SELECT statement. I am only getting one row of values, of which one of the values is NULL. I can't figure out what is wrong with my SQL statement or tables that it is causing it to not return all of the inputted values.
Here is an image of what I am getting as a result:
CREATE DATABASE IF NOT EXISTS vet;
USE vet;
CREATE TABLE IF NOT EXISTS customer (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(70)
);
CREATE TABLE IF NOT EXISTS invoice (
invoice_id INT PRIMARY KEY AUTO_INCREMENT,
invoice_date DATE,
customer_id INT ,
CONSTRAINT FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS pet (
pet_id INT PRIMARY KEY AUTO_INCREMENT,
pet_name VARCHAR(50),
customer_id INT,
CONSTRAINT FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS procedures (
procedure_id INT PRIMARY KEY AUTO_INCREMENT,
procedure_name VARCHAR(70),
amount DECIMAL
);
CREATE TABLE IF NOT EXISTS invoice_pet (
invoice_id INT,
pet_id INT,
CONSTRAINT FOREIGN KEY (invoice_id) REFERENCES invoice(invoice_id),
CONSTRAINT FOREIGN KEY (pet_id) REFERENCES pet(pet_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS procedures_pet (
procedure_id INT,
pet_id INT,
CONSTRAINT FOREIGN KEY (procedure_id) REFERENCES procedures(procedure_id),
CONSTRAINT FOREIGN KEY (pet_id) REFERENCES pet(pet_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
INSERT INTO invoice (invoice_date) VALUES ('2004-04-05');
INSERT INTO invoice (invoice_date) VALUES ('2014-12-05');
INSERT INTO invoice (invoice_date) VALUES ('2009-08-29');
INSERT INTO invoice (invoice_date) VALUES ('2016-07-15');
INSERT INTO customer (customer_name) VALUES ('John Garett');
INSERT INTO customer (customer_name) VALUES ('Mary Wist');
INSERT INTO customer (customer_name) VALUES ('Beth Smith');
INSERT INTO customer (customer_name) VALUES ('Rick Sanchez');
INSERT INTO pet (pet_name, customer_id) VALUES ('Rover', 1);
INSERT INTO pet (pet_name, customer_id) VALUES ('Max', 3);
INSERT INTO pet (pet_name, customer_id) VALUES ('Munchie', 4);
INSERT INTO pet (pet_name, customer_id) VALUES ('Dixon', 2);
INSERT INTO pet (pet_name, customer_id) VALUES ('Lucky', 4);
INSERT INTO procedures (procedure_name, amount) VALUES ('Rabies Vaccination', 30.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Sterilization', 190.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Dental Surgery', 120.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Cystotomy', 200.00);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (1, 1);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (2, 1);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (1, 4);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (3, 2);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (1, 1);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (3, 2);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (4, 4);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (2, 1);
SELECT inv.invoice_id, cust.customer_name, p.pet_name, pro.procedure_name,
pro.amount AS cost, SUM(amount) AS totalcost
FROM vet.procedures pro LEFT JOIN vet.procedures_pet propet
ON pro.procedure_id = propet.procedure_id
LEFT JOIN vet.pet p
ON propet.pet_id = p.pet_id
LEFT JOIN vet.invoice_pet invpet
ON p.pet_id = invpet.pet_id
LEFT JOIN vet.invoice inv
ON invpet.invoice_id = inv.invoice_id
LEFT JOIN vet.customer cust
ON inv.customer_id = cust.customer_id

The following gives each customer, their pets, procedures those pets have had (if any) and the cost of those procedures.
You don't say what you mean by "total cost" (total per customer? per pet? grand total?) so I have gone with cost per customer (see correlated sub query)
SELECT c.customer_name,
p.pet_name,
pr.procedure_name,
pr.amount,
(
SELECT SUM(amount)
FROM procedures pr1
JOIN procedures_pet pp1
ON pr1.procedure_id = pp1.procedure_id
JOIN pet p1
ON p1.pet_id = pp1.pet_id
JOIN customer c1
ON p1.customer_id = c1.customer_id
WHERE c1.customer_id = c.customer_id
) AS totalcost
FROM customer c
JOIN pet p
ON c.customer_id = p.customer_id
LEFT JOIN procedures_pet pp
ON pp.pet_id = p.pet_id
LEFT JOIN procedures pr
ON pr.procedure_id = pp.procedure_id
ORDER BY customer_name,
pet_name,
procedure_name;

The problem is that your invoices do never get any "customer_id" because you do not insert a value here in your insert statement.
So all tuples have "null" for customer_id.
Answering here as I cannot comment yet:
Of course you'll have to add your customers to the database first in this case. Otherwise they cannot be referenced.

Related

Update Table from Trigger

I have the following tables in a database:
CREATE TABLE `CRUISE-RES` (
`cruiseid` INT,
`end-day` DATE,
`start-day` DATE
PRIMARY KEY (`cruiseid`));
CREATE TABLE `ROOM` (
`cruise-id` INT,
`price` FLOAT,
FOREIGN KEY (`cruise-id`));
CREATE TABLE `PROFIT` (
`cruiseid` INT,
`total` FLOAT);
With the following sample table inserts:
-- cruise table inserts
insert into `CRUISE-ID` (`cruiseid`,`start-day`,`end-day`)
values (1, '2022/01/01', '2022/01/05'), (1, '2022/01/05', '2022/01/10'), (2, '2022/01/05', '2022/01/10')
-- room table inserts
insert into ROOM (price,`cruise-id`)
values (5,1), (10,1), (25,2)
I also have the following function that shows the profit of each cruiseid based on the number of days in the CRUISE-RES * price per day.
SELECT c.`cruiseid`, sum(rm.`price`*(DATEDIFF(c.`end-date`, c.`start-date`))) AS 'total_profit'
FROM ROOM rm
JOIN `CRUISE-RES` c
ON rm.`cruise-id` = c.cruiseid
GROUP BY rm.`cruise-id`,'cruiseid'
How can I use this information on a trigger that updates the PROFIT table after each insert into CRUISE-RES table?

Mysql How to SELECT information based other tables

I would like to get tokens. E.g if i would like to SELECT Gregor, Liza and if we look into committee it should retrive then their own tokens and also Matt, Bob tokens
Table structure:
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
);
INSERT INTO users (name)
VALUES ("Gregor"),
("Liza"),
("Matt"),
("Bob"),
("Tom");
CREATE TABLE committee(
user_id INT,
friend_id INT,
member_id INT,
PRIMARY KEY (`user_id`, `friend_id`, `member_id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
FOREIGN KEY (`friend_id`) REFERENCES `users` (`id`),
FOREIGN KEY (`member_id`) REFERENCES `users` (`id`)
);
INSERT INTO committee (user_id, friend_id, member_id)
VALUES (1, 2, 3),
(1, 2, 4);
CREATE TABLE tokens(
user_id INT,
token VARCHAR(255)
);
INSERT INTO tokens (user_id, token)
VALUES (1, "lclasld2"),
(2, "xxkakdasd"),
(3, "123kdkfs"),
(4, "hjj32"),
(5, "zz3dja");
Current query i got:
SELECT token FROM tokens WHERE user_id IN (1, 2);
How it behave now:
user_id, token
1, lclasld2
2, xxkakdasd
What i expect, when i run the query:
user_id, token
1, lclasld2
2, xxkakdasd
3, 123kdkfs
4, hjj32
I think it's enough to use the query without where clause
SELECT * FROM tokens
Your database design could be better or the schema values that you have used for demo arent correct.
You can try this
SELECT token
FROM tokens
WHERE user_id IN (SELECT member_id
FROM committee
WHERE user_id IN ( 1, 2 ))
OR user_id IN ( 1, 2 );

Error Code: 1054. Unknown column 's.Product_id' in 'on clause'Error Code: 1054. Unknown column 's.Product_id' in 'on clause'

I have an error in MySQL: Error Code: 1054. Unknown column 's.Product_id' in 'on clause'
It appears at:
SELECT c.customer_ID, p.Product_id, p.Product_price, s.Items_quantity, (p.Product_price * s.Items_quantity) AS total FROM Products p LEFT JOIN Shopping_list s ON p.Product_id = s.Product_id LEFT JOIN Customer_sign_in c ON c.customer_ID = s.customer_ID LIMIT 0, 50000
Does anyone has one idea why is it happening?
You should use LEFT JOIN
select c.customer_ID, p.Product_id, p.Product_price, s.Items_quantity,
(p.Product_price * s.Items_quantity) as total from Products p LEFT JOIN Shopping_list s ON p.Product_id = s.Product_id LEFT JOIN Customer_sign_in c ON c.customer_ID = s.customer_ID;
Are you sure you have table Shopping_list and a column named Product_id in it?
If you don't have it, create the column and try again.
drop database if exists shopmeout;
create database shopmeout;
use shopmeout;
drop table if exists Customer_sign_in;
CREATE TABLE Customer_sign_in (
customer_ID INT (50) not null,
Name_customer varchar(255) not null,
Credit_card BIGINT(100) not null,
Password_cust TEXT(150) not null,
Email_cust TEXT(50) not null,
primary key (customer_ID)
);
insert into Customer_sign_in values( 19867889, 'Stefan Darescu', 9018783269173917, 'personalcomputer84', 'stefandarescu#gmail.com');
insert into Customer_sign_in values( 11958178, 'Mogens Hemming', 5090180120973782,'masadebiliard4', 'mogenshemming#yahoo.com');
insert into Customer_sign_in values( 52562523, 'Viggo Kristian', 9015908218093698,'asawece', 'viggokristian#gmail.com');
insert into Customer_sign_in values( 42515323, 'Asbjørn Hjalmar', 0198490128761903,'esrjgads', 'asbjornhjalmar#hotmail.com');
insert into Customer_sign_in values( 69723642, 'Rasmus Bjarne', 9180583647839793,'Sgsyhdfs', 'rasmusbjarne#outlook.com');
CREATE TABLE Products (
Product_id INT(150) not null,
Product_name VARCHAR(100) not null,
Product_price DECIMAL(6,2) not null,
Deduction_price INT(50) not null,
Deadline DATE not null,
PRIMARY KEY (Product_id)
);
insert into Products values( 509687, 'Plain Flower', 59.46, 10, '2013-05-30');
insert into Products values( 485901, 'Cow Milk', 17.85, 12, '2013-04-18');
insert into Products values( 908571, 'Kærgården', 131.75, 8, '2013-04-07');
insert into Products values( 432612, 'Chicken Eggs', 27.57, 9, '2013-03-31');
insert into Products values( 853235, 'Pepsi Cherry', 25.11, 12, '2013-03-25');
insert into Products values( 357342, 'Spaghetti', 10.8, 5, '2013-03-20');
insert into Products values( 123563, 'Spaghetti sos', 22.4, 10, '2013-03-17');
delimiter //
CREATE TRIGGER first_trigger BEFORE UPDATE ON Products
FOR EACH ROW
BEGIN
IF NEW.Product_price < 20 THEN
SET NEW.Product_price = 15;
ELSEIF NEW.Product_price > 140 THEN
SET NEW.Product_price = 80;
END IF;
END;//
delimiter ;
UPDATE Products set Product_price = 25 where Product_id = 509687;
UPDATE Products set Product_price = 22 where Product_id = 908571;
CREATE TABLE Recipes (
Recipes_id INT (10) not null,
Recipe_name CHAR (150) not null,
Recipe_descrip TEXT(255) not null,
primary key (Recipes_id)
);
insert into Recipes values( 513678, 'Pancakes', 'Crack the eggs into a blender, add the flour, milk and a pinch of sea salt, and blitz until smooth.');
insert into Recipes values( 867523, 'Spaghetti Bolognese', 'Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat.');
CREATE TABLE Shopping_list (
Shopping_list_id INT (50) not null,
customer_ID INT (50) not null,
Items_quantity INT (50) not null,
primary key (Shopping_list_id),
foreign key (customer_ID)
REFERENCES
Customer_sign_in (customer_ID)
);
insert into Shopping_list values( 14081869, 19867889, 15);
insert into Shopping_list values( 15163266, 11958178, 30);
insert into Shopping_list values( 34232325, 52562523, 3);
insert into Shopping_list values( 82412845, 42515323, 7);
insert into Shopping_list values( 36272236, 69723642, 5);
CREATE TABLE Payment (
Payment_id INT(150) NOT NULL,
customer_ID INT(150) NOT NULL,
Date_pay DATETIME NOT NULL,
Shopping_list_id INT(50) NOT NULL,
PRIMARY KEY (Payment_id),
FOREIGN KEY (customer_ID)
REFERENCES Customer_sign_in (customer_ID),
FOREIGN KEY (Shopping_list_id)
REFERENCES Shopping_list (Shopping_list_id)
);
insert into Payment values( 84163801, 19867889, '2013-05-25 07:12:00', 14081869);
insert into Payment values( 89171750, 11958178, '2013-04-08 21:43:12', 15163266);
insert into Payment values( 19829247, 52562523, '2013-04-02 16:41:09', 34232325);
insert into Payment values( 35819204, 42515323, '2013-03-25 23:54:32', 82412845);
insert into Payment values( 25085739, 69723642, '2013-03-20 12:38:56', 36272236);
CREATE TABLE Has (
Product_id INT(150) NOT NULL,
Recipes_id INT(150) NOT NULL,
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
FOREIGN KEY (Recipes_id)
REFERENCES Recipes (Recipes_id)
);
insert into Has values( 509687, 513678);
insert into Has values( 485901, 513678);
insert into Has values( 908571, 513678);
insert into Has values( 357342, 867523);
insert into Has values( 123563, 867523);
CREATE TABLE Goes (
Product_id INT(150) NOT NULL,
Shopping_list_id INT(150) NOT NULL,
FOREIGN KEY (Product_id)
REFERENCES Products (Product_id),
FOREIGN KEY (Shopping_list_id)
REFERENCES Shopping_list (Shopping_list_id)
);
insert into Goes values( 485901, 14081869);
insert into Goes values( 123563, 14081869);
insert into Goes values( 908571, 14081869);
insert into Goes values( 485901, 82412845);
insert into Goes values( 123563, 34232325);
insert into Goes values( 357342, 34232325);
select * from Shopping_list;
select * from Payment;
select * from Customer_sign_in;
select * from Recipes;
select * from Products;
select * from Has;
select * from Goes;
SELECT c.customer_ID, p.Product_id, p.Product_price, s.Items_quantity,
(p.Product_price * s.Items_quantity) AS total FROM Products p LEFT JOIN Shopping_list s ON p.Product_id = s.Product_id LEFT JOIN Customer_sign_in c ON c.customer_ID = s.customer_ID;

Query from many-to-many relationship

Student stores a list of student name and Friend stores relationship between students.
Create table Student (
id int NOT NULL AUTO_INCREMENT,
name varchar(35),
PRIMARY KEY (id)
);
insert into Student (name) values ('John');
insert into Student (name) values ('Kelly');
insert into Student (name) values ('Mary');
Create table Friend (
id_from int NOT NULL REFERENCES Student(id),
id_to int NOT NULL REFERENCES Student(id),
PRIMARY KEY (id_from, id_to)
);
insert into Friend (id_from,id_to) values (1, 3);
insert into Friend (id_from,id_to) values (1, 2);
insert into Friend (id_from,id_to) values (3, 2);
How can I query all friends of "John", for example, in MySql? The schema is here.
http://sqlfiddle.com/#!9/aeacd/1
I have created a query. In general you can join tables with itself using the relation table. What the query does is join Student with Friend with Student and then selects the entries with "John" as name in the joined table between Student.id and Friend.id_from.
The query looks like this:
SELECT *
FROM Student as s1
INNER JOIN Friend as f1 on s1.id = f1.id_from
INNER JOIN Student as s2 on s2.id = f1.id_to
WHERE s1.name = "John";
you can try it out here:
http://sqlfiddle.com/#!9/aeacd/15

MySQL doesn't seem to work correct

I have this college database where i have to Retrieve names of instructors teaching Computer Science courses, the sections (course number, section number, year, semester) they are teaching, and the total number of students in the sections
So far this is what have managed but the output is not correct.
SELECT i.instructor_id,o.year,o.semester,o.section_number, o.number as CourseNumber,o.Total_num_student
FROM Instructor i JOIN
(SELECT t.instructor_id,t.year,t.semester,t.section_number,o1.number,o1.Total_num_student
FROM teaches t JOIN
(SELECT COUNT(s.student_id) as Total_num_student,e.section_number,e.number
FROM Student s JOIN
enrolls e
ON s.student_id=e.student_id
GROUP BY section_number,e.number) as o1
ON o1.section_number=t.section_number) as o
on i.instructor_id=o.instructor_id
WHERE department='Computer Science';
Can you see what is wrong with the output.
create table Course (
number int,
title varchar(255),
credits int,
syllabus varchar(255),
PRIMARY KEY (number)
);
INSERT INTO Course VALUES (620,'Algorithm',3,'XYZ');
INSERT INTO Course VALUES (621,'Algorithm',3,'XYZ');
INSERT INTO Course VALUES (232,'Java',2,'ABC');
INSERT INTO Course VALUES (420,'Cpp',2,'PQRS');
INSERT INTO Course VALUES (720,'Big Data',3,'NVGY');
INSERT INTO Course VALUES (120,'Intelligent System',4,'KJHU');
INSERT INTO Course VALUES (220,'Operating System',3,'GED');
INSERT INTO Course VALUES (480,'Graphics',4,'RSFN');
INSERT INTO Course VALUES (520,'Distributed Networks',3,'NHU');
INSERT INTO Course VALUES (820,'Data Mining',3,'TYU');
INSERT INTO Course VALUES (700,'Cryptography',1,'MNO');
create table Student (
student_id int,
name varchar(255),
department varchar(255),
PRIMARY KEY (student_id)
);
INSERT INTO Student VALUES (2345,'Mike','Computer Science');
INSERT INTO Student VALUES (346,'Rob','Computer Science');
INSERT INTO Student VALUES (789,'Nick','Game Design');
INSERT INTO Student VALUES (675,'Sara','Computer Science');
INSERT INTO Student VALUES (123,'Raj','Chemical');
INSERT INTO Student VALUES (5331,'Jack','Biotech');
INSERT INTO Student VALUES (1023,'Michelle','Chemical');
INSERT INTO Student VALUES (9800,'Jie','Game Design');
INSERT INTO Student VALUES (7834,'Dan','Petroleum');
INSERT INTO Student VALUES (4567,'Patrick','Computer Science');
create table Instructor (
instructor_id int,
name varchar(255),
department varchar(255),
title varchar(255),
PRIMARY KEY (instructor_id)
);
INSERT INTO Instructor VALUES (12,'John','Computer Science','Java');
INSERT INTO Instructor VALUES (23,'Bischof','Mechanical','Drawing');
INSERT INTO Instructor VALUES (56,'Kwon','Biotech','Biology');
INSERT INTO Instructor VALUES (78,'Deever','Security','Cryptography');
INSERT INTO Instructor VALUES (45,'Jin','Computer Science','Cpp');
INSERT INTO Instructor VALUES (67,'Bailey','Petroleum','Metalurgy');
INSERT INTO Instructor VALUES (90,'Richard','Industrial','Manufacture');
INSERT INTO Instructor VALUES (11,'Joe','Chemical','Chemistry');
INSERT INTO Instructor VALUES (24,'Roger','Game Design','Cpp');
INSERT INTO Instructor VALUES (55,'Zack','Computer Science','Design');
create table CourseOffering (
number int,
year int,
semester int,
section_number int,
classroom int,
PRIMARY KEY (number,year,semester,section_number),
FOREIGN KEY (number) REFERENCES Course (number)
);
INSERT INTO CourseOffering VALUES (620,2012,2,102,3540);
INSERT INTO CourseOffering VALUES (621,2013,3,103,3545);
INSERT INTO CourseOffering VALUES (232,2011,1,103,3455);
INSERT INTO CourseOffering VALUES (420,2013,3,101,2650);
INSERT INTO CourseOffering VALUES (720,2013,3,100,3455);
INSERT INTO CourseOffering VALUES (120,2014,1,102,3000);
INSERT INTO CourseOffering VALUES (220,2008,2,104,1450);
INSERT INTO CourseOffering VALUES (480,2009,4,106,1400);
INSERT INTO CourseOffering VALUES (520,2010,3,102,3500);
INSERT INTO CourseOffering VALUES (820,2008,3,100,2700);
INSERT INTO CourseOffering VALUES (700,2012,1,103,2540);
create table CourseOffering_Timing (
number int,
year int,
semester int,
section_number int,
timing int,
PRIMARY KEY (number,year,semester,section_number,timing),
FOREIGN KEY (number) REFERENCES Course (number)
);
INSERT INTO CourseOffering_Timing VALUES (620,2012,2,102,9);
INSERT INTO CourseOffering_Timing VALUES (621,2013,3,103,9);
INSERT INTO CourseOffering_Timing VALUES (232,2011,1,103,3);
INSERT INTO CourseOffering_Timing VALUES (420,2013,3,101,2);
INSERT INTO CourseOffering_Timing VALUES (720,2013,3,100,5);
INSERT INTO CourseOffering_Timing VALUES (120,2014,1,102,4);
INSERT INTO CourseOffering_Timing VALUES (220,2008,2,104,10);
INSERT INTO CourseOffering_Timing VALUES (480,2009,4,106,12);
INSERT INTO CourseOffering_Timing VALUES (520,2010,3,102,4);
INSERT INTO CourseOffering_Timing VALUES (820,2008,3,100,2);
INSERT INTO CourseOffering_Timing VALUES (700,2012,1,103,3);
create table PreRequisite (
number int,
prerequisite_number int,
PRIMARY KEY (number,prerequisite_number),
FOREIGN KEY (number) REFERENCES Course(number)
);
INSERT INTO PreRequisite VALUES (620,480);
INSERT INTO PreRequisite VALUES (621,481);
INSERT INTO PreRequisite VALUES (232,120);
INSERT INTO PreRequisite VALUES (420,320);
INSERT INTO PreRequisite VALUES (720,580);
INSERT INTO PreRequisite VALUES (120,110);
INSERT INTO PreRequisite VALUES (220,130);
INSERT INTO PreRequisite VALUES (480,380);
INSERT INTO PreRequisite VALUES (520,360);
INSERT INTO PreRequisite VALUES (820,700);
INSERT INTO PreRequisite VALUES (700,610);
create table enrolls (
student_id int,
number int,
year int,
semester int,
section_number int,
grade varchar(10),
PRIMARY KEY (student_id,number,year,semester,section_number),
FOREIGN KEY (number,year,semester,section_number) REFERENCES CourseOffering(number,year,semester,section_number),
FOREIGN KEY (student_id) REFERENCES Student (student_id)
);
INSERT INTO enrolls VALUES (2345,620,2012,2,102,'A');
INSERT INTO enrolls VALUES (346,232,2011,1,103,'B');
INSERT INTO enrolls VALUES (789,420,2013,3,101,'A');
INSERT INTO enrolls VALUES (675,621,2013,3,103,'C');
INSERT INTO enrolls VALUES (123,120,2014,1,102,'B');
INSERT INTO enrolls VALUES (5331,220,2008,2,104,'A');
INSERT INTO enrolls VALUES (1023,480,2009,4,106,'B');
INSERT INTO enrolls VALUES (9800,520,2010,3,102,'A');
INSERT INTO enrolls VALUES (7834,820,2008,3,100,'C');
INSERT INTO enrolls VALUES (4567,700,2012,1,103,'F');
create table teaches (
instructor_id int,
number int,
year int,
semester int,
section_number int,
PRIMARY KEY (instructor_id, number, year, semester, section_number),
FOREIGN KEY (instructor_id) REFERENCES Instructor(instructor_id),
FOREIGN KEY ( number, year, semester, section_number) REFERENCES CourseOffering( number, year, semester, section_number)
);
INSERT INTO teaches VALUES (12,620,2012,2,102);
INSERT INTO teaches VALUES (12,621,2013,3,103);
INSERT INTO teaches VALUES (23,232,2011,1,103);
INSERT INTO teaches VALUES (56,420,2013,3,101);
INSERT INTO teaches VALUES (78,720,2013,3,100);
INSERT INTO teaches VALUES (45,120,2014,1,102);
INSERT INTO teaches VALUES (67,220,2008,2,104);
INSERT INTO teaches VALUES (90,480,2009,4,106);
INSERT INTO teaches VALUES (11,520,2010,3,102);
INSERT INTO teaches VALUES (24,820,2008,3,100);
INSERT INTO teaches VALUES (55,700,2012,1,103);
I'm not completely sure what you are trying to achieve but this is if you like to retain the section info:
SELECT a.instructor_id, group_concat(DISTINCT a.year SEPARATOR ', ') YEAR, group_concat(DISTINCT a.semester SEPARATOR ', ') SEMESTER, group_concat(DISTINCT a.section_number SEPARATOR ', ') SECTION_NUMBER, group_concat(DISTINCT a.coursenumber SEPARATOR ', ') COURSENUMBER, count(a.student_id) as Total_num_student FROM (
SELECT e.student_id, i.instructor_id, t.year,t.semester,t.section_number, t.number as CourseNumber
FROM instructor i
JOIN teaches t
ON i.instructor_id = t.instructor_id
JOIN enrolls e
ON e.section_number=t.section_number
WHERE department='Computer Science') a
GROUP BY a.instructor_id
;
SELECT a.instructor_id, a.year, a.semester, a.section_number, a.coursenumber, count(a.student_id) as Total_num_student FROM (
SELECT e.student_id, i.instructor_id, t.year,t.semester,t.section_number, t.number as CourseNumber
FROM instructor i
JOIN teaches t
ON i.instructor_id = t.instructor_id
JOIN enrolls e
ON e.section_number=t.section_number
WHERE department='Computer Science') a
GROUP BY a.instructor_id, a.section_number
;
SQLFiddle demo
See if this will work for you
SELECT Instructor.name AS instructor_name, teaches.section_number AS section_number,
teaches.year AS year, teaches.semester AS semester, COUNT(enrolls.student_id) FROM Instructor
INNER JOIN teaches ON teaches.instructor_id = Instructor.instructor_id
INNER JOIN enrolls ON teaches.section_number = enrolls.section_number AND teaches.semester = enrolls.semester AND teaches.year = enrolls.year
WHERE department='Computer Science'
GROUP BY Instructor.name, teaches.section_number, teaches.year, teaches.semester;