SQL Case Study 2
SQL Case Study 2
USE CaseStudy2
City VARCHAR(50));
(123, 'Dallas'),
(124, 'Chicago'),
(167, 'Boston');
Name VARCHAR(50),
Location_Id INT,
);
Designation VARCHAR(50));
-- CREATE TABLE JOB
-- DESIGNATION VARCHAR(20))
(667, 'CLERK'),
(668,'STAFF'),
(669,'ANALYST'),
(670,'SALES_PERSON'),
(671,'MANAGER'),
(672, 'PRESIDENT')
(EMPLOYEE_ID INT,
LAST_NAME VARCHAR(20),
FIRST_NAME VARCHAR(20),
MIDDLE_NAME CHAR(1),
REFERENCES JOB(JOB_ID),
MANAGER_ID INT,
HIRE_DATE DATE,
SALARY INT,
COMM INT,
REFERENCES DEPARTMENT(DEPARTMENT_ID))
(7369,'SMITH','JOHN','Q',667,7902,'17-DEC-84',800,NULL,20),
(7499,'ALLEN','KEVIN','J',670,7698,'20-FEB-84',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)
-- SIMPLE QUERIES:
-- 5. List out the First Name, Last Name, Salary, Commission for all Employees.
-- 7. List out the annual salary of the employees with their names only.
-- WHERE CONDITION:
WHERE DEPARTMENT_ID = 20
-- 3. List out the employees who are earning salaries between 3000 and4500.
-- 7. List out the employees whose name starts with 'S' and ends with 'H'.
-- 8. List out the employees whose name length is 4 and start with 'S'.
-- 9. List out employees who are working in department 10 and draw salaries more than 3500.
-- 1. List out the Employee ID and Last Name in ascending order based on the Employee ID.
ORDER BY EMPLOYEE_ID
-- 2. List out the Employee ID and Name in descending order based on salary.
-- 3. List out the employee details according to their Last Name in ascending-order.
-- 4. List out the employee details according to their Last Name in ascending order and then
Department ID in descending order.
ORDER BY LAST_NAME
-- 2. List out the department wise maximum salary, minimum salary and average salary of the
employees.
GROUP BY DEPARTMENT_ID
-- 3. List out the job wise maximum salary, minimum salary and average salary of the employees.
GROUP BY JOB_ID
-- 4. List out the number of employees who joined each month in ascendingorder.
GROUP BY HIRE_DATE
ORDER BY MONTH(HIRE_DATE)
-- 5. List out the number of employees for each month and year in ascending order based on the
year and month.
GROUP BY Department_ID
HAVING COUNT(Employee_ID)>=4
GROUP BY MONTH(HIRE_DATE)
HAVING MONTH(HIRE_DATE) = 1
GROUP BY MONTH(HIRE_DATE)
GROUP BY YEAR(HIRE_DATE)
ORDER BY MONTH(HIRE_DATE)
-- 12. Which is the Department ID having greater than or equal to 3 employees joining in April 1985?
-- JOINS
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
JOIN
JOB AS J
ON J.Job_ID = E.JOB_ID
-- 3. Display the employees with their department names and regional groups.
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
JOIN
LOCATION AS L
ON D.Location_Id = L.Location_ID
-- 4. How many employees are working in different departments? Display with department names.
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
-- 6. Which is the department having greater than or equal to 5 employees? Display the department
names in ascending order.
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
HAVING COUNT(EMPLOYEE_ID)>=5
-- 7. How many jobs are there in the organization? Display with designations.
JOIN
JOB AS J
ON E.JOB_ID = J.Job_ID
GROUP BY j.Designation
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
JOIN
LOCATION AS L
ON D.Location_Id = L.Location_ID
GROUP BY L.City
-- 9. Display the employee details with salary grades. Use conditional statement to create a grade
column.
SELECT *,
CASE
ELSE 'HIGH'
END
AS SALARY_CATEGORY
FROM EMPLOYEE
-- 10. List out the number of employees grade wise. Use conditional statement to create a grade
column.
( SELECT Employee_ID,SalGrade=
CASE
ELSE 'A'
END
FROM Employee ) AS E
GROUP BY E.SalGrade
CASE
ELSE 'HIGH'
END
AS SALARY_CATEGORY
FROM EMPLOYEE
-- 11. Display the employee salary grades and the number of employees between 2000 to 5000
range of salary.
( SELECT Employee_ID,SalGrade=
CASE
END
FROM Employee
GROUP BY E.SalGrade
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
-- SET OPERATORS:
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
INNER JOIN
DEPARTMENT AS D
ON E.DEPARTMENT_ID = D.Department_Id
-- SUB QUERIES:
WHERE DEPARTMENT_ID =
(SELECT DEPARTMENT_ID FROM DEPARTMENT WHERE Location_Id =
WHERE DEPARTMENT_ID =
-- 6. Update the salaries of employees who are working as clerks on the basis of 10%.
RETURNS INT
AS
BEGIN
RETURN @Salary*(1+@increament)
END;
WHERE JOB_ID =
DELETE EMPLOYEE
WHERE DEPARTMENT_ID =
WHERE R=2
RETURNS TABLE
R FROM Employee ) AS RT
WHERE R=@NPOS)
-- 10. List out the employees who earn more than every employee in department 30.
Department_ID)
-- 11. List out the employees who earn more than the lowest salary in department. (Why am I getting
only one department Id)
Department_ID)
SELECT NAME FROM DEPARTMENT WHERE Department_Id NOT IN (SELECT Department_Id FROM
EMPLOYEE)
-- 13. Find out the employees who earn greater than the average salary for their department. (Why
am I getting only one department Id)
WITH AvgSal AS
GROUP BY DEPARTMENT_ID)
JOIN
EMPLOYEE AS E
ON AvS.DEPARTMENT_ID = E.DEPARTMENT_ID