I have Object table COMPANIES with a collection of Employees. The code you can see below.
- What is a query to find the company with the largest number of employees?
- How much there are employees with a salary of > 700 in a company ‘Company 1’?
- Show data for Company which has 3 employees.
For this Questions I need 3 SQL queries.
CREATE OR REPLACE TYPE EMPLOYEE_T as OBJECT( LAST_NAME VARCHAR2(20), FIRST_NAME VARCHAR2(20), SALARY NUMBER ); Type EMPLOYEE_T compiled create or replace type EMPLOYEES_T as table of EMPLOYEE_T; Type EMPLOYEES_T compiled create or replace type COMPANY_T as Object( C_NUM Integer, C_NAME VARCHAR2(20), EMPLOYEES EMPLOYEES_T ) ; CREATE TABLE COMPANIES OF COMPANY_T nested table EMPLOYEES store as EMPLOYEES; Table COMPANIES created. INSERT INTO COMPANIES VALUES(0, 'Company 1', EMPLOYEES_T(EMPLOYEE_T('Pugho','Alex',600),EMPLOYEE_T('Uldis','Ivanenko',1500), EMPLOYEE_T('Ovalenko','Ignat',2400))); 1 row inserted. INSERT INTO COMPANIES VALUES(1, 'Company 2', EMPLOYEES_T(EMPLOYEE_T('Pjetrenko','Max',600),EMPLOYEE_T('Plantgerms','Ilja',1500))); 1 row inserted. SELECT * FROM COMPANIES; C_NUM C_NAME ---------- -------------------- EMPLOYEES(LAST_NAME, FIRST_NAME, SALARY) 1 Company 2 EMPLOYEES_T(EMPLOYEE_T('Pjetrenko', 'Max', 600), EMPLOYEE_T('Plantgerms', 'Ilja', 1500)) 0 Company 1 EMPLOYEES_T(EMPLOYEE_T('Pugho', 'Alex', 600), EMPLOYEE_T('Uldis', 'Ivanenko', 1500), EMPLOYEE_T('Ovalenko', 'Ignat', 2400))
Advertisement
Answer
For this Answer I need 3 votes (if it is correct) 🙂
What is a query to find the company with the largest number of employees?
select C_NAME, cnt from ( select t1.C_NAME, count(*) as cnt from companies t1, table(t1.EMPLOYEES) group by t1.C_NAME) where cnt = (select max(cnt) from(select t1.C_NAME, count(*) as cnt from companies t1, table(t1.EMPLOYEES) group by t1.C_NAME))
How much there are employees with a salary of > 700 in a company ‘Company 1’?
select t1.C_NAME, count(*) as cnt from companies t1, table(t1.EMPLOYEES) t2 where t2.SALARY > 700 and t1.c_name = 'Company 1' group by t1.C_NAME;
Show data for Company which has 3 employees.
select t1.*, t2.* from companies t1, table(t1.EMPLOYEES) t2 where t1.C_NAME in ( select t1.C_NAME from companies t1, table(t1.EMPLOYEES) t2 having count(*) = 3 group by t1.C_NAME);