0% found this document useful (0 votes)
46 views18 pages

Employee Queries

Uploaded by

Prasad Dhumale
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)
46 views18 pages

Employee Queries

Uploaded by

Prasad Dhumale
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/ 18

1. Display all the details of all employees working in the company.

select * from employee;

2. Display ssn, lname, fname, address of employees who work in


department no 5.
select ssn,lname,fname,address from employee where dno=5;

3. Retrieve the birthdate and address of the employee whose name is


'Franklin T. Wong'
select bdate, address from employee where fname="franklin" and minit="t"
and lname="wong";

4. Retrieve the name and salary of every employee


select fname,salary from employee;
5. Retrieve all distinct salary values

select distinct salary from employee;

6.Retrieve all employee names whose address is in ‘Bellaire’

select fname from employee where address like "%bellaire%";

7. Retrieve all employees who were born during the 1950s

select fname from employee where bdate like "195%";

8. Retrieve all employees in department 5 whose salary is between


50,000 and 60,000(inclusive)
select fname,salary from employee where dno=5 and (salary>=25000 and
salary<=30000);
9. Retrieve the names of all employees who do not have supervisors
select fname from employee where superssn is null;

select fname from employee e where not exists (select fname from employee a
where a.ssn=e.superssn);

10. Retrieve SSN and department name for all employees


select ssn,dname from employee , department where dno=dnumber;

11. Retrieve the name and address of all employees who work for the
'Research' Department
select fname,minit,lname from employee,department where
dname="research" and department.dnumber=employee.dno;

12. For every project located in 'Stafford', list the project number, the
controlling department number, and the department manager's last
name, address, and birthdate.
Select pnumber, dnum, lname, address,bdate from project, department,
employee where plocation="Stafford" and dnum=dnumber and mgrssn=ssn;

13. For each employee, retrieve the employee's name, and the name of
his or her immediate supervisor.
select f.fname as employee,e.fname as supervisor from employee e,employee f
where e.ssn=f.superssn ;

14. Find the maximum salary, the minimum salary, and the average
salary among all employees.

SELECT MAX(SALARY), MIN (SALARY), AVG (SALARY)

FROM EMPLOYEE;

15. Find the maximum salary, the minimum salary, and the average
salary among employees who work for the 'Research' department.
SELECT MAX (SALARY), MIN(SALARY), AVG(SALARY)

FROM EMPLOYEE, DEPARTMENT

WHERE DNO=DNUMBER AND DNAME='Research' ;


16. Retrieve the total number of employees in the company.
SELECT COUNT (*)

FROM EMPLOYEE

17. Retrieve the number of employees in the 'Research' department


SELECT COUNT(*)

FROM EMPLOYEE, DEPARTMENT

WHERE DNO=DNUMBER AND DNAME=“Research”

18. Increase the salary of all employees by 1000 rs.


SELECT FNAME, LNAME, 1000+SALARY as increment from employee;
Increase the salary of employees who are working on “producty” by 15%.
select fname, salary+1000 from employee,works_on,project where
pname="producty" and pnumber=pno and essn=ssn;

Make a list of all project numbers for projects that involve an employee
whose last name is 'Smith' as a worker or as a manager of the department
that controls the project.

Make a list of all project numbers for projects that involve an employee
whose last name is 'wong' as a worker or as a manager of the department
that controls the project.
(SELECT PNUMBER

FROM PROJECT, DEPARTMENT, EMPLOYEE

WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME='wong')

UNION

(SELECT PNUMBER

FROM PROJECT, WORKS_ON, EMPLOYEE

WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME='wong');

Retrieve all employees in department 5 whose salary is between


£30,000 and £40,000
select * from employee where salary between 30000 and 40000 and
dno=5;

Retrieve the name of each employee who has a dependent with the same
first name and same sex as the employee

select e.fname, e.lname from employee e where e.ssn in (s

elect essn from dependent where e.fname=dependent_name and


e.sex=sex);

alternate:

select e.fname,e.lname from employee e, dependent d where


e.ssn=d.essn and e.sex=d.sex and e.fname=d.dependent_name;

Retrieve the social security numbers of all employees who work on project
number 1, 2 or 3
SELECT DISTINCT ESSN
FROM WORKS_ON
WHERE PNO IN (1,2,3);

For each department, retrieve the department number, the number of


employees in the department, and their average salary
SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO;

For each project, retrieve the project number, the project name, and the
number of employees who work on that
Project
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER = PNO
GROUP BY PNUMBER, PNAME;

For each project on which more than two employees work, retrieve the
project number, the project name, and the
number of employees who work on the project
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER = PNO
GROUP BY PNUMBER, PNAME
HAVING COUNT (*) > 2;

For each project, retrieve the project number, the project name, and the
number of employees from department 5 who
work on the project
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER = PNO AND SSN = ESSN AND DNO = 5
GROUP BY PNUMBER, PNAME;

DISPLAY THE SSN OF EMPLOYEE WHO HAS GOT MAXIMUM SALARY


select ssn from employee where salary in (select max(salary) from employee);

Retrieve the names of all employees who have two or more dependents

List the names of managers who have at least one dependent


Select all combination of EMPLOYEE SSN and DEPARTMENT DNAME in the database

Select the CROSS PRODUCT of the EMPLOYEE and DEPARTMENT relations


Retrieve all employees whose address is in Houston, Texas

15 Retrieve a list of employees and the projects they are working on,
ordered by department and, within each department, ordered alphabetically by last name,
first name
Retrieve the name of each employee who has a dependent with the

same first name and same sex as the employee


17 Retrieve the social security numbers of all employees who work on project number 1, 2 or 3

28A For each department that has more than 1 employee, retrieve the department
number and the number of its employees who are making more than RS 20,000

Display fname, lname and address of employees who are working in


research department.(USING JOINS)

SELECT Fname, Lname, Address FROM (EMPLOYEE JOIN


DEPARTMENT ON Dno = Dnumber) WHERE Dname = 'Research';
Display the names of employees, whose salary is greater than the salary
of all the employees in the department 5.

select fname,lname,salary from employee where salary > all(select


salary from employee where dno=5);

Display the names of employees, whose salary is greater than the


average salary of all the employees in the department 5.

select fname,lname,salary from employee where salary > al

l(select salary from employee where dno=5);

Display the list of all the employees who work in the same (project,
hours) combination of employee whose ssn=333445555.

select distinct essn from works_on where (pno,hours) in (select


pno,hours from works_on where essn=333445555);
VIEWS:

CREATE VIEW Syntax


Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database. We can create a view by selecting fields from one or more tables present
in the database. A View can either have all the rows of a table or specific rows based on certain
condition.

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE condition;

view_name: Name for the View


table_name: Name of the table
condition: Condition to select rows

CREATE VIEW EMP AS SELECT * FROM EMPLOYEE WHERE DNO=5;

SELECT * FROM EMP;


Rollback, commit :

You might also like