Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
create table employee
	(emp_id 	integer	 	not null,
	 manager_id 	integer		not null,	
	 emp_name 	char(20)	not null,
	 emp_tel 	char(10),
  	 emp_salary	number		not null,
	 hire_date	date,
	constraint pk_employee primary key(emp_id)
	);


create table customer
	(cust_id	integer 	not null,
	cust_name 	char(20)	not null ,
	cust_address 	varchar2(200)	,
	emp_id 		integer	 	not null,
    	constraint pk_customer primary key(cust_id)
	);


alter table customer 
add constraint fk_employee_customer foreign key(emp_id) 
references employee(emp_id);


alter table employee 
add constraint fk_employee_manager foreign key(manager_id) 
references employee(emp_id);


The salary of employee dealing with customer having id=4 has been doubled


What I have tried:

update employee
set emp_salary=emp_salary*2
from employee e
join customer c
on c.emp_id=e.emp_id
where e.emp_id =(4)


I tried with join but it did not work, any ideas how to solve this query
Posted
Updated 24-Feb-20 3:42am
v2
Comments
ZurdoDev 24-Feb-20 9:45am    
That sql looks right. What do you mean it does not work?

Although it looks right syntactically, I doubt you really want to do it. Why would an employees salary have anything to do with a customer's table?
Abed Al Rahman Hussien Balhawan 24-Feb-20 9:53am    
customer table and employee table are connected by a one to many relationship
One Employee can have Many Customers, as an example of a banking system.
the code is not working, and i am sure i need a join between the two tables.
ZurdoDev 24-Feb-20 11:02am    
You have a join.

1 solution

Try
SQL
update tab employee
set employee.emp_salary = employee.emp_salary * 2
where exists (select 1 from tab customer
where customer.emp_id = employee.emp_id
and customer.cust_id = 4)
or
SQL
update tab employee
set employee.emp_salary = employee.emp_salary * 2
from tab employee, tab customer
where customer.emp_id = employee.emp_id
and customer.cust_id = 4
or
SQL
update (select emp_salary
from employee e 
inner join customer c on c.emp_id = e.emp_id 
where c.cust_id = 4) t
set t.emp_salary = t.emp_salary * 2
Oracle Update with Join[^]
 
Share this answer
 
v3
Comments
Abed Al Rahman Hussien Balhawan 24-Feb-20 9:33am    
an error message is showing: ORA-00971: missing SET keyword
phil.o 24-Feb-20 9:35am    
Why did you tag MySQL then? Oracle is not MySQL, and there are major differences in their syntaxes.
Abed Al Rahman Hussien Balhawan 24-Feb-20 9:41am    
yes i did a mistake by tagging mysql.
phil.o 24-Feb-20 9:51am    
I updated my answer with oracle version.
Abed Al Rahman Hussien Balhawan 24-Feb-20 10:02am    
ORA-00904: "EMP_SALARY": invalid identifier

2nd Method

ORA-00933: SQL command not properly ended

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900