Vous êtes sur la page 1sur 6

MySQL 05

1) Create a database Department. Inside this database, create a table Empl having
the following structure.
Name of Column
Type
Eno
CHAR (4)
EName
VARCHAR (15)
Job
VARCHAR (15)
Mgr
INT(4)
Hiredate
DATE
Sal
FLOAT (10,2)
Comm
FLOAT (10,2)
Dept
INT(2)

ENO
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876

create database department;


use department;
create table empl
(eno char(4),
EName VARCHAR(15),
Job VARCHAR(15),
Mgr INT(4),
Hiredate Date,
Sal FLOAT(10,2),
Comm FLOAT(10,2),
Dept INT(2));

ENAME
SUNITA
ASHOK
ROHIT
JYOTI
MARTIN
BINOD
CHETAN
SUDIR
KAVITA
TUSHAR
ANAND

JOB
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
CHAIRMAN
SALESMAN
CLERK

MGR
7902
7698
7698
7839
7698
7839
7839
7566
NULL
7698
7788

HIREDATE
1980-12-17
1981-02-20
1981-02-22
1981-04-02
1981-09-28
1981-05-01
1981-06-09
1987-04-19
1981-11-17
1981-09-08
1987-05-23

SAL
2800
3600
5250
4975
6250
5850
2450
5000
5500
4500
3000

COMM
NULL
300
500
NULL
1400
NULL
NULL
NULL
NULL
0
NULL

DEPT
20
30
30
20
30
20
10
20
10
30
20

1. Populate table with the following records.


insert into empl values (7369,'SUNITA','CLERK',7902,'1980-1217',2800,NULL,20);
insert into empl values(7499,'ASHOK','SALESMAN',7698,'1981-0220',3600,300,30);
insert into empl values(7521,'ROHIT','SALESMAN',7698,'1981-0222',5250,500,30);
insert into empl values(7566,'JYOTI','MANAGER',7839,'1981-0402',4975,NULL,20);
insert into empl values(7654,'MARTIN'
,'SALESMAN',7698,'1981-0928',6250,1400,30);
insert into empl values(7698,'BINOD','MANAGER',7839,'1981-0501',5850,NULL,20);
insert into empl values(7782,'CHETAN','MANAGER',7839,'1981-0609',2450,NULL,10);
insert into empl values(7788,'SUDIR','ANALYST', 7566,'1987-0419',5000,NULL,20);
insert into empl values(7839,'KAVITA','CHAIRMAN',NULL,'1981-1117',5500,NULL,10);
insert into empl values(7844,'TUSHAR','SALESMAN',7698,'1981-0908',4500,0,30);
insert into empl values(7876,'ANAND','CLERK',7788,'1987-0523',3000,NULL,20);

2. Display all the records from table empl.

select * from empl;

3. To list the name and eno from the above table.

select Ename,eno from empl;

4. Display name, sal and sal added with comm from empl.

select Ename, sal, sal+comm from empl;

5. To display the name and the incremented value of sal as sal+300.

select ename, sal+300 as sal from empl;

6. To display name, salary and dept number who are not getting commission.

select ename, sal,dept from empl where comm is null ;

7. To display name, sal and annual salary who are getting commission.

select ename,sal*12 from empl;

8. To display eno, name of all employees whose department numbers are either
20 or 30

select eno, ename from empl where dept=20 or dept=30;


// or we can use
select eno, enmae from empl where dept in (20,30);

9. Write a query to display the name,job and department number of all employees in
deptno 20 and having job as manager.

select ename,job from empl where dept=20 and job='manager';

10. To list the salary between 3000 and 4000. First using AND logical operator and then
using BETWEEN operator.

select sal from empl where sal>3000 and sal <4000;


select sal from empl where sal between 3000 and 4000;

11. To list the name,manager which are in 7902, 7698, 7788.

select ename, mgr from empl where mgr in (7902,7698,7788);

12)Write a query to print names and salaries of all employees who obtain salary
in the range 1500 to 2500 both inclusive. This should be done in 2 ways.First using
AND logical operator and then using BETWEEN operator.

select ename , sal from empl where sal>1500 and sal<2500;


select ename , sal from empl where sal between 1500 and 2500;

13) To list the unique jobs from list.

select distinct (job) from empl;

14) To list the name and salary of all the employees who earn more than 1200
and are in department 10 or 30.

select ename, sal from empl where sal >1200 and dept=10 or
dept=30 ;

15) Write to query to display all fields of employees who are either analysts or managers
and deptno is 10. This should be done in 2 ways. First using OR logical operator and then
using IN operator.

select * from empl where job in ('manager','analyst') and dept= 10 ;


select * from empl where dept=10 and (job='manager' or
job='analyst') ;

16) List the department number not having duplicate values.

select distinct (dept) from empl;

17) Write a query to display all fields of employees who are not in deptno 30.This

should be done in 3 ways.Using <> operator,using NOT IN operator and logical


operator NOT.

select ename, dept from empl where dept < 30;


select ename, dept from empl where dept in ( 10,20);

18) List the details of all clerks who have not assigned commission as yet.

select * from empl where job='clerk' and dept is null;

19) List the details of all employees whose annual salary is between 25000-40000.

select * from empl where sal * 12 between 25000 and 40000;

20) How many job types are offered to employees.

select distinct job from empl;

21) Display eno,ename and job all employee in the ascending order of name.

select eno,ename,job from empl order by ename;

22) Display eno,ename and job all employee in the descending order of name.

select eno,ename,job from empl order by ename desc;

23) Write a query to display department number and employee number in the order of
department number then employee number.

select eno,dept from empl order by dept,eno;

24) Write a query to display job,name and dept of employees in dept 30 in the order
of job and then name in descending order.

select job,ename,dept from empl where dept=30 order by


job,ename desc;

25) Modify the query so that employees who do not receive commission have 0 as
commission.(Hint: use ifnull())
26) Display name, comm as commission in the order of commission.

select ename,comm from empl order by comm;

27) Display the name and salary of all employees in department 20 and 30 in alphabetical
order by name.

select ename ,sal from empl where dept in (20,30)order by ename;

28) Display the name of all employees in the order of hiredate.

select ename ,hiredate from empl order by hiredate;

29) To list the name starting with S.

select ename from empl where ename like's%';

30) To list the name ending with S.

select ename from empl where ename like '%s";

31) To list the name and hiredate of all the employees who are hired in 1981.

select ename, hiredate from empl where hiredate like


'1981%______';

32) To list the names of all employees where second letter of their name is Y.

select ename from empl where ename like '_y%';

33) Write a query to display the names and jobs of all clerks whose names have 6
characters in their name.

select ename,job from empl where ename like '______';

34) Write a query to display the department numbers, hiredate and names of all
employees who were hired in 1981 in the order of deptno and then hiredate.

select dept,hiredate,ename from empl where hiredate


like'1981%______'order by dept,hiredate;

35) To list the name of employee who is having h as any alphabet of the name.

select ename from empl where ename like'%h%';

36) Write a query to display names of employees who do not have letter 'A ' in their
names.

select ename from empl where ename not like '%a%';

37) To display maximum salary in the given table. (Use group function max ())

select max(sal) from empl;

38) Write a query to display the name and maximum salary for each department
number.

select ename,max(sal) from empl group by dept;

39) Write a query to display the dept and maximum salary for each department
number, but only having maximum salary greater than or equal to 3000. (Hint: use
group by with the having clause)

select ename,dept,max(sal) from empl group by dept having


max(sal)>=3000;

40) Write a query to display the count of the number of employees in dept 20.

select dept,count(*) from empl group by dept having dept=20;

41) Write a query to display the department number and count of the number of
employees in all the departments.

select dept, count(*) from empl group by dept;

42) Write a query to display the department number and count of the number of
employees in dept 10. (Hint: use group by with the having clause)

select dept,count(*) from empl group by dept having dept=10;

43) Write a query to display the department number, job and sum salary of employees
grouped by department number and then by job.

select dept, job,max(sal)from empl group by dept,job;

44) Write a query to display the job and earliest hiredate in each job.

select job,hiredate from empl order by hiredate;

45) Write a query to display the job, minimum salary of 'CLERK.


select job,min(sal) from empl where job='clerk';