0% found this document useful (0 votes)
39 views

Case Study 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Case Study 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

-----------------------------------------------------------------------------------

----------------------------
-------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 First Name, Last Name, Salary, Commission for all Employees

SELECT FIRST_NAME,LAST_NAME,SALARY,COMM AS Commission FROM EMPLOYEE

--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".

SELECT EMPLOYEE_ID AS [ID of the Employee],LAST_NAME AS [Name of the


Employee],DEPARTMENT_ID AS Dep_id FROM EMPLOYEE

---7. List out the annual salary of the employees with their names only.

SELECT LAST_NAME,Salary * 12 AS Annual_salary FROM EMPLOYEE

-------------------------------------------WHERE Condition:
---1. List the details about "Smith".

SELECT * FROM EMPLOYEE where LAST_NAME='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 2000 and 3000.

SELECT * FROM EMPLOYEE WHERE Salary BETWEEN '2000' AND '3000'

---4. List out the employees who are working in department 10 or 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 'L'.

SELECT * FROM EMPLOYEE WHERE LEFT(LAST_NAME,1)='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'.

SELECT * FROM EMPLOYEE WHERE LEFT(LAST_Name,1)='J' AND LEN(Last_Name)=4

--9. List out the employees who are working in department 30 and draw the salaries
more than 2500.

SELECT * FROM EMPLOYEE Where DEPARTMENT_ID=30 AND SALARY> 2500

---10. List out the employees who are not receiving commission

SELECT * FROM EMPLOYEE WHERE COMM IS NULL

----------------------------------------------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.

SELECT EMPLOYEE_ID,LAST_NAME 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 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

--------------------------------------------------GROUP BY and HAVING Clause


-----1. List out the department wise maximum salary, minimum salary and average
salary of the employees

SELECT DEPARTMENT_ID,MAX(SALARY) MAX_Salary, MIN (SALARY) AS MIN_Salary,


AVG(SALARY) AS AVG_Salary FROM EMPLOYEE
GROUP BY DEPARTMENT_ID

---2. List out the job wise maximum salary, minimum salary and average salary of
the employees

SELECT JOB_ID,MAX(SALARY) MAX_Salary, MIN (SALARY) AS MIN_Salary, AVG(SALARY) AS


AVG_Salary FROM EMPLOYEE
GROUP BY JOB_ID

---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.

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, Hire_Date), 0) AS


join_month_year,COUNT(*) AS num_employees FROM EMPLOYEE
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Hire_Date), 0)
ORDER BY join_month_year;

---5. List out the Department ID having at least four employees.


SELECT DEPARTMENT_ID,Count(EMPLOYEE_ID) Count_EMP FROM EMPLOYEE GROUP BY
DEPARTMENT_ID HAVING Count(EMPLOYEE_ID)>=4

---6. How many employees joined in February month.

SELECT Count(EMPLOYEE_ID) AS Count_EMP FROM EMPLOYEE WHERE MONTH(HIRE_DATE)=02

---7. How many employees joined in May or June month.


SELECT Count(EMPLOYEE_ID) AS Count_EMP FROM EMPLOYEE Where MONTH(HIRE_DATE) in
(5,6)

---8. How many employees joined in 1985


SELECT Count(EMPLOYEE_ID) AS Count_EMP FROM EMPLOYEE Where YEAR(HIRE_DATE) =1985

--9. How many employees joined each month in 1985


SELECT MONTH(HIRE_DATE) MONTH,Count(EMPLOYEE_ID) AS Count_EMP FROM EMPLOYEE Where
YEAR(HIRE_DATE) =1985
GROUP BY MONTH(HIRE_DATE)

---10. How many employees were joined in April 1985?


SELECT MONTH(HIRE_DATE) MONTH,Count(EMPLOYEE_ID) AS Count_EMP FROM EMPLOYEE Where
YEAR(HIRE_DATE) =1985 AND MONTH(HIRE_DATE)=04
GROUP BY MONTH(HIRE_DATE)

---11. Which is the Department ID having greater than or equal to 3 employees


joining in April 1985

SELECT DEPARTMENT_ID FROM EMPLOYEE WHERE YEAR(HIRE_DATE) =1985 AND


MONTH(HIRE_DATE)=04
GROUP BY DEPARTMENT_ID
HAVING COUNT(EMPLOYEE_ID) >=3

--------------------------------------------------------Joins:
---1. List out employees with their department names.

SELECT E.EMPLOYEE_ID,E.LAST_NAME EMP_Name,D.Name AS Department_Names FROM EMPLOYEE


E INNER JOIN DEPARTMENT D ON D.Department_Id=E.DEPARTMENT_ID

---2. Display employees with their designations.

SELECT E.EMPLOYEE_ID,E.LAST_NAME EMP_Name,D.Designation FROM EMPLOYEE E INNER JOIN


JOB D ON D.Job_ID=E.JOB_ID

---3. Display the employees with their department names and city.

SELECT E.EMPLOYEE_ID,E.LAST_NAME EMP_Name,D.Name AS Department_Names,L.City FROM


EMPLOYEE E INNER JOIN DEPARTMENT D ON D.Department_Id=E.DEPARTMENT_ID INNER JOIN
Location L ON L.Location_ID=D.Location_Id

---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

---5. How many employees are working in the sales department?


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
WHERE D.Name='Sales'
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.

SELECT D.Name Department,Count(EMPLOYEE_ID) Count_EMP FROM EMPLOYEE E INNER JOIN


DEPARTMENT D ON D.Department_Id=E.DEPARTMENT_ID
GROUP BY D.Name
HAVING COUNT(EMPLOYEE_ID) >=3
ORDER BY D.Name ASC

--7. How many employees are working in 'Dallas'

SELECT L.City,Count(EMPLOYEE_ID) Count_EMP FROM EMPLOYEE E INNER JOIN DEPARTMENT D


ON D.Department_Id=E.DEPARTMENT_ID INNER JOIN Location L ON
L.Location_ID=D.Location_Id
WHERE L.City='Dallas'
GROUP BY L.City

---8. Display all employees in sales or operation departments.

SELECT E.* FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON


D.Department_Id=E.DEPARTMENT_ID
WHERE D.Name='Operations'

----------------------------------------------------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.

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
WHERE SALARY BETWEEN '2000' AND '5000'
GROUP BY 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')

--3. Display the employees who are working as 'Clerk'.


SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY,JOB_ID FROM EMPLOYEE
WHERE JOB_ID = (SELECT JOB_ID FROM JOB Where Designation='Clerk')

---4. Display the list of employees who are living in 'Boston'.


SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME FROM EMPLOYEE E
INNER JOIN DEPARTMENT D ON D.Department_Id=E.DEPARTMENT_ID
WHERE D.Location_Id= (SELECT Location_Id FROM Location Where City='Boston')

---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')

----7. Display the second highest salary drawing employee details.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME,SALARY FROM EMPLOYEE WHERE SALARY=(SELECT


MAX(SALARY) FROM EMPLOYEE WHERE SALARY <(SELECT MAX(salary)FROM employee))

---8. List out the employees who earn more than every employee in department 30

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME,SALARY,DEPARTMENT_ID FROM EMPLOYEE WHERE


SALARY=(SELECT MAX(SALARY) FROM EMPLOYEE WHERE DEPARTMENT_ID =30)

----9. Find out which department has no employees.


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

-----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)

You might also like