I have 2 tables: employee and employeedetails.
employee looks like this:
id name
----------------
1 Suresh
2 Ram
3 Ravi
employeedetails looks like this:
empid salary
----------------
1 10000
2 5000
3 40000
I want to update salary field of a particular person. For this i know the employee name of the first table. Based on this, I need to write a single query that will update the salary field with single query. How can I do this?
For example, if I have to update Ravi's salary details, how can I do it in a single query?
update employeedetails
inner join employee on employeedetails.empid = employee.id
set salary = 1000
where employee.name = 'Ram'
Try this :
update employeedetails set salary=1000000 where empid in (select id from employee where name='suresh');
MERGE INTO employeedetails
USING employee
ON employeedetails.empid = employee.id
AND employee.name = 'Ravi'
WHEN MATCHED THEN
UPDATE
SET salary = 10000;
UPDATE employeedetails det
SET salary=100
WHERE EXISTS ( SELECT NULL
FROM employee emp
WHERE name='Ravi'
AND det.empid = emp.id
)
;
Related
I have 2 tables as following:
Tam trying to get the department name of each employee (DepName column in table Emp table) from Dep table:
I have written this query:
update Emp
set DepName= (
select DepName
from Dep
where array_to_string(EmpID, ',') like EmpID
);
It did not update the table Emp with the requested information, although I haven't got any error. Any help?
You can do:
update emp
set dept = d.depname
from dep
where emp.empid = any (dep.empid);
Having pointed that out, you should not do this. Instead, I would suggest that you have a proper link to the department table and use join to bring in the department name.
you have to convert id int to character varying array data type and then use contains operator with table dept and update as usual
UPDATE emp t1
SET dept = dname
from dept t2
where t2.eid #> concat(concat('{',(t1.id::text)),'}') ::varchar[]
https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e5c24b26b3479b68adf0b17c2050f715
I have some records in emp1 :
SELECT distinct
substrb(emp.employee_NAME,1,50) employee_NAME
FROM employee emp , employee_sites sites , (SELECT DISTINCT employee_id ,
emp_site_number
FROM abc
) abc
where emp.employee_id = sites.employee_id
and abc.employee_id=emp.employee_id
and abc.emp_site_number = sites.emp_site_number ;
and some records in emp:
SELECT distinct emp.employee_NAME employee_NAME
FROM employee emp
WHERE 1=1 and EXISTS
(SELECT 1 FROM employee_ACCOUNTS acc WHERE acc.employee_id = emp.employee_id
)
rowcount of emp : 205001
rowcount of emp1 : 18003
I want to find out if emp has all the records of emp1 ,in other words if emp is superset of emp1. I tried this :
select count(*) from (SELECT distinct emp.employee_NAME employee_NAME
FROM employee emp
WHERE 1=1 and EXISTS
(SELECT 1 FROM employee_ACCOUNTS acc WHERE acc.employee_id = emp.employee_id
) ) emp ,
(SELECT distinct
substrb(emp.employee_NAME,1,50) employee_NAME
FROM employee emp , employee_sites sites , (SELECT DISTINCT employee_id ,
emp_site_number
FROM abc
) abc
where emp.employee_id = sites.employee_id
and abc.employee_id=emp.employee_id
and abc.emp_site_number = sites.emp_site_number) emp1
where emp.employee_NAME = emp1.employee_NAME ;
Rowcount for the above query : 12360.
So I have concluded that emp is not a superset of emp1
Someone please let me know what I have done is fine or it needs some modification.
Also please share if you know some better way of doing it .
Thanks
You could avoid the correlated subqueries and just do a simple set MINUS operation:
select employee_name -- or whatever makes the employee the same in 2 tables
from emp1 -- the table which may have rows not in the other table
MINUS
select employee_name
from emp2 -- the table which you think may be missing some rows
You could also use a left join:
select emp2.employee_name from emp2
left join emp1 on emp2.employee_name = emp1.employee_name
where emp1.employee_name is null
The performance will depend on factors like indexes, data volumes. Inspection of the query plans and benchmarking will give you a good idea of which is the better option.
I have two tables Employees
EmpID | EmpName | EmpDob
and WareHouseEmployeers
WarehouseEmpID | position | province
I need to update the Employee table according to the WareHouseEmployers table's values. How do I update the details about the employee table according to warehouse province and position?
I have tried this but it's not working:
UPDATE Employee
SET a.EmpName = 'Steven', a.EmpDob = '5-5-1990'
FROM Employee a, WareHouseEmployee b,
WHERE
a.EmpID = b.WareHouseEmpID
AND position = 'manager', province = 'central'
Can someone please help me to do that in SQL Server?
Please use this script
UPDATE a
SET a.EmpName = 'Steven' , a.EmpDob='5-5-1990'
FROM Employee a
INNER JOIN WareHouseEmployee b ON a.EmpID = b.WareHouseEmpID
AND position = 'manager'
AND province = 'central'
I'm using Oracle 10g.
If i have the following duplicate rows (Same Employee with two Employee numbers):
Employee_No Employee_Name ID_NO
----------------------------------------------
0002345 John Debb 100345642
0030988 John Debb 100345642
----------------------------------------------
i want to get the result as:
Employee_No_1 Employee_No_2 Employee Name ID_NO
----------------------------------------------------------------
0002345 0030988 John Debb 100345642
----------------------------------------------------------------
Is it possible to be done in SQL? or it needs PL/SQL? and what would the query be?
SELECT MIN(Employee_no), MAX(employee_no), Employee_name, id_no
FROM Employee
GROUP BY Employee_name, id_no
HAVING MIN(employee_no) <> MAX(employee_no)
I don't do Oracle, but I think this is pretty generic syntax that should work.
Not quite in the format requested, but this will handle the case where there could be more than just 2 duplicates.
SELECT e.Employee_No, e.Employee_Name, e.ID_NO
FROM (SELECT Employee_Name, ID_NO
FROM Employee
GROUP BY Employee_Name, ID_NO
HAVING COUNT(*) > 1) q
INNER JOIN Employee e
ON q.Employee_Name = e.Employee_Name
AND q.ID_NO = e.ID_NO
ORDER BY e.Employee_Name, e.ID_NO, e.Employee_No
Query is as below,
select e1.employee_no, e2.employee_no, e1.employee_name, e1.id_no
from employee e1
join employee e2
on e1.id_no = e2.id_no
where e1.employee_no < e2.employee_no
I have a table employee that has employee’s benefit data.
I have a field in the table called isenrolled if field is 1 that means employee has enrolled for benefit and if field is 0 that means not enrolled.
My problem is i have multiple recs of a employee, that means Scott has two entries with isenrolled =1 and isenrolled =0.
I want to select only one rec of SCOTT where his isenrolled =1 and reject the one where isenrolled =0, that way i will get only unique recs for employees who has enrolled and who has not enrolled. How do i select those employees? I tried the qry below and it doesn't work
select * FROM employee e
WHERE e.empid not IN( SELECT empid FROM employee e2
WHERE e2.isenrolled =1)
First I set up some test data using:
create table t4
as select * from scott.emp
alter table t4 add (isenrolled number(10))
update t4
set isenrolled = 0
insert into t4
(select emp.*, 1
from scott.emp)
At this point the t4 table has now two records for each employee (one where isenrolled = 0 and one where isenrolled = 1)
so I change ALLEN's data so he has "opted" out
delete from t4
where ename = 'ALLEN'
and isenrolled = 1
This query then shows the data for ALLEN (1 record) and SMITH (2 records)
select *
from t4
where ename IN ('ALLEN', 'SMITH')
order by ename
Then to show just one record per employee (restricted to ALLEN and SMITH in this case) you could use:
select t.*
from t4 t
where isenrolled = (select MAX(isenrolled)
from t4
where t4.empno = t.empno)
and ename IN ('ALLEN', 'SMITH')
Hope this helps
select unique(e.empid) from employee e where e2.isenrolled =1
this will give you unique list of employees who are enrolled. Is that what you want to get?