0% found this document useful (0 votes)
218 views17 pages

SQL Queries for Employee and Department Analysis

This document contains the solutions to two exercises on SQL queries using sample employee and department database tables. Exercise 1 contains 14 queries to retrieve various data from the tables, such as employee names by salary, department names by starting or ending characters, and date calculations. Exercise 2 contains 10 queries using aggregation functions to get statistics like number of departments, average salaries by department, and employee counts by department and name. The output of each query is displayed.

Uploaded by

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

SQL Queries for Employee and Department Analysis

This document contains the solutions to two exercises on SQL queries using sample employee and department database tables. Exercise 1 contains 14 queries to retrieve various data from the tables, such as employee names by salary, department names by starting or ending characters, and date calculations. Exercise 2 contains 10 queries using aggregation functions to get statistics like number of departments, average salaries by department, and employee counts by department and name. The output of each query is displayed.

Uploaded by

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

Database Systems lab

Assessment -2

Name- Prianshu Goutam


Reg no-21BCE0400
Slot- L29 + L30
Faculty-Shashank Mouli Satapathy
EXERCISE -1
1) Find the employee names having salary greater than Rs.25000.
Query:
select first_name,last_name from employee
where salary >25000;

Output:

2) Find the employee names whose salary lies in the range between 30000 and
70000.
Query :
SELECT First_Name, Last_Name
FROM Employee
WHERE Salary >= 30000 AND Salary <= 70000;
Output:

3) Find the employees who have no supervisor.


Query:
SELECT e.First_Name, e.Last_Name
FROM Employee e
LEFT JOIN Employee s ON e.Supervisor_SSN = s.SSN_Number
WHERE s.SSN_Number IS NULL;

Output:
4) Display the bdate of all employee s in the format ‘DDthMonthYYYY’.
Query:
SELECT EXTRACT(DAY FROM birthday) || 'th' || TO_CHAR(birthday, 'MonthYYYY')
AS Birth_day
FROM Employee;

Output:

5) Display the employee names whose bdate is on or before 1978.


Query:
SELECT first_name || ' ' || mid_name || ' ' || last_name AS employee_name
FROM Employee
WHERE EXTRACT(YEAR FROM birthday) <= 1978;
Output:

6) Display the employee names having ‘salt lake’ in their address.


Query:
SELECT first_name || ' ' || mid_name || ' ' || last_name AS employee_name
FROM Employee
WHERE address LIKE '%Salt Lake%';

Output:

7) Display the department name that starts with ’M’.


Query:
SELECT department_name
FROM Dept
WHERE department_name LIKE 'M%';

Output:
8) Display the department names’ that ends with ‘E’.
Query:
SELECT department_name
FROM Dept
WHERE department_name LIKE '%e';

Output:

9) Display the names of all the employees having supervisor with any of the
following SSN 554433221, 333445555.
Query:
SELECT first_name || ' ' || mid_name || ' ' || last_name AS employee_name
FROM Employee
WHERE SSN_NUMBER in(554433221, 333445555);
Output:

10) Display all the department names in upper case and lower case
Query:
SELECT UPPER(department_name) AS department_name_upper,
LOWER(department_name) AS department_name_lower
FROM dept;

Output:

11)Display the first four characters and last four of the department names using
ltrim and rtrim.
Query:
SELECT LTRIM(SUBSTR(department_name, 1, 4)) AS first_four_chars,
RTRIM(SUBSTR(department_name, -4)) AS last_four_chars
FROM dept;

Output:

12) Display the substring of the Address (starting from 5th position to 11th
position) of all employees.
Query:
SELECT SUBSTR(Address, 5, 7) AS address_substring_5to11
FROM Employee;

Output:

13)Display the Mgrstartdate on adding three months to it.


Query:
SELECT MANAGERSTARTDATE + INTERVAL '3' MONTH AS
MgrStartDate_Plus_3_Months
FROM dept;
Output:

14)Display the age of all the employees rounded to two digits.


Query:
SELECT ROUND((SYSDATE - BIRTHDAY) / 365.25, 2) AS AGE
FROM Employee;

Output:
15)Find the last day and next day of the month in which each manager has
joined.
Query:
SELECT LAST_DAY(MANAGERSTARTDATE) AS
LAST_DAY_OF_MONTH, MANAGERSTARTDATE + INTERVAL '1' DAY
AS NEXT_DAY
FROM dept;

Output:
EXERCISE 2
For the relational schema given as part of Assessment – 1, write
the SQL queries using Group Functions to get the following
information.

1) How many different departments are there in the ‘employee’


table.

Query:

SELECT COUNT(DISTINCT DEPARTMENT_NUMBER) AS


DEPARTMENT_COUNT

FROM employee;

Output:

2) For each department display the minimum and maximum


employee salaries.

Query:
SELECT DEPARTMENT_NUMBER, MIN(SALARY) AS MIN_SALARY,

MAX(SALARY) AS MAX_SALARY

FROM Employee

GROUP BY DEPARTMENT_NUMBER;
Output:

3) Print the average annual salary.

Query:

SELECT AVG(SALARY) AS AVERAGE_SALARY

FROM Employee;

Output:

4) Count the number of employees over 30 age.

Query:

SELECT COUNT(*) AS employee_count FROM Employee

WHERE EXTRACT(YEAR FROM CURRENT_DATE) - EXTRACT(YEAR FROM BIRTHDAY) >


30;
Output:

5) Print the Department name and average salary of each


department.

Query:

SELECT d.DEPARTMENT_NAME, AVG(e.SALARY) AS average_salary FROM dept d

LEFT JOIN EMPLOYEE e ON d.DEPARTMENT_NUMBER = e.DEPARTMENT_NUMBER

GROUP BY d.DEPARTMENT_NAME;

Output:
6) Display the department name which contains more than 30
employees.

Query:

SELECT d.DEPARTMENT_NAME FROM dept d

JOIN EMPLOYEE e ON d.DEPARTMENT_NUMBER = e.DEPARTMENT_NUMBER

GROUP BY d.DEPARTMENT_NAME HAVING COUNT(*) > 30;

Output:

7) Calculate the average salary of employees by department and


age

Query:
SELECT DISTINCT DEPARTMENT_NUMBER, ROUND(MONTHS_BETWEEN(SYSDATE,BIRTHDAY)/12) AS
AGE_IN_YEARS, AVG(SALARY)

FROM EMPLOYEE GROUP BY DEPARTMENT_NUMBER, ROUND(MONTHS_BETWEEN(SYSDATE,


BIRTHDAY)/12) ORDER BY DEPARTMENT_NUMBER ASC;

Output:
8) Count separately the number of employees in the finance and
research department.

Query:

SELECT DEPARTMENT_NAME, COUNT(*) AS employee_count

FROM dept

JOIN EMPLOYEE ON dept.DEPARTMENT_NUMBER = EMPLOYEE.DEPARTMENT_NUMBER

WHERE DEPARTMENT_NAME IN ('Finance', 'Research')

GROUP BY DEPARTMENT_NAME;

Output:

9) List out the employees based on their seniority.

Query:

SELECT E.FIRST_NAME,E.LAST_NAME

FROM EMPLOYEE E

JOIN dept D ON E.DEPARTMENT_NUMBER = D.DEPARTMENT_NUMBER

ORDER BY D.managerstartdate ASC;


Output:

10)List out the employees who works in ‘manufacture’ department


group by first name.

Query:

SELECT E.FIRST_NAME, COUNT(*) AS EMPLOYEE_COUNT

FROM EMPLOYEE E

JOIN DEPT D ON E.DEPARTMENT_NUMBER = D.DEPARTMENT_NUMBER

WHERE D.DEPARTMENT_NAME = 'Manufacture'

GROUP BY E.FIRST_NAME;

Output:

You might also like