To get number of employees working in a Department - sql

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;

Related

Find sum of salary from department where sum of salary is smaller than 20000

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

Can't get manager output in SQL

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

SQL developer Query

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
;

Find the number of employees in each department - SQL Oracle

I have two tables Emp and Dept and I am trying to display how many people work in each department along with their department name, but I can't get it to work. I have tried this select count(ename) as count from emp group by deptno; but the output I am getting is this :
COUNT
----------
6
5
3
But I also want to show the names for each of those departments. Here are my tables :
Empt:
CREATE TABLE EMP
(EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2));
INSERT INTO EMP VALUES
(7369, 'SMITH', 'CLERK', 7902,
TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20);
INSERT INTO EMP VALUES
(7499, 'ALLEN', 'SALESMAN', 7698,
TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30);
INSERT INTO EMP VALUES
(7521, 'WARD', 'SALESMAN', 7698,
TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
INSERT INTO EMP VALUES
(7566, 'JONES', 'MANAGER', 7839,
TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
INSERT INTO EMP VALUES
(7654, 'MARTIN', 'SALESMAN', 7698,
TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30);
INSERT INTO EMP VALUES
(7698, 'BLAKE', 'MANAGER', 7839,
TO_DATE('1-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
INSERT INTO EMP VALUES
(7782, 'CLARK', 'MANAGER', 7839,
TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
INSERT INTO EMP VALUES
(7788, 'SCOTT', 'ANALYST', 7566,
TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES
(7839, 'KING', 'PRESIDENT', NULL,
TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10);
INSERT INTO EMP VALUES
(7844, 'TURNER', 'SALESMAN', 7698,
TO_DATE('8-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30);
INSERT INTO EMP VALUES
(7876, 'ADAMS', 'CLERK', 7788,
TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20);
INSERT INTO EMP VALUES
(7900, 'JAMES', 'CLERK', 7698,
TO_DATE('3-DEC-1981', 'DD-MON-YYYY'), 950, NULL, 30);
INSERT INTO EMP VALUES
(7902, 'FORD', 'ANALYST', 7566,
TO_DATE('3-DEC-1981', 'DD-MON-YYYY'), 3000, NULL, 20);
INSERT INTO EMP VALUES
(7934, 'MILLER', 'CLERK', 7782,
TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10);
Dept:
CREATE TABLE DEPT
(DEPTNO NUMBER(2),
DNAME VARCHAR2(14),
LOC VARCHAR2(13) );
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');
Please try:
select count(*) as count,dept.DNAME
from emp
inner join dept on emp.DEPTNO = dept.DEPTNO
group by dept.DNAME
A request to list "Number of employees in each department" or "Display how many people work in each department" is the same as "For each department, list the number of employees", this must include departments with no employees. In the sample database, Operations has 0 employees. So a LEFT OUTER JOIN should be used.
SELECT dept.name, COUNT(emp.empno) AS count
FROM dept
LEFT OUTER JOIN emp ON emp.deptno = dept.deptno
GROUP BY dept.name;
select count(e.empno), d.deptno, d.dname
from emp e, dep d
where e.DEPTNO = d.DEPTNO
group by d.deptno, d.dname;
Try the query below:
select count(*),d.dname from emp e , dept d where d.deptno = e.deptno
group by d.dname
SELECT d.DEPTNO
, d.dname
, COUNT(e.ename) AS count
FROM emp e
INNER JOIN dept d ON e.DEPTNO = d.deptno
GROUP BY d.deptno
, d.dname;
select d.dname
,count(e.empno) as count
from dept d
left outer join emp e
on e.deptno=d.deptno
group by d.dname;
Try to do this:
SQL> select dept,count(*) "no of emp" from employee group by dept;
DEPT no of emp
-------------------- ----------
HR 2
Account 2
Admin 3
select count(EMPNO),DNAME from EMP e join DEPT d on e.DEPTNO = d.DEPTNO group by DNAME ;

Select EMP with max SAL from each DEPT

I´m having a bad time with a SQL query. I´m using oracle default tables:
'EMP' TABLE
http://imageshack.us/photo/my-images/850/sinttuloxps.png/
AND
'DEPT' TABLE
http://imageshack.us/photo/my-images/19/assayp.png/
I wan´t to get the employee with the highest salary in each department.
I´m using SQLPLUS.
Thanks in advance!
ALSO CODE:
CREATE TABLE dept (
deptno numeric(2,0) NOT NULL,
dname character varying(14),
loc character varying(13),
CONSTRAINT dept_pkey PRIMARY KEY (deptno)
);
CREATE TABLE emp (
empno numeric(4,0) NOT NULL,
ename character varying(10),
job character varying(9),
mgr numeric(4,0),
hiredate date,
sal numeric(7,2),
comm numeric(7,2),
deptno numeric(2,0),
CONSTRAINT emp_pkey PRIMARY KEY (empno),
CONSTRAINT emp_deptno_fkey FOREIGN KEY (deptno) REFERENCES dept(deptno),
CONSTRAINT emp_mgr_fkey FOREIGN KEY (mgr) REFERENCES emp(empno)
);
INSERT INTO dept (deptno, dname, loc) VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept (deptno, dname, loc) VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO dept (deptno, dname, loc) VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO dept (deptno, dname, loc) VALUES (40, 'OPERATIONS', 'BOSTON');
insert into emp values (7839, 'KING' , 'PRESIDENT', NULL, to_date('17/11/81','dd/mm/yy') , 5000, NULL, 10);
insert into emp values (7566, 'JONES' , 'MANAGER' , 7839, to_date('02/04/81','dd/mm/yy') , 2975, NULL, 20);
insert into emp values (7902, 'FORD' , 'ANALYST' , 7566, to_date('03/12/81','dd/mm/yy') , 3000, NULL, 20);
insert into emp values (7369, 'SMITH' , 'CLERK' , 7902, to_date('17/12/80','dd/mm/yy') , 800, NULL, 20);
insert into emp values (7698, 'BLAKE' , 'MANAGER' , 7839, to_date('01/05/81','dd/mm/yy') , 2850, NULL, 30);
insert into emp values (7499, 'ALLEN' , 'SALESMAN' , 7698, to_date('20/02/81','dd/mm/yy') , 1600, 300, 30);
insert into emp values (7521, 'WARD' , 'SALESMAN' , 7698, to_date('22/02/81','dd/mm/yy') , 1250, 500, 30);
insert into emp values (7654, 'MARTIN' , 'SALESMAN' , 7698, to_date('28/09/81','dd/mm/yy') , 1250, 1400, 30);
insert into emp values (7782, 'CLARK' , 'MANAGER' , 7839, to_date('09/06/81','dd/mm/yy') , 2450, NULL, 10);
insert into emp values (7788, 'SCOTT' , 'ANALYST' , 7566, to_date('09/12/82','dd/mm/yy') , 3000, NULL, 20);
insert into emp values (7844, 'TURNER' , 'SALESMAN' , 7698, to_date('08/09/81','dd/mm/yy') , 1500, 0, 30);
insert into emp values (7876, 'ADAMS' , 'CLERK' , 7788, to_date('12/01/83','dd/mm/yy') , 1100, NULL, 20);
insert into emp values (7900, 'JAMES' , 'CLERK' , 7698, to_date('03/12/81','dd/mm/yy') , 950, NULL, 30);
insert into emp values (7934, 'MILLER' , 'CLERK' , 7782, to_date('23/01/82','dd/mm/yy') , 1300, NULL, 10);
You can also use the analytical RANK() function:
SELECT * FROM (
SELECT
Dept.DeptNo,
Dept.DName,
Emp.EName,
Emp.Sal,
RANK() OVER (PARTITION BY Dept.DeptNo ORDER BY Emp.Sal DESC) AS DeptSalRank
FROM Emp
INNER JOIN Dept ON Emp.DeptNo = Dept.DeptNo
)
WHERE DeptSalRank = 1
Classic greatest-n-per-group query. Here is what you want:
select dept.dname, emp.empno, emp.ename, emp.sal
from emp
inner join dept on emp.deptno = dept.deptno
inner join
(
select emp.deptno, max(emp.sal) sal
from emp
group by emp.deptno
) ss on emp.deptno = ss.deptno and emp.sal = ss.sal
order by emp.sal desc
Here is a working fiddle: http://sqlfiddle.com/#!4/7147b/6
Additionally, you might want to checkout a different approach. Look here (SQL Select only rows with Max Value on a Column) to see an interesting answer on the topic.
The following query will omit duplicate values
SELECT DEPTNO, MAX(SAL) FROM EMP GROUP BY DEPTNO;
The following query will include the duplicate values
SELECT * FROM EMP WHERE (DEPTNO,SAL) IN
( SELECT DEPTNO, MAX(SAL) FROM EMP GROUP BY DEPTNO)
As short as the question: SELECT DeptID, MAX(Salary) FROM Employees GROUP BY DeptID
Tested. Here is the query:
Select outemp.Employee_ID, outemp.First_Name, outemp.Last_Name
From Hr.Employees outemp,
HR.Departments outdept,
(Select Max(e.salary) maxsal, D.Department_ID Dept_ID From HR.EMPLOYEES e, HR.Departments d
WHERE e.Department_ID = d.Department_ID
Group By d.Department_ID) DummyTable
where outemp.Department_ID = outdept.Department_ID
AND outdept.Department_ID = DummyTable.Dept_ID
AND outemp.Salary = DummyTable.Maxsal
list of top 10 highest paid employee detail
(SELECT *, RANK() OVER (ORDER BY Emp.Sal DESC)AS DeptSalRank
FROM Emp INNER JOIN Dept ON Emp.DeptNo = Dept.DeptNo) LIMIT 10;