Case Study 2
Case Study 2
----------------------------
-------Simple Queries:
--5. List out the First Name, Last Name, Salary, Commission for all Employees
--6. List out the Employee ID, Last Name, Department ID for all employees and alias
Employee ID as "ID of the Employee", Last Name as "Name of the Employee",
Department ID as "Dep_id".
---7. List out the annual salary of the employees with their names only.
-------------------------------------------WHERE Condition:
---1. List the details about "Smith".
---2. List out the employees who are working in department 20.
---3. List out the employees who are earning salary between 2000 and 3000.
---4. List out the employees who are working in department 10 or 20.
---5. Find out the employees who are not working in department 10 or 30
---6. List out the employees whose name starts with 'L'.
--7. List out the employees whose name starts with 'L' and ends with 'E'.
SELECT * FROM EMPLOYEE WHERE LEFT(LAST_NAME,1)='L' AND RIGHT(LAST_NAME,1)='E'
--- 8. List out the employees whose name length is 4 and start with 'J'.
--9. List out the employees who are working in department 30 and draw the salaries
more than 2500.
---10. List out the employees who are not receiving commission
----------------------------------------------ORDER BY Clause:
--1. List out the Employee ID and 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 and Name in descending order based on salary.
----3. List out the employee details according to their Last Name in ascending-
order.
SELECT * FROM EMPLOYEE ORDER BY LAST_NAME ASC
---4.List out the employee details according to their Last Name in ascending order
and then Department ID in descending order.
SELECT * FROM EMPLOYEE ORDER BY LAST_NAME ASC ,EMPLOYEE_ID DESC
---2. List out the job wise maximum salary, minimum salary and average salary of
the employees
---3. List out the number of employees who joined each month in ascending order.
SELECT HIRE_DATE,COUNT(EmploYEE_ID) Count_Emp FROM EMPLOYEE
GROUP BY HIRE_DATE
ORDER BY HIRE_DATE ASC
---4. List out the number of employees for each month and year in ascending order
based on the year and month.
--------------------------------------------------------Joins:
---1. List out employees with their department names.
---3. Display the employees with their department names and city.
---4. How many employees are working in different departments? Display with
department names.
SELECT D.Name AS Department_Names,Count(EMPLOYEE_ID) Count_EMP FROM EMPLOYEE E
INNER JOIN DEPARTMENT D ON D.Department_Id=E.DEPARTMENT_ID
GROUP BY D.Name
---6. Which is the department having greater than or equal to 3 employees and
display the department names in ascending order.
----------------------------------------------------CONDITIONAL STATEMENT
------1. Display the employee details with salary grades. Use conditional statement
to create a grade column.
SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY,
CASE WHEN SALARY >= 1000 THEN 'A'
WHEN SALARY >= 800 AND salary < 1000 THEN 'B'
WHEN SALARY >= 600 AND salary < 800 THEN 'C'
WHEN SALARY >= 400 AND salary < 600 THEN 'D'
ELSE 'E' END AS GRADES
FROM EMPLOYEE;
----2. List out the number of employees grade wise. Use conditional statement to
create a grade column.
SELECT CASE WHEN SALARY >= 1000 THEN 'A'
WHEN SALARY >= 800 AND salary < 1000 THEN 'B'
WHEN SALARY >= 600 AND salary < 800 THEN 'C'
WHEN SALARY >= 400 AND salary < 600 THEN 'D' ELSE 'E' END AS GRADES
,Count(EMPLOYEE_ID) COUNT_EMP
FROM EMPLOYEE
GROUP BY SALARY
---3. Display the employee salary grades and the number of employees between 2000
to 5000 range of salary.
------------------------------------------------------Subqueries:
----1. Display the employees list who got the maximum salary
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEE
WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEE)
---2. Display the employees who are working in the sales department.
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY,DEPARTMENT_ID FROM EMPLOYEE
WHERE DEPARTMENT_ID = (SELECT DEPARTMENT_ID FROM DEPARTMENT Where Name='Sales')
---5. Find out the number of employees working in the sales department.
SELECT Count(EMPLOYEE_ID) Count_EMP,DEPARTMENT_ID FROM EMPLOYEE
WHERE DEPARTMENT_ID = (SELECT DEPARTMENT_ID FROM DEPARTMENT Where Name='Sales')
GROUP BY DEPARTMENT_ID
----6. Update the salaries of employees who are working as clerks on the basis of
10%.
UPDATE EMPLOYEE SET SALARY=SALARY*1.1
WHERE JOB_ID = (SELECT JOB_ID FROM JOB Where Designation='Clerk')
---8. List out the employees who earn more than every employee in department 30
-----10. Find out the employees who earn greater than the average salary for their
department
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME,SALARY,DEPARTMENT_ID FROM EMPLOYEE E
WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE WHERE
DEPARTMENT_ID=E.DEPARTMENT_ID)