Professional Documents
Culture Documents
207
Module - 11 (Practícals)
CREATING TABLES
Module - 19 (Practicals)
MODIFYINGTABLES
Consider the following table PAY whose column structure is shown below:
mysql> desc pay;
+
! Fie ld 1 Type I Null Key i Default } Extra !
enpno | smallint (6) I NO I PRI
iname I varchar(20> I YES NULL
i age I sma1lint (6) ! YES INULL
I city 1 varchar(20> I YES I NULL
salary decima1(?,2> I YES I NULL
| dob I date YES I NULL
6 rowS in set (0.09 sec)
b) Add a column 'bonus' decimal(7,2) to this table after the salary column with
default value 2000.
mysql) add
alterbonus
table pay
decimal(?,2) default 2000 af ter salary;
Query 0K, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings : 9
Module - 11 (Practícals)
INSERTING /ALTERING DATA
Module - 111(Practícals)
QUERIES
Table EMPLOYEES contains the fields emp id smallint not null, fname varchar(20)
Iname varchar(20), email varchar(25), phoneno varchar(10), hire date date, jobid
varchar(10), salary decimal(8,2), commission decimal(6,2), manager id smallint, dept id
smallint, where emp id is the primary key.
Write MySQL statements for the following:
a) Display the structure of this table.
Rysql> desc emplo ye es ;
| Pie ld : Type 1 Nul1 Key ! Default Extra !
| emp_id i smallint (6) | NO PRI I NULIL
! fnae ! yarchar(20) YES ! NULL
lnane varc har(20> YES NULL
enail i varchar(25 ) YES ! NULL
phoneno varchar(10) YES NULL,
hire date ! date YES NULL
jobid varchar(10) YES 1 NULL
salary ! decinal(8,2) YES NULL
comAíss ion decinal(6,2) YES I NULL
| manager id smallint <6) YES I NULL
| dept_id Snallint (6> I YES I NULL
11 rows in set (0.05 sec)
display last name and salary of all employees earning more than
d) To14,000
se lect Iname,salary
Lusq From employees
-s where salary>14000:
! salary
lname +
21000-00
joshi ! 19000.0Ø
Jain 25Ø00.Qg
nehta
gadiwalla 54000.00 !
shah
35000.00 !
55009.00
nehta
set (9.03 sec)
6
rOWS 1n
lain 104 H
) To display last name and salary for all employees whose salary is in the range
15000 to 24000.
nysql> se lect lname,salary
-> from employees
-) where salary between 15000 and 24000;
Iname salary
joshi i 21900.90
Jain
g) To display last name, job id and hire date of employees hired between
Feb 20, 1998 and May 1, 1998 in the ascending order of hire date.
aysql>
->
se lect lname . jobid, hire_date
from emplo yees
-> where hire date>'1998-02-20 and hire date<'1998-05-01
-> order by hire_date;
InameI jobid
+
hire_date
4Sandhi st clerk 1998-02-211
row in set <0.00 sec>
n) To display the last name and department of the employees in department 101 or
103 in the alphabetic order of last name.
ysql> select lnane.dept_id
-) from employees
Rwhere dept_id in (101,103>
) order by lname ;
lname dept id
joshi !
nehta 101
103
B
rows in set (0.01 sec)
216
Computer Systems and Applications (T.Y. B.Com.) (Sem. - V)
i) To display the last
name and salary of employees who earn from 15000 to 24000
and are in department 101 or 103. Label the columns as Employee
Salary respectively. and monthly
nysql> select lname Enplo yee, salary 'Monthly Salary'
-> from employees
7where salary between 15000 and 24000 and dept_id in (181,103);
Enployee Monthly Salary
joshi 21000.00
1 row in set (0.90 sec)
DTo display the last name and hire date of every employee who was hired in 2001
nysql> select Iname,hire_date
from employees
- where yearhire _date) =2901 or hire_date like "2001z';
! lname ! hire date
joshi } 2001-10-05 !
Jain 2001-10-12 !
2 rows in set, 1 warning (0.06 sec
k) To display the last name and job type of all employees who do not have
manager.
nysgl> select lname,jobid
3 from employees
where manager_id is null;
! lname ! jobid
| mehta gen mgr i
4+
) Display the last name salary and commission of all employees who eam
Commissions in the descending order of salary and commission.
mysql> se lect Inane , salary,comniss ion
employees
-> where comniss ion is not null or comniss ion ?
-> order by salary desc,comnission desc ;
H lname salary I commiss ion
m) Display the last names of all employees where the third letter of the name is an h.
nysql) select Inane
-> fron employees
-> where Iname like h';
mehta !
| nehta !
2 rows in set (0.00 sec)
217
es
(Pracçcal)
last
To display the last name of the employees who have an 'a' and an 'e' in their
n) name.
select lname
hsqFrom omployees
-) where
Iname 1ike '%az' and lnano 11ke %0%';
Iname !
nehta
nehta
in set (a.00 sec)
rOws
2
stclerk 14000.00 !
gandhi I 19000.00
Jain ! sarep
(0.01 sec)
2 rows in set
pysql) select *
-> from departments;
dept id ! deptname ngrid locat ionid !
101 ! admin 515 !
102 ! purchase 512 1800
103 accounts 511 ! 1500
104 sales 515 ! 1400
b) Create a unique listing of all jobs that are in department 101. Include the location
of the department in the output.
nysql> select distinct a-jobid, b. locat ion id
-> from employees a,departments b
-> where a.dept id=b.dept id and a.dept id=101;
jobid locat ion id
+
|! ad vp i 1708
c) who
Todisplay the last name, departmentname, location ld and city of allemployees
earn a comnmission.
ysql> select a.Inane,b.deptnane ,b.locat
from enplo yees a,departments b, ion 1dC.C1ty
C
-> where a.deptid-b. dept id
and b. location id=c.locat ion id
-> and a.comniss ion >0;
Iname deptname locat ion id city
joshi admin 1799 unbai
gandhi purchase 1800 pune
shah 1purchase 1800
mehta ipurchase 1800 pune
mehta accounts 1509 bhopal
Ja in ! sales 1400 aurangabad
gadiwalla sales 1400 aurangabad
? rows in set (0.09 sec)
d) Display the last name and department name of allemployees who have 'a' in their
last name.
pysql> se lect a.lname,b.deptname
-> from emplo yees a, departments b
where a.dept id=b.deptu
and a.lname like
lname deptname !
gandhi 1 purchase
shah purchase
| mehta purchase
nehta 8 accounts
Jain sales
4. gadiwalla ! sales
þ rows in set (0.00 sec>
(T.Y. B.Com.) (Sem1
222
Computer Systerns and Applications
same job_id.
d) Display the number of people with the
aysql) select jobid,count (*)
-> fron omployees
group by jobid;
jobid I count (*) I
acngr
ad_vp 1
gen gr
purgr 1
sal gr 1
1
sarep
st_c lerk
rows in set (0.0 sec>
Range
) Display the difference between the highest and lowest salaries and label it
of Salary.
se lect nax(salary)-min (salary) 'Bange of Salary'
from employees ;
| Range of Salary
41000.00
1 roW in set (0.02 sec)
) Display the manager number and the salary of the lowest paid employee for that
Manager. Exclude anyone whose manager is not known. Exclude any groups
where the minimum salary is 16,000 or more. Sort the output in the descending
order of salary.
select manager_id, min (salary)
nysq?enployees
-> where manager_id is not null
-> group by manager_id
-> hauing min (salary) >16900
-> order by min (salary) desc;
manager_id min(salaPy) !
515 21000.00
511 ! 19000.00 !
Using the table EMPLOYEES listed above write MySQL statements for the following :
a) Todisplay the employee Id and last names of all employees who earn more than
the average salary. Sort the results in ascending order of salary.
nysq select emp_id, lname
Fpom employees
-> where salary>(select avg{salary> from enplo yees>
-> order by salary;
emp_id lname
512 shah
511 gadiwalla
515 ! mehta
b) To display the last name and hire date of any employee in the same department as
'Gandhi'.Exclude 'Gandhi'.
nysql> se lect lnane , hire date
from employees
-) where deptid= (select deptid fron employees where Inane =Gandhi'>
-> and lname () 'Gandhi';
1 Iname hire_date
shah 1999-12-10
i nehta 1995-10-20
2 rOWs in set (9.00 sec)
c) To display the employee Id and last names of all employees who work in a
department with any employee whose last name contains a 'w'.
pysql> se lect enp_id, Inane
-> from emplo ye es lname like z ) :
-> where deptid = (se lect dept id from enployees where
+
i enp_id I Iname
3 Ja in
S11 gadiwalla
rows in set (0.00 sec)
224
Computer Systems and Applications (T.Y. B.Com.) (Sem.
d) To display the last name, department ld and job Id of all
-V)
department location ID is 1700. employees whose
ysql> se lect Inane , dept id.jobid
fron omployees
3 where dept id-(se lect deptid fron de part nents where locat ion id -1708);
| lname dept id i jobid 1
| joshi ! 101 ad_wp
row in set (0.00 sec)
e) To display the last name and salary of every employee who reports to 'gadiwalla'
aysql> se lect InaNe ,salary
-> fron employees
3where anager id-(select emp_id fron enployees where Inane='gadivalla' >;
Inane ! salary
Jain 190O0.00 I
ehta i 25000.00
2 Os in set (0.00 sec)
f) To display the department number, last name and Job Id of every employee in the
accounts department.
ysql> select deptid, Inane.jobid
-> from enployees
-> where deptid=(se lect deptid froR departments where deptnane =' accounts');
deptid i Inae jobid !
103 ! nehta i ac_ngr H
1 ov in set (8.03 sec)