How to count repeated project_no in mysql - mysql

imagine i have 3 table employee detail, project details and Works on(where it's set that which employee will work on which project).
The question is To show the average salary of all the employees work on those project which are having more than three employees.
create table Employee
(
empid varchar(5) primary key,
ename varchar(20),
address varchar(50),
phone bigint(10),
job varchar(10),
salary varchar(10)
);
insert into Employee values ('e101','Ankita','Delhi',9987654321,'Manager','12000');
insert into Employee values ('e102','Rahul','Banglore',9219849816,'Developer','80000');
insert into Employee values ('e103','Aditya','Noida',9517536842,'Chef','16000');
insert into Employee values ('e104','Rachna','Mumbai',7539561682,'Teacher','45000');
insert into Employee values ('e105','Karan','Delhi',8529517463,'Driver','18000');
create table Project
(
project_no varchar(5) primary key,
pname varchar(20),
no_of_hours int(3)
);
Insert into Project values('p101', 'Data Handling', 5);
Insert into Project values('p102', 'Game Develop', 3);
Insert into Project values('p103', 'New Recipe', 6);
Insert into Project values('p104', 'Question Paper', 9);
Insert into Project values('p105', 'Ride to Shimla', 12);
create table Works_on
(
empid varchar(5),
project_no varchar(5),
start_date date,
end_date date,
manager_name varchar(20),
primary key(empid, project_no),
foreign key(empid) references Employee(empid),
foreign key(project_no) references Project(project_no)
);
Insert into Works_on value('e101', 'p101', '2021-02-24', '2021-03-25', 'Anshika');
Insert into Works_on value('e102', 'p101', '2021-03-4', '2021-04-2', 'Anshika');
Insert into Works_on value('e103', 'p102', '2021-02-4', '2021-03-5', 'Roshan');
Insert into Works_on value('e104', 'p101', '2021-05-20', '2021-05-29', 'Anshika');
Insert into Works_on value('e105', 'p103', '2021-02-4', '2021-02-5', 'Dharmesh');
Insert into Works_on value('e103', 'p101', '2021-02-4', '2021-02-5', 'Anshika');
Insert into Works_on value('e105', 'p104', '2021-02-4', '2021-02-5', 'Ritika');
Insert into Works_on value('e105', 'p105', '2021-02-4', '2021-02-5', 'Sid');

select w.project_no project_number,
count(w.project_no) number_of_employees,
avg(e.salary) average_salary
from
Employee e, Project p, Works_on w
where
w.project_no= p.project_no
and w.empid=e.empid
group by w.project_no
having(count(w.project_no))>3;
DBFiddle

Related

My SQL Auto_increment not working for a fixed column

I am trying to create three tables using MySQL Workbench, where two columns need to be auto incremented with a fixed starting value. I have checked some online resources and figured it out what statements to use.
create database test;
use test;
/*table Project */
create table Project(
Pnumber INT NOT NULL AUTO_INCREMENT,
Pname varchar(30) NOT NULL,
Plocation enum ('QLD', 'VIC', 'NSW', 'SA') NOT NULL,
primary key(Pnumber)
);
ALTER TABLE Project AUTO_INCREMENT = 7777770;
/*table Department*/
Create table Department(
Dcode varchar(5) NOT NULL,
Dname varchar(30),
Dmg_ssn varchar(30),
primary key(Dcode)
);
/* Table Employee*/
create table Employee(
Ssn INT NOT NULL AUTO_INCREMENT,
Ename varchar(30) NOT NULL,
Bdate DATE,
Address varchar(30),
Dcode varchar(5) NOT NULL,
Driver_License varchar(30),
primary key(Ssn),
foreign key(Dcode) references Department(Dcode)
);
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
/* Insert into Project*/
Insert into Project values ('7777770','Star', 'QLD');
Insert into Project values ('7777771','Innova', 'NSW');
Insert into Project values ('7777772','Andra', "QLD");
/* Insert into Department */
insert into Department values ('ABC12', 'Finances', 'RA12');
insert into Department values ('WXY10', 'Human Resources', 'RA12');
insert into Department values ('PBC32', 'S2', 'RB13');
/*Insert into Employee */
insert into Employee values ('0000001','Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee values ('0000002','Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee values ('0000003','Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
However, after I created the tables, only the column Pnumber in table Project follows the required format, while the column Ssn in Employee does not.
This is a SELECT over the Employee table:
Ssn,Ename,Bdate,Address,Dcode,Driver_License
1,"Vladimir Rostov",2008-07-04,"19 Wilson St",ABC12,1023456
2,"Rory Reid",2002-02-10,"10 Mary St",WXY10,2365947
3,"Andy Murray",2001-05-11,"1280 Albert St",WXY10,5891655
Any idea what I am doing wrong??
Your sql script has an employee table, not a student, I assume these two names refer to the same table. I will use the employee name.
So, you set the start value auto increment of the employee table to 1000000:
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
But then you explicitly insert 1, 2, and 3 into this column with your insert statements because '0000001' translates into 1. If you explicitly insert a value into auto increment and it is higher than the maximum value in the given field, then mysql will insert that value as is into the auto increment field.
If you are using auto increment, then you should let it work and do not specify an explicit value:
/*Insert into Employee */
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');

MySQL stored procedure to insert or update table

I have two tables:
CREATE TABLE EMPLOYEE (
EMPLOYEE_ID bigint NOT NULL AUTO_INCREMENT,
EMPLOYEE_NO VARCHAR(30),
EMPLOYEE_TYPE VARCHAR(20),
PRIMARY KEY(EMPLOYEE_ID)
);
CREATE TABLE EMP_DETAIL(
EMPLOYEE_ID BIGINT NOT NULL ,
INS_ID VARCHAR(20) ,
SALARY int,
PRIMARY KEY(EMPLOYEE_ID ,INS_ID),
FOREIGN KEY(EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID)
);
I want to insert a value in the emp table and the emp_detail table. If the employee already exists, then only insert a record in the emp_details table.
This is the stored procedure:
CREATE PROCEDURE INSERTEMP(
IN EMPLOYEE_NO VARCHAR(30),
IN EMPLOYEE_TYPE VARCHAR(20),
IN INS_ID VARCHAR(20),
IN SALARY INT
)
BEGIN
DECLARE EMPLOYEE_ID BIGINT;
SET #EMPLOYEE_ID= (SELECT IFNULL((SELECT EMPLOYEE_ID FROM EMPLOYEE WHERE EMPLOYEE_NO=EMPLOYEE_NO AND EMPLOYEE_TYPE=EMPLOYEE_TYPE) ,'0'));
IF #EMPLOYEE_ID=0 THEN
INSERT INTO EMPLOYEE (EMPLOYEE_NO ,EMPLOYEE_TYPE ) VALUES (EMPLOYEE_NO,EMPLOYEE_TYPE);
SET #EMPLOYEE_ID=(SELECT LAST_INSERT_ID());
END IF;
INSERT INTO EMP_DETAIL(EMPLOYEE_ID,INS_ID,SALARY) VALUES (#EMPLOYEE_ID,INS_ID,SALARY);
END
If I call the procedure:
CALL INSERTEMP(100, 'PERM','AAA',100);
first execute it created id as 1 in emp table if execute it in second time also insert with new id and third time it fails.
Can someone help?

updating date in mysql (phpMyAdmin)

i have created a table called vendor and imported the document with these commands:
DROP TABLE IF EXISTS vendor;
CREATE TABLE VENDOR (
V_CODE int,
V_NAME varchar(15),
V_CONTACT varchar(50),
V_AREACODE varchar(3),
V_PHONE varchar(8),
V_STATE varchar(2),
V_ORDER varchar(1)
);
INSERT INTO VENDOR VALUES('21225','Bryson, Inc.','Smithson','615','223-3234','TN','Y');
INSERT INTO VENDOR VALUES('21226','SuperLoo, Inc.','Flushing','904','215-8995','FL','N');
INSERT INTO VENDOR VALUES('21231','D&E Supply','Singh','615','228-3245','TN','Y');
INSERT INTO VENDOR VALUES('21344','Gomez Bros.','Ortega','615','889-2546','KY','N');
INSERT INTO VENDOR VALUES('22567','Dome Supply','Smith','901','678-1419','GA','N');
INSERT INTO VENDOR VALUES('23119','Randsets Ltd.','Anderson','901','678-3998','GA','Y');
INSERT INTO VENDOR VALUES('24004','Brackman Bros.','Browning','615','228-1410','TN','N');
INSERT INTO VENDOR VALUES('24288','ORDVA, Inc.','Hakford','615','898-1234','TN','Y');
INSERT INTO VENDOR VALUES('25443','B&K, Inc.','Smith','904','227-0093',null ,'N');
INSERT INTO VENDOR VALUES('25501','Damal Supplies','Smythe','615','890-3529','TN','N');
INSERT INTO VENDOR VALUES('25595','Rubicon Systems','Orton','904','456-0092','FL','Y');
DROP TABLE IF EXISTS product;
CREATE TABLE PRODUCT (
P_CODE varchar(10),
P_DESCRIPT varchar(35),
P_INDATE datetime,
P_QOH int,
P_MIN int,
P_PRICE float(8),
P_DISCOUNT float(8),
V_CODE int
);
INSERT INTO PRODUCT VALUES('11QER/31','Power painter, 15 psi., 3-nozzle','11/3/2007','8','5','109.98999786377','0','25595');
INSERT INTO PRODUCT VALUES('13-Q2/P2','7.25-in. pwr. saw blade','12/13/2007','32','15','14.9899997711182','0.05','21344');
INSERT INTO PRODUCT VALUES('14-Q1/L3','9.00-in. pwr. saw blade','11/13/2007','18','12','17.4899997711182','0','21344');
INSERT INTO PRODUCT VALUES('1546-QQ2','Hrd. cloth, 1/4-in., 2x50','1/15/2008','15','8','39.9500007629395','0','23119');
INSERT INTO PRODUCT VALUES('1558-QW1','Hrd. cloth, 1/2-in., 3x50','1/15/2008','23','5','43.9900016784668','0','23119');
INSERT INTO PRODUCT VALUES('2232/QTY','B&D jigsaw, 12-in. blade','12/30/2007','8','5','109.919998168945','0.05','24288');
INSERT INTO PRODUCT VALUES('2232/QWE','B&D jigsaw, 8-in. blade','12/24/2007','6','5','99.870002746582','0.05','24288');
INSERT INTO PRODUCT VALUES('2238/QPD','B&D cordless drill, 1/2-in.','1/20/2008','12','5','38.9500007629395','0.05','25595');
INSERT INTO PRODUCT VALUES('23109-HB','Claw hammer','1/20/2008','23','10','9.94999980926514','0.1','21225');
INSERT INTO PRODUCT VALUES('23114-AA','Sledge hammer, 12 lb.','1/2/2008','8','5','14.3999996185303','0.05','');
INSERT INTO PRODUCT VALUES('54778-2T','Rat-tail file, 1/8-in. fine','12/15/2007','43','20','4.98999977111816','0','21344');
INSERT INTO PRODUCT VALUES('89-WRE-Q','Hicut chain saw, 16 in.','2/7/2008','11','5','256.989990234375','0.05','24288');
INSERT INTO PRODUCT VALUES('PVC23DRT','PVC pipe, 3.5-in., 8-ft','2/20/2008','188','75','5.86999988555908','0','');
INSERT INTO PRODUCT VALUES('SM-18277','1.25-in. metal screw, 25','3/1/2008','172','75','6.98999977111816','0','21225');
INSERT INTO PRODUCT VALUES('SW-23116','2.5-in. wd. screw, 50','2/24/2008','237','100','8.44999980926514','0','21231');
INSERT INTO PRODUCT VALUES('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','1/17/2008','18','5','119.949996948242','0.1','25595');
DROP TABLE IF EXISTS CUSTOMER;
CREATE TABLE CUSTOMER (
CUS_CODE int,
CUS_LNAME varchar(15),
CUS_FNAME varchar(15),
CUS_INITIAL varchar(1),
CUS_AREACODE varchar(3),
CUS_PHONE varchar(8),
CUS_BALANCE float(8)
);
INSERT INTO CUSTOMER VALUES('10010','Ramas','Alfred','A','615','844-2573','0');
INSERT INTO CUSTOMER VALUES('10011','Dunne','Leona','K','713','894-1238','0');
INSERT INTO CUSTOMER VALUES('10012','Smith','Kathy','W','615','894-2285','345.859985351562');
INSERT INTO CUSTOMER VALUES('10013','Olowski','Paul','F','615','894-2180','536.75');
INSERT INTO CUSTOMER VALUES('10014','Orlando','Myron','','615','222-1672','0');
INSERT INTO CUSTOMER VALUES('10015','O''Brian','Amy','B','713','442-3381','0');
INSERT INTO CUSTOMER VALUES('10016','Brown','James','G','615','297-1228','221.190002441406');
INSERT INTO CUSTOMER VALUES('10017','Williams','George','','615','290-2556','768.929992675781');
INSERT INTO CUSTOMER VALUES('10018','Farriss','Anne','G','713','382-7185','216.550003051758');
INSERT INTO CUSTOMER VALUES('10019','Smith','Olette','K','615','297-3809','0');
DROP TABLE IF EXISTS invoice;
CREATE TABLE INVOICE (
INV_NUMBER int,
CUS_CODE int,
INV_DATE datetime,
INV_SUBTOTAL float(8),
INV_TAX float(8),
INV_TOTAL float(8)
);
INSERT INTO INVOICE VALUES('1001','10014','1/16/2008','24.8999996185303','1.99000000953674','26.8899993896484');
INSERT INTO INVOICE VALUES('1002','10011','1/16/2008','9.97999954223633','0.800000011920929','10.7799997329712');
INSERT INTO INVOICE VALUES('1003','10012','1/16/2008','153.850006103516','12.3100004196167','166.160003662109');
INSERT INTO INVOICE VALUES('1004','10011','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1005','10018','1/17/2008','70.4400024414062','5.6399998664856','76.0800018310547');
INSERT INTO INVOICE VALUES('1006','10014','1/17/2008','397.829986572266','31.8299999237061','429.660003662109');
INSERT INTO INVOICE VALUES('1007','10015','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1008','10011','1/17/2008','399.149993896484','31.9300003051758','431.079986572266');
DROP TABLE IF EXISTS line;
CREATE TABLE LINE (
INV_NUMBER int,
LINE_NUMBER int,
P_CODE varchar(10),
LINE_UNITS float(8),
LINE_PRICE float(8),
LINE_TOTAL float(8)
);
INSERT INTO LINE VALUES('1001','1','13-Q2/P2','1','14.9899997711182','14.9899997711182');
INSERT INTO LINE VALUES('1001','2','23109-HB','1','9.94999980926514','9.94999980926514');
INSERT INTO LINE VALUES('1002','1','54778-2T','2','4.98999977111816','9.97999954223633');
INSERT INTO LINE VALUES('1003','1','2238/QPD','1','38.9500007629395','38.9500007629395');
INSERT INTO LINE VALUES('1003','2','1546-QQ2','1','39.9500007629395','39.9500007629395');
INSERT INTO LINE VALUES('1003','3','13-Q2/P2','5','14.9899997711182','74.9499969482422');
INSERT INTO LINE VALUES('1004','1','54778-2T','3','4.98999977111816','14.9700002670288');
INSERT INTO LINE VALUES('1004','2','23109-HB','2','9.94999980926514','19.8999996185303');
INSERT INTO LINE VALUES('1005','1','PVC23DRT','12','5.86999988555908','70.4400024414062');
INSERT INTO LINE VALUES('1006','1','SM-18277','3','6.98999977111816','20.9699993133545');
INSERT INTO LINE VALUES('1006','2','2232/QTY','1','109.919998168945','109.919998168945');
INSERT INTO LINE VALUES('1006','3','23109-HB','1','9.94999980926514','9.94999980926514');
INSERT INTO LINE VALUES('1006','4','89-WRE-Q','1','256.989990234375','256.989990234375');
INSERT INTO LINE VALUES('1007','1','13-Q2/P2','2','14.9899997711182','29.9799995422363');
INSERT INTO LINE VALUES('1007','2','54778-2T','1','4.98999977111816','4.98999977111816');
INSERT INTO LINE VALUES('1008','1','PVC23DRT','5','5.86999988555908','29.3500003814697');
INSERT INTO LINE VALUES('1008','2','WR3/TT3','3','119.949996948242','359.850006103516');
INSERT INTO LINE VALUES('1008','3','23109-HB','1','9.94999980926514','9.94999980926514');
DROP TABLE IF EXISTS EMP;
CREATE TABLE EMP (
EMP_NUM int,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB datetime,
EMP_HIRE_DATE datetime,
EMP_AREACODE varchar(3),
EMP_PHONE varchar(8),
EMP_MGR int
);
INSERT INTO EMP VALUES('100','Mr.','Kolmycz','George','D','6/15/1942','3/15/1985','615','324-5456','');
INSERT INTO EMP VALUES('101','Ms.','Lewis','Rhonda','G','3/19/1965','4/25/1986','615','324-4472','100');
INSERT INTO EMP VALUES('102','Mr.','VanDam','Rhett','','11/14/1958','12/20/1990','901','675-8993','100');
INSERT INTO EMP VALUES('103','Ms.','Jones','Anne','M','10/16/1974','8/28/1994','615','898-3456','100');
INSERT INTO EMP VALUES('104','Mr.','Lange','John','P','11/8/1971','10/20/1994','901','504-4430','105');
INSERT INTO EMP VALUES('105','Mr.','Williams','Robert','D','3/14/1975','11/8/1998','615','890-3220','');
INSERT INTO EMP VALUES('106','Mrs.','Smith','Jeanine','K','2/12/1968','1/5/1989','615','324-7883','105');
INSERT INTO EMP VALUES('107','Mr.','Diante','Jorge','D','8/21/1974','7/2/1994','615','890-4567','105');
INSERT INTO EMP VALUES('108','Mr.','Wiesenbach','Paul','R','2/14/1966','11/18/1992','615','897-4358','');
INSERT INTO EMP VALUES('109','Mr.','Smith','George','K','6/18/1961','4/14/1989','901','504-3339','108');
INSERT INTO EMP VALUES('110','Mrs.','Genkazi','Leighla','W','5/19/1970','12/1/1990','901','569-0093','108');
INSERT INTO EMP VALUES('111','Mr.','Washington','Rupert','E','1/3/1966','6/21/1993','615','890-4925','105');
INSERT INTO EMP VALUES('112','Mr.','Johnson','Edward','E','5/14/1961','12/1/1983','615','898-4387','100');
INSERT INTO EMP VALUES('113','Ms.','Smythe','Melanie','P','9/15/1970','5/11/1999','615','324-9006','105');
INSERT INTO EMP VALUES('114','Ms.','Brandon','Marie','G','11/2/1956','11/15/1979','901','882-0845','108');
INSERT INTO EMP VALUES('115','Mrs.','Saranda','Hermine','R','7/25/1972','4/23/1993','615','324-5505','105');
INSERT INTO EMP VALUES('116','Mr.','Smith','George','A','11/8/1965','12/10/1988','615','890-2984','108');
but when I entered commands:
SELECT INV_DATE FROM invoice;
the data only shows 0000-00-00 00:00:00
how do i update the whole column in INV_DATE to what I wish (example 1/16/2008)?
INSERT INTO INVOICE VALUES('1001','10014','1/16/2008','24.8999996185303','1.99000000953674','26.8899993896484');
INSERT INTO INVOICE VALUES('1002','10011','1/16/2008','9.97999954223633','0.800000011920929','10.7799997329712');
INSERT INTO INVOICE VALUES('1003','10012','1/16/2008','153.850006103516','12.3100004196167','166.160003662109');
INSERT INTO INVOICE VALUES('1004','10011','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1005','10018','1/17/2008','70.4400024414062','5.6399998664856','76.0800018310547');
INSERT INTO INVOICE VALUES('1006','10014','1/17/2008','397.829986572266','31.8299999237061','429.660003662109');
INSERT INTO INVOICE VALUES('1007','10015','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1008','10011','1/17/2008','399.149993896484','31.9300003051758','431.079986572266');
Type of INV_DATE in invoice table is 'datetime'. The default format of datetime is 'YYYY-MM-DD HH:MM:SS'.
So, if you insert values in different format, it will misinterpret it and show unexpected output.
Update the type of the INV_DATE column to any other favorable one or insert date and time formatted in this format : YYYY-MM-DD HH:MM:SS[.fraction]
Hope this helps.

MySQL UPDATE value in SQLfiddle

I've just started on SQL and so far I've made this and it works
CREATE TABLE employees(employee_ID int NOT NULL, name varchar(20) NOT NULL UNIQUE,
PRIMARY KEY (employee_ID)
);
INSERT INTO employees VALUES(1, 'Adam Jones');
INSERT INTO employees VALUES(2, 'Amy Smith');
INSERT INTO employees VALUES(3, 'Anthony Wright');
CREATE TABLE department(department_ID varchar(20) NOT NULL,
department_name varchar(20) NOT NULL, head_of_dep varchar(20),
num_of_employees_in_dep int
);
INSERT INTO department VALUES('Bad At SQL Ltd', 'Need Help HQ', 'No One Yet', 3);
But I cant understand why this wont work
UPDATE department SET head_of_dep = name FROM employees WHERE employee_ID = 1
What am I doing wrong?
Using SQLfiddle and MySQL 5.5.32
You need to rewrite it to
UPDATE department SET head_of_dep = (SELECT name FROM employees WHERE employee_ID = 1)
because you actually have to SELECT the value

MySQL-Need to select a row from one table to another table WHERE a <# will generate in SELECT statement

My first table has department name and number of phd's. My second table has the professor's name in particular departments. I need to pull the professors that are in the departments with less than 50 phd's.
CREATE TABLE Dept (
dname VARCHAR(100),
numphds INT
);
INSERT INTO Dept VALUES ( '"Chemical Engineering"', 32 );
INSERT INTO Dept VALUES ( '"Civil Engineering"', 88 );
INSERT INTO Dept VALUES ( '"Computer Sciences"', 47 );
CREATE TABLE Prof (
pname VARCHAR(100),
dname VARCHAR(100)
);
INSERT INTO Prof VALUES ( '"Brian, C."', '"Chemical Engineering"' );
INSERT INTO Prof VALUES ( '"Brown, S."', '"Civil Engineering"' );
INSERT INTO Prof VALUES ( '"Jones, J."', '"Computer Sciences"' );
SELECT dname, numphds FROM dept WHERE numphds <50;
/using the following SELECT statement is where I am lost and cannot seem to understand how to get the professor name within my results of the dname and numphds that are <50. I know I won't get the results by using the FROM DEPT statement. Any ideas/
Personally, I would give Dept a primary key and index column deptId and have this also on the Prof table. That way, there's no need to join on a text field.
CREATE TABLE Dept (
deptId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
dname VARCHAR(100),
numphds INT
);
INSERT INTO Dept VALUES ( 1, 'Chemical Engineering', 32 );
INSERT INTO Dept VALUES ( 2, 'Civil Engineering', 88 );
INSERT INTO Dept VALUES ( 3, 'Computer Sciences', 47 );
CREATE TABLE Prof (
pname VARCHAR(100),
dname VARCHAR(100),
deptId INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
INSERT INTO Prof VALUES ( 'Brian, C.', 'Chemical Engineering', 1);
INSERT INTO Prof VALUES ( 'Brown, S.', 'Civil Engineering', 2);
INSERT INTO Prof VALUES ( 'Jones, J.', 'Computer Sciences', 3);
Second, you do realise that your text field have apostrophes?
To select them, using this format, I would join using the query
SELECT dept.dname, dept.numphds, prof.pname FROM dept join `prof` on prof.deptId = dept.deptId WHERE dept.numphds <50;
Here is your SQLFiddle demo.