Case Study 2 Solution
Case Study 2 Solution
Case Study 2 Solution
--SIMPLE QUERIES
/*1) LIST ALL THE EMPLOYEE DETAILS.*/
SELECT * 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"*/
/*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___';
/*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.*/
/*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;
SELECT
DEPARTMENT_ID,
MIN(SALARY) AS MIN_SALARY,
MAX(SALARY) AS MAX_SALARY,
AVG(SALARY) AS AVG_SALARY
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID;
/*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;
--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;
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;
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';
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;
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;
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';
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
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*/
-- SET OPERATORS
/*1) LIST OUT THE DISTINCT JOBS IN SALES AND ACCOUNTING DEPARTMENTS.*/
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);
/*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');
/*10)LIST OUT THE EMPLOYEES WHO EARN MORE THAN EVERY EMPLOYEE IN DEPARTMENT 30.*/
SELECT DEPARTMENT_ID, SALARY FROM EMPLOYEE;
/*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);
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;