SQL Delete using joins - oracle

I am trying to delete rows from both emp and dept tables using the below query.
But after the query executed, it is deleting rows from only dept table and not from emp.
delete (
select * from emp e,dept d
where e.empno=d.empno
and e.sal in ('200','1950','5000')
and d.objid in ('1001','1002','1003')
);
I am using oracle 8i.
Please help!!
Thanks in Advance

Related

How can I update in oracle apex interactive grid for given sql query only SAL_DATE column on emp table ( group by has applied on this sql query )

Interactive grid sql query :
select
deptno,
sum(SAL),
SAL_DATE
from
emp
group by
deptno,
SAL_DATE
I am also giving the emp table details

converting correlated oracle subquery to single query

Hi I have two tables EMP and Dept( data as normal emp and dept tables of oracle)
I want to find the number of employees earning more than the avg(sal) of their own dept without use of correlated subquery. I wrote the query as below
SELECT * from emp e JOIN
(SELECT avg(sal) avgsal,deptno
FROM emp group by deptno )avgsal_tab
on e.sal > avgsal_tab.avgsal where e.deptno =avgsal_tab.deptno
order by e.deptno
This gets me the output but how can I rewrite this with a single query without inline query as shown above.
You can use analytic window functions:
SELECT *
from (
SELECT
e.*
,avg(sal)over(partition by deptno) avgsal
from emp e
)
where avgsal>sal

Splitting MERGE statement in Update, Delete and Insert

I am trying to migrate one on SQL from Oracle to Hive. To make to work on hive , I have to break Merge statement into UPDATE/DELETE and INSERT.
Please help me on this. I have my version of splitting , but i am not sure on it.
Note: I am not using Hive MERGE for other reason.
Merge Statement:
MERGE INTO bonuses D
USING (SELECT employee_id, salary, department_id FROM employees
WHERE department_id = 80) S
ON (D.employee_id = S.employee_id)
WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01 WHERE (S.salary > 8000)
DELETE WHERE (S.salary >= 10000)
WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
VALUES (S.employee_id, S.salary*.01)
WHERE (S.salary <= 8000);
Table DDLs:
CREATE TABLE bonuses (employee_id NUMBER, bonus NUMBER DEFAULT 100);
CREATE TABLE employees ( employee_id NUMBER, salary NUMBER, department_id int);

Oracle 20 million update based on join

I have a need to do the following
UPDATE TABLE2 t2
SET t2.product_id = (select t1.product_id from
table1 t1 where t1.matching_id = t2.matching_id)
Except that TABLE2 has 27 million records. The product_id is a newly added column and hence populating data to it.
I could use a cursor , break down my record set in TABLE2 to a reasonably smaller number, But with 27 million records, I am not sure whats the best way.
Pl suggest, even if it means exporting my data to excel.
Update - THe matching columns are indexed too.
The only thing I could do different is replace the update for a CREATE TABLE AS
CREATE TABLE table2_new AS
SELECT t2.* (less product_id), t1.product_id
FROM table1 t1
JOIN table2 t2
ON t1.matching_id = t2.matching_id
But later you will have to add the CONSTRAINTS manually, delete table2 and replace for table2_new
update (select t1.product_id as old_product_id, t2.product_id as new_product_id
from table1 t1
join table2 t2 on (t1.matching_id = t2.matching_id)) t
set t.new_product_id = t.old_product_id

How to use maximum select queries in hive

I have two tables emp and dept in oracle. Have imported it to hive. The same structure is in hive. I need a query where I can select max no of empno coloumn in hive. Can I use ORDER BY EMPNO instead of select max(empno)?
This is the query for Oracle Database that I am using.
select a.empno,
a.ename,
a.hiredate,
a.mgr,
a.job,
a.sal,
a.comm,
a.deptno,
b.deptno,
b.dname,
b.loc
from emp2 a,
dept1 b
where a.deptno=b.deptno
and a.empno=(select max(empno) from emp2);
How can I select max empno in hive?
This should work:
SELECT a.empno,a.ename,a.hiredate,a.mgr,
a.job,a.sal,a.comm,a.deptno,b.dname,b.loc
FROM emp2 a, JOIN dept1 b
ON (a.deptno=b.deptno )
WHERE a.empno = max(b.empno)
GROUP BY a.empno,a.ename,a.hiredate,a.mgr,
a.job,a.sal,a.comm,a.deptno,b.dname,b.loc
;

Resources