Creating Department table
CREATE TABLE department
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL UNIQUE,
ADDRESS VARCHAR2(20) NOT NULL);
Populating Department Table
INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;
Creating Employee table
CREATE TABLE employee
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),
HIRE_DATE DATE NOT NULL,
SALARY NUMBER(9, 2) NOT NULL,
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES department(DEPARTMENT_ID));
Populating Employee table
INSERT INTO employee
VALUES(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee
VALUES(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL, 10);
INSERT INTO employee
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee
VALUES(7999, 'WOLFE', 'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee
VALUES(7952, 'LANCASTER', 'SALESMAN', 7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900, NULL, 40);
INSERT INTO employee
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee
VALUES(8000, 'BREWSTER', 'TBA', NULL, '22-AUG-13', 2500, NULL, NULL);
COMMIT;
QUESTION: Based on the DEPARTMENT and EMPLOYEE tables created in write a PL/SQL anonymous block that accepts an employee ID from the user input and displays 1) the employee’s name, hire date, and total pay (salary + commission), 2) his/her manager’s name, hire date, and total pay (salary + commission), 3) the name of the department where the employee works, 4) the number of employees in the department where the employee works, and 5) the sum of total pay (salary + commission) for the department where the employee works,
So far I've got the query below I am not able to get the manager's name, hire date and total pay (salary + comission) how can I do that?
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate EMPLOYEE.HIRE_DATE%TYPE;
BEGIN
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
DBMS_OUTPUT.PUT_LINE(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE employee_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('USER INPUT:');
DBMS_OUTPUT.PUT_LINE(v_input_emp_id);
DBMS_OUTPUT.PUT_LINE('----------EMPLOYEE----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE manager_id = v_input_emp_id;
IF v_n != 0 THEN
SELECT employee_name ,salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name , v_pay, v_hiredate
FROM employee
WHERE employee.manager_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
END IF;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
ELSE
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NOT IN THE TABLE');
END IF;
END;
You should first get employees manager_id, and then query the manager.
This works for me:
SET SERVEROUTPUT ON
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_mangager_id employee.manager_id%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate employee.hire_date%TYPE;
BEGIN
SELECT COUNT(*) INTO v_n FROM employee WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
dbms_output.put_line(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name
,salary + nvl(commission, 0)
,hire_date
,manager_id
INTO v_emp_name
,v_pay
,v_hiredate
,v_mangager_id
FROM employee
WHERE employee_id = v_input_emp_id;
dbms_output.put_line('USER INPUT:');
dbms_output.put_line(v_input_emp_id);
dbms_output.put_line('----------EMPLOYEE----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
dbms_output.put_line(rpad('HIRE DATE:', 10) || lpad(v_hiredate, 20));
dbms_output.put_line('TOTAL PAY:' || lpad(to_char(round(v_pay, 2), '$9,990.00'), 21));
SELECT COUNT(*) INTO v_n FROM employee WHERE employee_id = v_mangager_id;
IF v_n != 0 THEN
SELECT employee_name
,salary + nvl(commission, 0)
,hire_date
INTO v_emp_name
,v_pay
,v_hiredate
FROM employee
WHERE employee.employee_id = v_mangager_id;
dbms_output.put_line('----------MANAGER----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
dbms_output.put_line(rpad('HIRE DATE:', 10) || lpad(v_hiredate, 20));
dbms_output.put_line('TOTAL PAY:' || lpad(to_char(round(v_pay, 2), '$9,990.00'), 21));
END IF;
dbms_output.put_line('----------MANAGER----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
ELSE
dbms_output.put_line('EMPLOYEE NOT IN THE TABLE');
END IF;
END;
/
And the result:
USER INPUT:
7610
----------EMPLOYEE----------
NAME: WILSON
HIRE DATE: 2001.12.03
TOTAL PAY: $3,000.00
----------MANAGER----------
NAME: JOST
HIRE DATE: 2001.05.04
TOTAL PAY: $4,500.00
----------MANAGER----------
NAME: JOST
PL/SQL procedure successfully completed
Related
I'm trying to write a query that returns the total salary of every department.
So it needs to look who works in a department and count the salary up and do this for every department. Then it should only show me the departments that have a total salary lower than 20000.
My database looks like this
create table dept
(
deptno number(2,0),
dname varchar2(14),
loc varchar2(13),
constraint pk_dept primary key (deptno)
);
create table emp
(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
mgr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0),
constraint pk_emp primary key (empno),
constraint fk_deptno foreign key (deptno) references dept (deptno)
);
insert into dept
values(10, 'ACCOUNTING', 'NEW YORK');
insert into dept
values(20, 'RESEARCH', 'DALLAS');
insert into dept
values(30, 'SALES', 'CHICAGO');
insert into dept
values(40, 'OPERATIONS', 'BOSTON');
insert into emp
values(7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981','dd-mm-yyyy'), 5000, null, 10);
insert into emp
values(7698, 'BLAKE', 'MANAGER', 7839, to_date('1-5-1981','dd-mm-yyyy'), 2850, null, 30);
insert into emp
values(7782, 'CLARK', 'MANAGER', 7839, to_date('9-6-1981','dd-mm-yyyy'), 2450, null, 10);
insert into emp
values(7566, 'JONES', 'MANAGER', 7839, to_date('2-4-1981','dd-mm-yyyy'), 2975, null, 20);
insert into emp
values(7788, 'SCOTT', 'ANALYST', 7566, to_date('13-JUL-87','dd-mm-rr') - 85, 3000, null, 20);
insert into emp
values(7902, 'FORD', 'ANALYST', 7566, to_date('3-12-1981','dd-mm-yyyy'), 3000, null, 20);
insert into emp
values(7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980','dd-mm-yyyy'), 800, null, 20);
insert into emp
values(7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-2-1981','dd-mm-yyyy'), 1600, 300, 30);
insert into emp
values(7521, 'WARD', 'SALESMAN', 7698, to_date('22-2-1981','dd-mm-yyyy'), 1250, 500, 30);
insert into emp
values(7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-9-1981','dd-mm-yyyy'), 1250, 1400, 30);
insert into emp
values(7844, 'TURNER', 'SALESMAN', 7698, to_date('8-9-1981','dd-mm-yyyy'), 1500, 0, 30);
insert into emp
values(7876, 'ADAMS', 'CLERK', 7788, to_date('13-JUL-87', 'dd-mm-rr') - 51, 1100, null, 20);
insert into emp
values(7900, 'JAMES', 'CLERK', 7698, to_date('3-12-1981','dd-mm-yyyy'), 950, null, 30);
insert into emp
values(7934, 'MILLER', 'CLERK', 7782, to_date('23-1-1982','dd-mm-yyyy'), 1300, null, 10);
I tried using this query but it keeps telling me that name is an invalid identifier.
SELECT
(SELECT d.dname
FROM dept d
WHERE d.deptno = e.deptno) AS naam,
SUM(e.sal) AS sal
FROM
emp e
WHERE
sal < 20000
GROUP BY
naam;
Does anyone know what I'm doing wrong?
Use inner join and having
SELECT d.dname, sum(e.sal) sal
FROM dept d
INNER JOIN emp e on d.deptno = e.deptno
GROUP BY d.dname
HAVING sum(e.sal) < 20000
Creating Department table
CREATE TABLE department
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL UNIQUE,
ADDRESS VARCHAR2(20) NOT NULL);
Populating Department Table
INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;
Creating Employee table
CREATE TABLE employee
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),
HIRE_DATE DATE NOT NULL,
SALARY NUMBER(9, 2) NOT NULL,
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES department(DEPARTMENT_ID));
Populating Employee table
INSERT INTO employee
VALUES(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee
VALUES(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL, 10);
INSERT INTO employee
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee
VALUES(7999, 'WOLFE', 'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee
VALUES(7952, 'LANCASTER', 'SALESMAN', 7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900, NULL, 40);
INSERT INTO employee
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee
VALUES(8000, 'BREWSTER', 'TBA', NULL, '22-AUG-13', 2500, NULL, NULL);
COMMIT;
QUESTION: Based on the DEPARTMENT and EMPLOYEE tables created in write a PL/SQL anonymous block that accepts an employee ID from the user input and displays 1) the employee’s name, hire date, and total pay (salary + commission), 2) his/her manager’s name, hire date, and total pay (salary + commission), 3) the name of the department where the employee works, 4) the number of employees in the department where the employee works, and 5) the sum of total pay (salary + commission) for the department where the employee works,
So far I've got the query below I am not able to get the number of employees working in the department. How can I do that?
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate EMPLOYEE.HIRE_DATE%TYPE;
BEGIN
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
DBMS_OUTPUT.PUT_LINE(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE employee_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('USER INPUT:');
DBMS_OUTPUT.PUT_LINE(v_input_emp_id);
DBMS_OUTPUT.PUT_LINE('----------EMPLOYEE----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
IF v_input_emp_id IS NOT NULL THEN
SELECT manager_id
INTO v_MANAGER_ID
FROM EMPLOYEE
WHERE employee_id = v_input_emp_id;
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE v_manager_id = employee_id;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
ELSE
DBMS_OUTPUT.PUT_LINE ('NO DATA');
END IF;
IF v_n = 1 THEN
SELECT department_name
INTO v_dept_name
FROM department
INNER JOIN employee
ON department.department_id = employee.department_id
WHERE employee_id = v_input_emp_id;
/*
SELECT department_id
INTO v_dept_id
from department
WHERE department_id = v_dept_id;
SELECT COUNT(*)
INTO v_emp_num
FROM employee INNER JOIN DEPARTMENT
ON DEPARTMENT.DEPARTMENT_ID = EMPLOYEE.DEPARTMENT_ID
WHERE employee.department_id = department.v_dept_id;
*/
DBMS_OUTPUT.PUT_LINE('----------DEPARTMENT----------');
DBMS_OUTPUT.PUT_LINE('NAME: ' || v_dept_name);
/*DBMS_OUTPUT.PUT_LINE('NUMBER OF EMPLOYEES' || ' ' || TO_CHAR(v_emp_num));
*/
ELSE
DBMS_OUTPUT.PUT_LINE('No output!');
END IF;
END IF;
END;
you probably need simple query for departament, for example:
SELECT department_name
, count(employee_id) over (partition by department_id) AS count_of_employee
, EMPLOYEE_NAME
, HIRE_DATE
, SALARY + COMMISSION AS pay
, managers.EMPLOYEE_NAME AS manager
, sum(SALARY + COMMISSION) over (partition by department_id) AS total_pay
FROM department
INNER JOIN employee ON department.department_id = employee.department_id
LEFT JOIN employee as managers ON managers.employee_id = employee.MANAGER_ID
WHERE department_id = .........;
or for employee:
SELECT department_name
, count_of_employee
, EMPLOYEE_NAME
, HIRE_DATE
, SALARY + COMMISSION AS pay
, managers.EMPLOYEE_NAME AS manager
, total_pay
FROM employee
INNER JOIN department ON department.department_id = employee.department_id
INNER JOIN (
SELECT Count(1) AS count_of_employee
, Sum(SALARY + COMMISSION) AS total_pay
, department_id
FROM employee
GROUP BY department_id
) AS employee_total ON employee_total.department_id = employee.department_id
LEFT JOIN employee as managers ON managers.employee_id = employee.MANAGER_ID
WHERE employee_id = v_input_emp_id;
QUESTION: Write a SQL SELECT statement to display the name and address of all departments (except the departments in Dallas) having maximum number of employees. Sort your output in ascending order by department name.
Creating Department Table
CREATE TABLE department
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL UNIQUE,
ADDRESS VARCHAR2(20) NOT NULL);
Populating Department Table
INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;
Creating Employee Table
CREATE TABLE employee
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),
HIRE_DATE DATE NOT NULL,
SALARY NUMBER(9, 2) NOT NULL,
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES department(DEPARTMENT_ID));
Populating Employee Table
INSERT INTO employee
VALUES(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee
VALUES(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL, 10);
INSERT INTO employee
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee
VALUES(7999, 'WOLFE', 'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee
VALUES(7952, 'LANCASTER', 'SALESMAN', 7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900, NULL, 40);
INSERT INTO employee
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee
VALUES(8000, 'BREWSTER', 'TBA', NULL, '22-AUG-13', 2500, NULL, NULL);
COMMIT;
The Query That i wrote is below and it works perfectly in finding the maximum number of employees in DALLAS department but I want to eliminate DALLAS and find the other department which has the MAXIMUM number of employee's working in it which is by the data are (CHICAGO and NEW YORK). Can anyone please help me out?
SELECT d.department_id, d.department_name, COUNT(*)
FROM department d
INNER JOIN employee e
ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM employee
GROUP BY department_id)
ORDER BY department_id;
Using your query:
SELECT d.department_id, d.department_name, COUNT(*)
FROM department d
INNER JOIN employee e
ON d.deptartment_id = e.department_id
where d.address != 'DALLAS'
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM employee
where department_id not in (select department_id from department where address = 'DALLAS')
GROUP BY department_id)
ORDER BY department_id;
;
You can use a rank analytic function:
select *
from (
select department_id, department_name, rank() over (order by cnt desc) rnk
from(
SELECT d.department_id, d.department_name, COUNT(*) cnt
FROM department d
INNER JOIN employee e
ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
)
)
where rnk = 2 --here you may change to what position you want
;
---create EMPLOYEE table---
CREATE TABLE EMPLOYEES
(
EMPLOYEEID INT NOT NULL,
EMPLOYNAME VARCHAR2(25) NULL,
EMPLOYAGE INT NULL,
EMPLOYADDRESS VARCHAR2(25) NULL,
SALARY NUMBER(9,2) DEFAULT (0) NULL,
CONSTRAINT EMPLOYEES_PK PRIMARY KEY(EMPLOYEEID)
);
--INSERT EMPLOYEES--
INSERT INTO EMPLOYEES
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00);
INSERT INTO EMPLOYEES
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00);
INSERT INTO EMPLOYEES
VALUES (3, 'Kaushik', 24, 'Kota', 2000.00);
INSERT INTO EMPLOYEES
VALUES (4,'Chaitali', 25, 'Mumbai', 6500.00);
INSERT INTO EMPLOYEES
VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00);
INSERT INTO EMPLOYEES
VALUES (6, 'Komal', 22, 'MP', 4500.00);
--CREATE TRIGGER FOR AN UPDATE TO THE SALARY--
CREATE OR REPLACE TRIGGER display
BEFORE DELETE OR INSERT OR UPDATE ON EMPLOYEES
FOR EACH ROW
WHEN (NEW.employeeid >0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
End;
--triggering trigger with insert--
INSERT INTO EMPLOYEES (EMPLOYEEID,EMPLOYNAME,EMPLOYAGE, EMPLOYADDRESS, SALARY)
VALUES(9,'Kri', 67, 'PK', 7900.00);
UPDATE EMPLOYEES
SET SALARY = SALARY +500
WHERE EMPLOYEEID = 4;
this code will all compile in SQL developer but when I try to update and run any of the code it will not run. I enable it through the trigger tab in developer, so it should be working fine. Any help is appreciated because I am teaching this to my self.
thanks
RMan2015
I was practising for Dynamic SQL and Procedures in order to implement some scripts for weekly, daily, and monthly dashboards. These dashboards require that we create tables, hence I decided to stick with Dynamic SQL.
While practising for these, I am getting some errors, and to simulate the errors, I have created the following tables and copied the procedures.
1. Generating the EMP table
CREATE TABLE EMP
(EMPNO NUMERIC(4) NOT NULL,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR NUMERIC(4),
HIREDATE DATE,
SAL NUMERIC(7, 2),
COMM NUMERIC(7, 2),
DEPTNO NUMERIC(2));
INSERT INTO EMP VALUES
(7369, 'SMITH', 'CLERK', 7902, '17-DEC-1980', 800, NULL, 20);
INSERT INTO EMP VALUES
(7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-1981', 1600, 300, 30);
INSERT INTO EMP VALUES
(7521, 'WARD', 'SALESMAN', 7698, '22-FEB-1981', 1250, 500, 30);
INSERT INTO EMP VALUES
(7566, 'JONES', 'MANAGER', 7839, '2-APR-1981', 2975, NULL, 20);
INSERT INTO EMP VALUES
(7654, 'MARTIN', 'SALESMAN', 7698, '28-SEP-1981', 1250, 1400, 30);
INSERT INTO EMP VALUES
(7698, 'BLAKE', 'MANAGER', 7839, '1-MAY-1981', 2850, NULL, 30);
INSERT INTO EMP VALUES
(7782, 'CLARK', 'MANAGER', 7839, '9-JUN-1981', 2450, NULL, 10);
INSERT INTO EMP VALUES
(7788, 'SCOTT', 'ANALYST', 7566, '09-DEC-1982', 3000, NULL, 20);
INSERT INTO EMP VALUES
(7839, 'KING', 'PRESIDENT', NULL, '17-NOV-1981', 5000, NULL, 10);
INSERT INTO EMP VALUES
(7844, 'TURNER', 'SALESMAN', 7698, '8-SEP-1981', 1500, 0, 30);
INSERT INTO EMP VALUES
(7876, 'ADAMS', 'CLERK', 7788, '12-JAN-1983', 1100, NULL, 20);
INSERT INTO EMP VALUES
(7900, 'JAMES', 'CLERK', 7698, '3-DEC-1981', 950, NULL, 30);
INSERT INTO EMP VALUES
(7902, 'FORD', 'ANALYST', 7566, '3-DEC-1981', 3000, NULL, 20);
INSERT INTO EMP VALUES
(7934, 'MILLER', 'CLERK', 7782, '23-JAN-1982', 1300, NULL, 10);
2. Generating the EmpSalary table
CREATE TABLE empSalary
(ename varchar(20),
salary number
);
INSERT INTO empSalary VALUES ('SMITH', 2000);
INSERT INTO empSalary VALUES ('WARD', 300);
COMMIT;
The following procedure works fine:
---- Formulating a Procedure - Try 1
CREATE OR REPLACE PROCEDURE getData(dataType IN VARCHAR2)
IS
newTbl VARCHAR2(20);
sqlQuery VARCHAR2(100);
empName VARCHAR2(20) := 'SMITH';
BEGIN
IF dataType = 'Weekly' THEN
newTbl := 'empTblWeekly'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM emp';
EXECUTE IMMEDIATE sqlQuery;
ELSIF dataType = 'Daily' THEN
newTbl := 'empTblDaily'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM empSalary';
EXECUTE IMMEDIATE sqlQuery;
ELSIF dataType = 'Monthly' THEN
newTbl := 'empTblMonthly'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM emp WHERE ENAME = ''SMITH''';
EXECUTE IMMEDIATE sqlQuery;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found!');
COMMIT;
END;
BEGIN
getData('Monthly');
END;
SELECT *
FROM empTblMonthly21Feb;
The following 2 formulations return errors:
---- Formulating a Procedure - Try 2
CREATE OR REPLACE PROCEDURE getData(dataType IN VARCHAR2)
IS
newTbl VARCHAR2(20);
sqlQuery VARCHAR2(100);
empName VARCHAR2(20) := 'SMITH';
BEGIN
IF dataType = 'Weekly' THEN
newTbl := 'empTblWeekly'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM emp';
EXECUTE IMMEDIATE sqlQuery;
ELSIF dataType = 'Daily' THEN
newTbl := 'empTblDaily'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM empSalary';
EXECUTE IMMEDIATE sqlQuery;
ELSIF dataType = 'Monthly' THEN
newTbl := 'empTblMonthly'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM emp WHERE ENAME = :empName';
EXECUTE IMMEDIATE sqlQuery USING empName;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found!');
COMMIT;
END;
BEGIN
getData('Monthly');
END;
SELECT *
FROM empTblMonthly21Feb;
---ORA-01027: bind variables not allowed for data definition operations ORA-06512: at "SYS.GETDATA", line 18 ORA-06512: at line 2
---- Formulating a Procedure - Try 3
CREATE OR REPLACE PROCEDURE getData(dataType IN VARCHAR2)
IS
newTbl VARCHAR2(20);
sqlQuery VARCHAR2(100);
empName VARCHAR2(20) := 'SMITH';
BEGIN
IF dataType = 'Weekly' THEN
newTbl := 'empTblWeekly'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM emp';
EXECUTE IMMEDIATE sqlQuery;
ELSIF dataType = 'Daily' THEN
newTbl := 'empTblDaily'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM empSalary';
EXECUTE IMMEDIATE sqlQuery;
ELSIF dataType = 'Monthly' THEN
newTbl := 'empTblMonthly'||TO_CHAR(SYSDATE, 'ddmon');
sqlQuery := 'CREATE TABLE '||newTbl||' as SELECT * FROM emp WHERE ENAME ='||empName;
EXECUTE IMMEDIATE sqlQuery;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found!');
COMMIT;
END;
BEGIN
getData('Monthly');
END;
SELECT *
FROM empTblMonthly21Feb;
---ORA-00904: "SMITH": invalid identifier ORA-06512: at "SYS.GETDATA", line 18 ORA-06512: at line 2
But I am unable to understand why the errors, because in the 2nd try, I am not using a bind variable for a Table name, and in the 3rd try I am simply concatenating a variable...
Thanks for the assistance...
Your third attempt would have worked, but you forgot the single quotes in the sql statement (around ename variable). Try something like:
declare
l_tabname varchar2(100) := 'MY_TAB1';
l_name varchar2(100) := 'SMITH';
l_sql varchar2(1000);
begin
l_sql := 'create table ' || l_tabname || ' as select * from emp where ename = ''' || l_name || '''';
execute immediate l_sql;
end;
No bind vars used, but its still creating the table using variables.