how to find the number of employees who are supervised by John, who
is supervised by Danny using REFERENCES oracle sql. Any suggestions?
You can just use:
SELECT COUNT(*)
FROM employees e
WHERE e.supervisor.name = 'John'
AND e.supervisor.supervisor.name = 'Danny';
Assuming you have the type:
CREATE TYPE employee AS OBJECT(
name VARCHAR2(8),
supervisor REF employee
);
And then the object-derived table:
CREATE TABLE employees OF employee;
Related
I am trying to apply filtering logic and also searching from specific string in a column but unable to figure it out.
here is the sample script below:
CREATE TABLE [Test](
service_id int,
category_id int,
employee_name varchar(50)
)
insert into Test values (38756,201830,'kalyan');
insert into Test values (38366,201790,'kalyan');
insert into Test values (38756,201830,'kalyan');
insert into Test values (38366,201790,'kalyan');
CREATE TABLE [major](
spec_id int,
manager_name varchar(150)
)
insert into major values (38756,null);
insert into major values (38366,null);
insert into major values (38756,'Express Door for on going : DECLINE by kalyan');
insert into major values (38366,'Express door for on going : APPROVE by Joy kalyan')\
insert into major values (38366,'Request required');
Query I tried:
select distinct service_id,
category_id,
employee_name,
case when manager_name like 'Express Door%' then manager_name
when manager_name like 'Express door%' then manager_name end as manager
from Test
left join major on spec_id = service_id and manager_name is not null
My expected output should be like this ((get value after string (by) in the manager field)
service_id category_id employee_name manager
38366 201790 kalyan Joy kalyan
38756 201830 kalyan kalyan
Thanks in advance
Swetha J
How about this?
SQL> select distinct
2 t.service_id,
3 t.category_id,
4 t.employee_name,
5 substr(m.manager_name, instr(m.manager_name, 'by') + 3) manager
6 from test t join major m on m.spec_id = t.service_id
7 where instr(m.manager_name, 'by') > 0;
SERVICE_ID CATEGORY_ID EMPLOYEE_NAME MANAGER
---------- ----------- -------------------- --------------------
38366 201790 kalyan Joy kalyan
38756 201830 kalyan kalyan
SQL>
Create an anonymous PL/SQL block, which looks for an employee by last name based on a SQL parameter (e.g. &variable) that the user responds to with a valid last_name in the EMPLOYEES table (i.e. King, or Kochhar, or De Haan, or Hunold, or Ernst, etc….…). If the employee last_name exists in the EMPLOYEES table, then insert into the OUTPUT_LOG table, the following string:
'Employee is is Found'. Test your PL/SQL block by checking the content of the OUTPUT_LOG table. You should find the string the you inserted in the OUTPUT_LOG table.
employee table has these columns
EMPLOYEE_ID ,
FIRST_NAME ,
LAST_NAME ,
EMAIL ,
PHONE_NUMBER ,
HIRE_DATE ,
JOB_ID ,
SALARY ,
COMMISSION_PCT ,
MANAGER_ID ,
DEPARTMENT_ID
output_log table only has one column VARCHAR2(250) called data and id column as PRIMARY KEY
I am struggling to figure check if the name exist in the table. I thought about using
begin
SELECT e.LAST_NAME as LAST_NAME, o.data
INTO OUTPUT_LOG
FROM EMPLOYEES as e, OUTPUT_LOG as O
WHERE e.employee_id = o.id
end;
but i dont think it would be wise and make sense to do so
Since this appears to be a homework question, I'm not going to solve it all for you but (to help you on your way) you could just use SELECT ... INTO and if a row is not found it will raise a NO_DATA_FOUND exception which you can catch (and use this to bypass the INSERT). Something like this:
DECLARE
p_found NUMBER(1,0);
BEGIN
SELECT 1
INTO p_found
FROM Employees
WHERE ROWNUM = 1
AND last_name = 'King'; -- replace with your where condition
-- then do the insert here (since it will only reach this point if
-- a row was found).
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
/
I have a table which has a column of user defined type.There is a requirement to move this table along with UDT to another schema. I tried to create table as, the problem is it uses the UTD from original schema. Is there any way to achieve this?
Ex:
User 1:
CREATE TYPE employee_t AS OBJECT
(employee_id NUMBER
,employee_name VARCHAR2(30)
,salary NUMBER
,dept_id NUMBER);
CREATE TABLE department
(emp employee_t
,mgr varchar2(40)
,dept_id number
,dept_name varchar2(30));
In the above example, department table has a column called emp which is UDT.
Now on user2, if I do the following
create table department as select * from user1.department.
Table will be created as follows. Actually I want EMPLOYEE_T also to be moved to user2.
Desc department:
Name Null? Type
----------------------------------------- -------- -------------------
EMP USER1.EMPLOYEE_T
MGR VARCHAR2(40)
DEPT_ID NUMBER
DEPT_NAME VARCHAR2(30)
Please somebody explain why
select count(*) from employees where employee_id not in (select manager_id from employees)
returns 0
when clearly there are some employees who are managers also.
I am using HR schema.
based on the query i would suggest you to use
NOT EXISTS as it gives BOOLEAN result which boosts up the performance.
SELECT COUNT(*)
FROM employees
WHERE NOT EXISTS
(SELECT employee_id
FROM employees
WHERE type = 'manager' -- or however you differentiate
-- employees and managers
);
Based on this it looks like you're trying to link the employee_id with the manager_id, which are probably different.
Instead you could query for matching employee_id values for managers, using some other criteria to identify managers in the employees table.
SELECT COUNT(*)
FROM employees
WHERE employee_id NOT IN
(
SELECT employee_id
FROM employees
WHERE type = 'manager' -- or however you differentiate
-- employees and managers
)
I have a sales object and a table (tab1) of sales objects. I want to create a second table (tab2) that contains references to the objects in tab1.
CREATE TYPE sales AS OBJECT
( marca NUMBER(4),
nume VARCHAR2(40),
orasp VARCHAR2(20),
nrv NUMBER(4)
);
/
DROP TABLE tab1 CASCADE CONSTRAINTS;
CREATE TABLE tab1
(
vanzator sales
);
I try to get the object ref, as so but hit the PL/SQL ORA-00904 "p" invalid identifier.
DECLARE
CURSOR c_pers_ref IS
SELECT REF (p)
FROM tab1 p;
rec_vanz_ref REF t2;
What am I doing wrong? Please help.
Ok. Got it. When creating tab1 changed the create line to CREATE TABLE tab1 OF sales;