Case Study 2 Solution

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

USE JARVIS1;

CREATE TABLE LOCATION


(
LOCATION_ID INT PRIMARY KEY,
CITY VARCHAR(20)
);

INSERT INTO LOCATION


VALUES
(122, 'NEW YORK'),
(123, 'DALLAS'),
(124, 'CHICAGO'),
(167, 'BOSTON');

CREATE TABLE DEPARTMENT


(
DEPARTMENT_ID INT PRIMARY KEY,
NAME VARCHAR(20),
LOCATION_ID INT REFERENCES LOCATION(LOCATION_ID)
);

INSERT INTO DEPARTMENT


VALUES
(10, 'ACCOUNTING', 122),
(20, 'SALES', 124),
(30, 'RESEARCH', 123),
(40, 'OPERATIONS', 167);

CREATE TABLE JOB(JOB_ID INT,DESIGNATION VARCHAR(20))

INSERT INTO JOB VALUES


(667,'Clerk'),
(668,'Staff'),
(669,'Analyst'),
(670,'Sales Person'),
(671,'Manager'),
(672,'President');

CREATE TABLE EMPLOYEE (Employee_ID INT, Last_Name VARCHAR(20), First_Name


VARCHAR(20), Middle_Name VARCHAR(20),
Job_ID INT, Manager_ID INT, Hire_Date DATE,Salary MONEY, Comm INT,Department_ID
INT);

INSERT INTO EMPLOYEE VALUES


(7369, 'SMITH', 'JOHN', 'Q', 667, 7902, '17-DEC-84', 800, NULL, 20),
(7499, 'ALLEN', 'KEVIN', 'J', 670, 7698, '20-FEB-85', 1600, 300, 30),
(7505, 'DOYLE', 'JEAN', 'K', 671, 7839, '04-APR-85', 2850, NULL, 30),
(7506, 'DENNIS', 'LYNN', 'S', 671, 7839, '15-MAY-85', 2750, NULL, 30),
(7507, 'BAKER', 'LESLIE', 'D', 671, 7839, '10-JUN-85', 2200, NULL, 40),
(7521, 'WARK', 'CYNTHIA', 'D', 670, 7698, '22-FEB-85', 1250, 500, 30);

SELECT * FROM EMPLOYEE;

SELECT * FROM LOCATION;


SELECT * FROM DEPARTMENT;
SELECT * FROM JOB;
SELECT * FROM EMPLOYEE;

--SIMPLE QUERIES
/*1) LIST ALL THE EMPLOYEE DETAILS.*/
SELECT * FROM EMPLOYEE;

/*2) LIST ALL THE DEPARTMENT DETAILS*/


SELECT * FROM DEPARTMENT;

/*3)LIST ALL JOB DETAILS.*/


SELECT * FROM JOB;

/*4) LIST ALL THE LOCATIONS.*/


SELECT * FROM LOCATION;

/*5) LIST OUT THE FIRSTNAME, LASTNAME, SALARY,


COMMISSION FOR ALL EMPLOYEES*/
SELECT
FIRST_NAME, LAST_NAME, SALARY, COMM
FROM EMPLOYEE;

/*6) LIST OUT EMPLOYEEID,LAST NAME, DEPARTMENT ID FOR ALL EMPLOYEES


AND ALIAS EMPLOYEEID AS "ID OF THE EMPLOYEE",
LAST NAME AS "NAME OF THE EMPLOYEE",
DEPARTMENTID AS "DEP_ID".*/
SELECT
EMPLOYEE_ID AS 'ID OF THE EMPLOYEE',
LAST_NAME AS 'NAMR OF THE EMPLOYEE',
DEPARTMENT_ID AS 'DEP_ID'
FROM EMPLOYEE;

/*7) LIST OUT THE EMPLOYEES ANNUAL SALARY WITH THEIR NAMES ONLY.*/
SELECT FIRST_NAME+' '+ MIDDLE_NAME +' '+ LAST_NAME AS FULL_NAME,
(SALARY*12) AS ANNUAL_SALARY
FROM EMPLOYEE;

--WHERE CONDITION
/*1) LIST THE DETAILS ABOUT "SMITH"*/

SELECT * FROM EMPLOYEE WHERE FIRST_NAME = 'SMITH' OR LAST_NAME = 'SMITH';


SELECT * FROM EMPLOYEE WHERE 'SMITH' IN (FIRST_NAME, LAST_NAME);

/*2) LIST OUT THE EMPLOYEES WHO ARE WORKING IN DEPARTMENT 20*/
SELECT * FROM EMPLOYEE WHERE Department_ID = 20;

/*3) LIST OUT THE EMPLOYEES WHO ARE EARNING SALARY BETWEEN 3000 AND 4500.*/
SELECT * FROM EMPLOYEE WHERE Salary BETWEEN 3000 AND 4500;

/*4) LIST OUT THE EMPLOYEES WHO ARE WORKING IN DEPARTMENT 10 OR 20.*/
SELECT * FROM EMPLOYEE WHERE DEPARTMENT_ID = 10 OR DEPARTMENT_ID = 20;
SELECT * FROM EMPLOYEE WHERE DEPARTMENT_ID IN (10, 20);

/*5) FIND OUT THE EMPLOYEES WHO ARE NOT WORKING IN DEPARTMENT 10 OR 30.*/
SELECT * FROM EMPLOYEE WHERE Department_ID NOT IN (10, 30);

/*6) LIST OUT THE EMPLOYEES WHOSE NAME STARTS WITH 'S'*/
SELECT * FROM EMPLOYEE WHERE First_Name LIKE 'S%';

/*7) LIST OUT THE EMPLOYEES WHOSE NAME STARTS WITH 'S' AND ENDS WITH 'H'*/
SELECT * FROM EMPLOYEE
WHERE FIRST_NAME LIKE 'S%'
AND LAST_NAME LIKE '%H';
SELECT * FROM EMPLOYEE
WHERE LAST_NAME LIKE 'S%H';

/*8) LIST OUT THE EMPLOYEES WHOSE NAME LENGTH IS 4 AND START WITH 'S'.*/
SELECT * FROM EMPLOYEE
WHERE First_Name LIKE 'S___';

SELECT * FROM EMPLOYEE


WHERE FIRST_NAME LIKE 'S%' AND LEN(FIRST_NAME) = 4;

/*9) LIST OUT EMPLOYEES WHO ARE WORKING IN DEPARRTMENT 10 AND DRAW THE
SALARIES MORE THAN 3500.*/
SELECT * FROM EMPLOYEE
WHERE Department_ID = 10
AND SALARY > 3500;

/*10) LIST OUT THE EMPLOYEES WHO ARE NOT RECEVING COMMISSION*/
SELECT * FROM EMPLOYEE
WHERE COMM IS NULL;

--ORDER BY CLAUSE
/*1) LIST OUT THE EMPLOYEE ID, LAST NAME IN
ASCENDING ORDER BASED ON THE EMPLOYEE ID.*/

SELECT EMPLOYEE_ID, LAST_NAME FROM EMPLOYEE


ORDER BY Employee_ID ASC;

/*2) LIST OUT THE EMPLOYEE ID, NAME IN DESCENDING ORDER BASED ON SALARY.*/
SELECT Employee_ID, FIRST_NAME+' '+MIDDLE_NAME+' '+LAST_NAME AS FULLNAME
FROM EMPLOYEE
ORDER BY SALARY DESC;
/*3) LIST OUT THE EMPLOYEE DETAILS ACCORDING TO THEIR
LAST-NAME IN ASCENDING ORDER*/
SELECT * FROM EMPLOYEE
ORDER BY LAST_NAME;

/*4) LIST OUT THE EMPLOYEE DETAILS ACCORDING TO THEIR LAST-NAME


IN ASCENDING ORDER AND THEN ON DEPARTMENT_ID IN DESCENDING ORDER.*/
SELECT * FROM EMPLOYEE
ORDER BY LAST_NAME, DEPARTMENT_ID DESC;

--GROUP BY AND HAVING CLAUSE


/* 1) HOW MANY EMPLOYEES WHO ARE IN DIFFERENT DEPARTMENTS WISE
IN THE ORGANIZATION*/
SELECT
DEPARTMENT_ID,
COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES
FROM EMPLOYEE
GROUP BY Department_ID;
/*2) LIST OUT THE DEPARTMENT WISE MAXIMUM SALARY,
MINIMUM SALARY, AVERAGE SALARY OF THE EMPLOYEES*/

SELECT
DEPARTMENT_ID,
MIN(SALARY) AS MIN_SALARY,
MAX(SALARY) AS MAX_SALARY,
AVG(SALARY) AS AVG_SALARY
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID;

/*3) LIST OUT JOB WISE MAXIMUM SALARY, MINIMUM SALARY,


AVERAGE SALARIES OF THE EMPLOYEES.*/
SELECT
JOB_ID,
MAX(SALARY) AS MAX_SALARY,
MIN(SALARY) AS MIN_SALARY,
AVG(SALARY) AS AVG_SALARY
FROM EMPLOYEE
GROUP BY JOB_ID;
/*4) LIST OUT THE NUMBER OF EMPLOYEES JOINED
IN EVERY MONTH IN ASCENDING ORDER*/

SELECT MONTH(HIRE_DATE) AS MONTH, COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES


FROM EMPLOYEE
GROUP BY MONTH(HIRE_DATE);

/*5) LIST OUT THE NUMBER OF EMPLOYEES FOR EACH MONTH AND YEAR,
IN THE ASCENDING ORDER BASED ON THE YEAR, MONTH.*/
SELECT
MONTH(HIRE_DATE) AS MONTH,
YEAR(HIRE_DATE) AS YEAR,
COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES
FROM EMPLOYEE
GROUP BY MONTH(HIRE_DATE), YEAR(HIRE_DATE)
ORDER BY YEAR(HIRE_DATE), MONTH(HIRE_DATE) ASC;

/*6) LIST OUT THE DEPARTMENT ID HAVING ATLEAST FOUR EMPLOYEES.*/


SELECT DEPARTMENT_ID FROM EMPLOYEE
GROUP BY DEPARTMENT_ID
HAVING COUNT(DEPARTMENT_ID) = 4;

/*7) HOW MANY EMPLOYEES JOINED IN JANUARY MONTH.*/


SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES
FROM EMPLOYEE
WHERE MONTH(HIRE_DATE) = 1;

SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES, MONTH(HIRE_DATE) AS MONTH


FROM EMPLOYEE
GROUP BY MONTH(HIRE_DATE)
HAVING MONTH(HIRE_DATE) = 1;

/*8) HOW MANY EMPLOYEES JOINED IN JANUARY OR SEPTEMBER MONTH.*/


SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES
FROM EMPLOYEE
WHERE MONTH(HIRE_DATE) IN (1, 9);

SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES, MONTH(HIRE_DATE) AS MONTH


FROM EMPLOYEE
GROUP BY MONTH(HIRE_DATE)
HAVING MONTH(HIRE_DATE) IN (1,9);

/*9) HOW MANY EMPLOYEES WERE JOINED IN 1985?*/


SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES
FROM EMPLOYEE
WHERE YEAR(HIRE_DATE) = 1985;

SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES, YEAR(HIRE_DATE) AS YEAR


FROM EMPLOYEE
GROUP BY YEAR(HIRE_DATE)
HAVING YEAR(HIRE_DATE) = 1985;

/*10) HOW MANY EMPLOYEES WERE JOINED EACH MONTH IN 1985.*/


SELECT
COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES,
MONTH(HIRE_DATE) AS MONTH,
YEAR(HIRE_DATE) AS YEAR
FROM EMPLOYEE
GROUP BY MONTH(HIRE_DATE), YEAR(HIRE_DATE)
HAVING YEAR(HIRE_DATE) = 1985;

/*11) HOW MANY EMPLOYEES WERE JOINED IN MARCH 1985?*/


SELECT
COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES,
MONTH(HIRE_DATE) AS MONTH,
YEAR(HIRE_DATE) AS YEAR
FROM EMPLOYEE
GROUP BY MONTH(HIRE_DATE), YEAR(HIRE_DATE)
HAVING MONTH(HIRE_DATE) = 3 AND YEAR(HIRE_DATE) = 1985;

/*12) WHICH IS THE DEPARTMENT ID,


HAVING GREATER THAN OR EQUAL TO 3 EMPLOYEES JOINED IN APRIL 1985?*/
SELECT DEPARTMENT_ID,
MONTH(HIRE_DATE) AS MONTH,
YEAR(HIRE_DATE) AS YEAR
FROM EMPLOYEE
GROUP BY Department_ID, MONTH(HIRE_DATE), YEAR(HIRE_DATE)
HAVING MONTH(HIRE_DATE) = 4 AND YEAR(HIRE_DATE) = 1985
AND COUNT(*) >= 3;

--JOINS
/*1) LIST OUT EMPLOYEES WITH THEIR DEPARTMENT NAMES*/
SELECT * FROM EMPLOYEE;
SELECT * FROM DEPARTMENT;

SELECT
E.EMPLOYEE_ID,
E.FIRST_NAME+' '+E.MIDDLE_NAME+' '+E.LAST_NAME AS FULLNAME,
E.DEPARTMENT_ID,
D.NAME
FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON E.Department_ID = D.DEPARTMENT_ID;

/*2)DISPLAY EMPLOYEES WITH THEIR DESIGNATIONS.*/


SELECT * FROM EMPLOYEE;
SELECT * FROM JOB;

SELECT
E.EMPLOYEE_ID,
J.JOB_ID,
E.FIRST_NAME+' '+E.MIDDLE_NAME+' '+E.LAST_NAME AS FULLNAME
FROM EMPLOYEE E INNER JOIN JOB J ON E.JOB_ID = J.JOB_ID;

/*3)DISPLAY THE EMPLOYEES WITH THEIR DEPARTMENT NAMES AND REGIONAL GROUPS.*/
SELECT * FROM LOCATION;
SELECT * FROM DEPARTMENT;
SELECT * FROM JOB;
SELECT * FROM EMPLOYEE;

SELECT
E.EMPLOYEE_ID,
E.FIRST_NAME+' '+E.MIDDLE_NAME+' '+E.LAST_NAME AS FULLNAME,
D.DEPARTMENT_ID,
D.NAME,
L.LOCATION_ID,
L.CITY
FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON E.Department_ID = D.DEPARTMENT_ID
INNER JOIN LOCATION L ON D.LOCATION_ID = L.LOCATION_ID;

/*4)HOW MANY EMPLOYEES WHO ARE WORKING IN DIFFERENT DEPARTMENTS


AND DISPLAY WITH DEPARTMENT NAMES.*/
SELECT
D.NAME,
COUNT(E.EMPLOYEE_ID) AS NO_OF_EMPLOYEES
FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON E.Department_ID = D.DEPARTMENT_ID
GROUP BY D.NAME;

/*5)HOW MANY EMPLOYEES WHO ARE WORKING IN SALES DEPARTMENT.*/


SELECT * FROM EMPLOYEE;
SELECT * FROM DEPARTMENT;

SELECT
COUNT(E.EMPLOYEE_ID) AS NO_OF_EMPLOYEES,
D.NAME
FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID = E.Department_ID
GROUP BY D.NAME
HAVING D.NAME = 'SALES';

/*6)WHICH IS THE DEPARTMENT HAVING GREATER THAN OR EQUAL TO 5


EMPLOYEES AND DISPLAY THE DEPARTMENT NAMES IN ASCENDING ORDER.*/

SELECT
D.NAME,
E.Department_ID,
COUNT(E.DEPARTMENT_ID) AS TOTAL_EMPLOYEES
FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON D.DEPARTMENT_ID = E.Department_ID
GROUP BY D.NAME, E.Department_ID
HAVING COUNT(E.Department_ID) >= 5;

SELECT
D.NAME,
E.Department_ID,
COUNT(E.DEPARTMENT_ID) AS TOTAL_EMPLOYEES
FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON D.DEPARTMENT_ID = E.Department_ID
GROUP BY D.NAME, E.Department_ID
HAVING COUNT(E.Department_ID) >= 4;

/*7) HOW MANY JOBS IN THE ORGANIZATION WITH DESIGNATIONS.*/


SELECT * FROM JOB
SELECT * FROM EMPLOYEE;

SELECT
E.JOB_ID,
J.DESIGNATION,
COUNT(E.JOB_ID) AS NO_OF_EMPLOYEES
FROM JOB J INNER JOIN EMPLOYEE E ON J.JOB_ID = E.Job_ID
GROUP BY E.JOB_ID, J. DESIGNATION;

/*8) HOW MANY EMPLOYEES ARE WORKING IN "NEW YORK".*/


SELECT * FROM EMPLOYEE;
SELECT * FROM LOCATION;
SELECT * FROM DEPARTMENT;

SELECT
L.CITY,
D.LOCATION_ID,
E.DEPARTMENT_ID,
COUNT(D.LOCATION_ID) AS TOTAL_EMPLOYEES
FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON D.DEPARTMENT_ID = E.Department_ID
INNER JOIN LOCATION L ON L.LOCATION_ID = D.LOCATION_ID
GROUP BY D.LOCATION_ID, L.CITY, E.DEPARTMENT_ID
HAVING L.CITY = 'NEW YORK';

SELECT
L.CITY,
D.LOCATION_ID,
E.DEPARTMENT_ID,
COUNT(D.LOCATION_ID) AS TOTAL_EMPLOYEES
FROM DEPARTMENT D INNER JOIN EMPLOYEE E ON D.DEPARTMENT_ID = E.Department_ID
INNER JOIN LOCATION L ON L.LOCATION_ID = D.LOCATION_ID
GROUP BY D.LOCATION_ID, L.CITY, E.DEPARTMENT_ID
HAVING L.CITY = 'DALLAS';

/*9)DISPLAY THE EMPLOYEE DETAILS WITH SALARY GRADES.*/


SELECT * FROM EMPLOYEE;

SELECT
FIRST_NAME+' '+MIDDLE_NAME+' '+LAST_NAME AS FULLNAME,
SALARY,
CASE
WHEN SALARY BETWEEN 0 AND 800 THEN 'GRADE1'
WHEN SALARY BETWEEN 801 AND 1300 THEN 'GRADE2'
WHEN SALARY BETWEEN 1301 AND 2199 THEN 'GRADE4'
WHEN SALARY BETWEEN 2200 AND 2749 THEN 'GRADE3'
ELSE 'GRADE5'
END AS GRADE
FROM EMPLOYEE;

/*10)LIST OUT THE NO. OF EMPLOYEES ON GRADE WISE.*/


WITH CTE AS
(

SELECT
FIRST_NAME+' '+MIDDLE_NAME+' '+LAST_NAME AS FULLNAME,
SALARY,
CASE
WHEN SALARY BETWEEN 0 AND 800 THEN 'GRADE1'
WHEN SALARY BETWEEN 801 AND 1300 THEN 'GRADE2'
WHEN SALARY BETWEEN 1301 AND 2199 THEN 'GRADE4'
WHEN SALARY BETWEEN 2200 AND 2749 THEN 'GRADE3'
ELSE 'GRADE5'
END AS GRADE
FROM EMPLOYEE
)
SELECT GRADE, COUNT(GRADE) AS TOTAL_GRADE FROM CTE
GROUP BY GRADE;
/*QUESTION 11 TO 15 INADEQUATE DATA, NO GRADE TABLE AND MANAGER TABLE*/

/*16)DISPLAY ALL EMPLOYEES IN SALES OR OPERATION DEPARTMENTS.*/


SELECT * FROM EMPLOYEE;
SELECT * FROM DEPARTMENT;
SELECT * FROM EMPLOYEE E INNER JOIN DEPARTMENT D
ON E.Department_ID = D.DEPARTMENT_ID
WHERE D.NAME IN ('SALES', 'OPERATIONS');

-- SET OPERATORS
/*1) LIST OUT THE DISTINCT JOBS IN SALES AND ACCOUNTING DEPARTMENTS.*/

SELECT * FROM JOB


WHERE JOB_ID IN (SELECT JOB_ID FROM EMPLOYEE
WHERE Department_ID = (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'SALES'))
UNION
SELECT * FROM JOB
WHERE JOB_ID IN (SELECT JOB_ID FROM EMPLOYEE
WHERE Department_ID = (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'ACCOUNTING'));

SELECT
J.JOB_ID,
J.DESIGNATION,
D.DEPARTMENT_ID,
D.NAME
FROM EMPLOYEE E INNER JOIN JOB J ON J.JOB_ID = E.Job_ID
INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID =
E.Department_ID
WHERE D.NAME='SALES'
UNION
SELECT
J.JOB_ID,
J.DESIGNATION,
D.DEPARTMENT_ID,
D.NAME
FROM EMPLOYEE E INNER JOIN JOB J ON J.JOB_ID = E.Job_ID
INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID =
E.Department_ID
WHERE D.NAME='ACCOUNTING';
/*2) LIST OUT ALL THE JOBS IN SALES AND ACCOUNTING DEPARTMENTS.*/
SELECT * FROM JOB
WHERE JOB_ID IN (SELECT JOB_ID FROM EMPLOYEE
WHERE Department_ID = (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'SALES'))
UNION ALL
SELECT * FROM JOB
WHERE JOB_ID IN (SELECT JOB_ID FROM EMPLOYEE
WHERE Department_ID = (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'ACCOUNTING'));

SELECT
J.JOB_ID,
J.DESIGNATION,
D.DEPARTMENT_ID,
D.NAME
FROM EMPLOYEE E INNER JOIN JOB J ON J.JOB_ID = E.Job_ID
INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID =
E.Department_ID
WHERE D.NAME='SALES'
UNION ALL
SELECT
J.JOB_ID,
J.DESIGNATION,
D.DEPARTMENT_ID,
D.NAME
FROM EMPLOYEE E INNER JOIN JOB J ON J.JOB_ID = E.Job_ID
INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID =
E.Department_ID
WHERE D.NAME='ACCOUNTING';

/*3) LIST OUT THE COMMON JOBS IN RESEARCH AND ACCOUNTING DEPARTMENTS IN
ASCENDING ORDER.*/

SELECT
J.JOB_ID,
J.DESIGNATION
FROM EMPLOYEE E INNER JOIN JOB J ON J.JOB_ID = E.Job_ID
INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID =
E.Department_ID
WHERE D.NAME='RESEARCH'
INTERSECT
SELECT
J.JOB_ID,
J.DESIGNATION
FROM EMPLOYEE E INNER JOIN JOB J ON J.JOB_ID = E.Job_ID
INNER JOIN DEPARTMENT D ON D.DEPARTMENT_ID =
E.Department_ID
WHERE D.NAME='ACCOUNTING'
ORDER BY J.JOB_ID;
SELECT * FROM EMPLOYEE;
SELECT * FROM JOB;
SELECT * FROM DEPARTMENT;

--SUB QUERIES
/*1)DISPLAY THE EMPLOYEES LIST WHO GOT THE MAXIMUM SALARY.*/
SELECT * FROM EMPLOYEE;
SELECT * FROM EMPLOYEE
WHERE SALARY IN (SELECT MAX(SALARY) FROM EMPLOYEE);

/*2)DISPLAY THE EMPLOYEES WHO ARE WORKING IN SALES DEPARTMENT.*/


SELECT * FROM EMPLOYEE
WHERE Department_ID IN (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'SALES');

/*3)DISPLAY THE EMPLOYEES WHO ARE WORKING AS 'CLERCK'.*/


SELECT * FROM EMPLOYEE
WHERE JOB_ID IN (SELECT JOB_ID FROM JOB WHERE DESIGNATION = 'CLERK');

/*4)DISPLAY THE LIST OF EMPLOYEES WHO ARE LIVING IN "NEW YORK".*/


SELECT * FROM EMPLOYEE
WHERE Department_ID IN (SELECT Department_ID FROM DEPARTMENT
WHERE LOCATION_ID IN (SELECT LOCATION_ID FROM LOCATION
WHERE CITY = 'NEW YORK'));

/*5)FIND OUT NO. OF EMPLOYEES WORKING IN "SALES" DEPARTMENT.*/


SELECT * FROM EMPLOYEE;
SELECT * FROM DEPARTMENT;

SELECT COUNT(EMPLOYEE_ID) AS NO_OF_EMPLOYEES_FROM_SALES FROM EMPLOYEE


WHERE DEPARTMENT_ID IN (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'SALES');

/*6)UPDATE THE EMPLOYEES SALARIES, WHO ARE WORKING AS CLERK ON THE BASIS OF 10%.*/
UPDATE EMPLOYEE
SET SALARY = (SALARY+SALARY*0.1)
WHERE JOB_ID IN (SELECT JOB_ID FROM JOB
WHERE DESIGNATION = 'CLERK');

/*7) DELETE THE EMPLOYEES WHO ARE WORKING IN ACCOUNTING DEPARTMENT.*/


DELETE FROM EMPLOYEE
WHERE Department_ID IN (SELECT Department_ID FROM DEPARTMENT
WHERE NAME = 'ACCOUNTING');

/*8) DISPLAY THE SECOND HIGHEST SALARY DRAWING EMPLOYEE DETAILS.*/


SELECT MAX(SALARY) AS SECOND_HIGHEST_SALARY FROM EMPLOYEE
WHERE SALARY < (SELECT MAX(SALARY) FROM EMPLOYEE);

/*9)DISPLAY THE N'TH HIGHEST SALARY DRAWING EMPLOYEE DETAILS.*/


--LET THE NTH VALUE BE 3
SELECT * FROM EMPLOYEE
WHERE SALARY = (SELECT TOP 1 SALARY FROM
(SELECT TOP 3 SALARY FROM EMPLOYEE
ORDER BY SALARY DESC)
AS NTH_HIGHEST_SALARY);

/*10)LIST OUT THE EMPLOYEES WHO EARN MORE THAN EVERY EMPLOYEE IN DEPARTMENT 30.*/
SELECT DEPARTMENT_ID, SALARY FROM EMPLOYEE;

SELECT * FROM EMPLOYEE


WHERE SALARY > (SELECT MAX(SALARY) FROM EMPLOYEE WHERE Department_ID = 30);

/*11)LIST OUT THE EMPLOYEES WHO EARN MORE THAN THE LOWEST SALARY IN DEPARTMENT
30.*/
SELECT * FROM EMPLOYEE
WHERE SALARY > (SELECT MIN(SALARY) FROM EMPLOYEE WHERE Department_ID = 30);

/*12)FIND OUT WHOSE DEPARTMENT HAS NOT EMPLOYEES.*/

SELECT * FROM DEPARTMENT


WHERE DEPARTMENT_ID NOT IN (SELECT DEPARTMENT_ID FROM EMPLOYEE);

/*13)FIND OUT WHICH DEPARTMENT DOES NOT HAVE ANY EMPLOYEES.*/


SELECT * FROM DEPARTMENT
WHERE DEPARTMENT_ID NOT IN (SELECT DEPARTMENT_ID FROM EMPLOYEE);

/*14)FIND OUT THE EMPLOYEES WHO EARN


GREATER THAN THE AVERAGE SALARY FOR THEIR DEPARTMENT.*/

SELECT * FROM EMPLOYEE WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE)
GROUP BY Department_ID;
SELECT
EMPLOYEE_ID,
FIRST_NAME,
MIDDLE_NAME,
LAST_NAME,
SALARY,
EMPLOYEE. Department_ID,
DEPT_AVG.AVG_SALARY
FROM EMPLOYEE
JOIN
(
SELECT DEPARTMENT_ID, AVG(SALARY) AS AVG_SALARY FROM EMPLOYEE
GROUP BY Department_ID
) AS DEPT_AVG ON EMPLOYEE.Department_ID = DEPT_AVG.Department_ID
WHERE EMPLOYEE.Salary > DEPT_AVG.AVG_SALARY;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy