1.
create database:
mysql> create database school;
Query OK, row affected (0 . 12 sec)
2. create table:
mysql > us e sc hool;
Database changed
mysql > create table employee(
- > emp_ id integer,
-> emp_name varchar ( 20 ) ,
-> job_title varchar ( l0 ) ,
- > salary integer,
-> bonu s integer,
- > age integer,
- > manager_id integer ) ;
Query OK, 0 rows affected ( 0.33 sec)
3. Show table:
mysql> select•FROM employee;
+---- - - - -+- ------ - --+-- - ---- - - - -+- --- - - - - + - - • ----+- - -- - - +- - - - ---- - - - -+
I emp_id I emp_narne I job_title I salary 1 bonus I age I manager _id I
+--------+---------- +-----------+--------+------- +------+------------+
1201 divya president 50006 NULL 29 NULL
120S amyra man.iger 30000 2500 26 1201
rahul a nalyst 1500 1205
1211
1213
1216
manish
megha
salesman
analyst
20800
15000
22000
NULL
1300
"
22
25 1201
1205
1217 mohit salesman 16000 NUll 22 1205
•I•------ - - +-- • --- • --- ,!-- -------- • • +-- • --- • - +--- ---- +--- • -- +• •--------. • +
G rows in set (8.02 sec)
4. insert values:
mysql> insert i ,,to employee values {l 201, 'di vya' , ' preside,,t ', 500&0, ,,ull ,29, null);
Query OK, 1 row affected (0.06 sec}
mysql> insert into employee values(120S, 'amyra', •manager' ,30000,2500,26,1201);
Query OK, l row affected (0.01 sec }
mysql > inser-t into employee values (Hl 1, 'r-ahu 1 ' , ·analyst ' , 1.eeee, l see, 23, 1205);
Query OK, 1 row affec·ted (0.01 sec: )
mysql> insert into employee values(12l3, "manish', ·salesman• ,15080,null,22,1205);
Query OK, l row affected (0.01 sec:)
mysql> insert into employee values{l216, 'megha', •analyst' ,22000,1300,25, 1201);
Query OK, l row affected (0.01 sec )
mysql> insert into employee values(1217, 'mohit', ' salesman' ,1660{1,null,22,1205);
Query OK , 1 row affected (0.01 sec )
mysql>
5. delete the employee having
emp_id1217
mys ql > DELE TE FROM employee
- > WHERE emp_id =1217;
Query OK, 1 r ow aff ect ed (0 .02 sec )
6. update the salary
mysql > UPDATE employee
- > SET salary=40000
- > WHERE emp_name="amyra";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed : 1 Warnings: 0
7. alter the table to no Null
mysql > ALTER TABLE employee
- > MODIFY age integer NOT NULL;
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
8. display names and salaries of
employees whose salary are greater
than20000
mysql> SELECT emp_ name,salary
-> FROM employee
-> WHERE salary>20000;
+----------+--------+
I emp_name I salary I
+----------+--------+
I divya s0000 I
I amyra 40000 I
I megha 22000 I
+----------+--------+
3 rows in set (0.01 sec)
9. display names of employees who are
not getting bonus
mysql> SE LECT*FROM employee
- > l._lHERE bonu:. i:;, null;
+-- --- -- -+----· ·· · ··+--- -- ------+-·---- ·-+- ----- -+------+---- -- ---- --+
l emp_id I emp_name I job_title I salary I bonus I age I man ager_id I
+-------- +---------- +----------- +-------- +------- +------ +------------ +
1201 I divya I pre s ident I 50000 I NULL I 29 I NULL I
1213 I manis h I salesman I 15000 I NULL I 22 J 1205 I
2 rows in set (0 .02 sec)
10. display name who contains "a" as
last alphabet
mysql > SELECT emp_name
-> FROM employee WHERE emp_name
-> LIKE"%a";
+----------+
I emp_name I
+----------+
I divya
I amyra
I megha
+----------+
3 rows in set (0 . 01 sec)
11. display name and job title whose
manager id is 1201
mysql> SELECT emp_name,job_title
-> FROM employee
-> WHERE manager_id=1201;
+----------+-----------+
I emp_name I job_title I
+----------+-----------+
I amyra I manager
I megha I analyst
+----------+-----------+
2 rows in set (0.00 sec)
12. display name and job titile whose
manager is Amyra
Tiysql> SELECT emp_name,job_title
-> FROM employee
-> WHERE manager_id=1205;
+----------+-----------+
I emp_name I job_title I
+----------+-----------+
I rahul I analyst
I manish I salesman
+----------+-----------+
2 rows in set (0.00 sec)
13. Display name and job titile of
employees aged between 26 and 39
years
mysql> SELECT emp_name,job_title
- > FROM employee
- > WHERE age between 26 and 30;
+----------+-----------+
I emp_name I job_title I
+- - - -- -- - --+ -- - - --- - -- - +
I divya I president I
I amyra I manager I
+----------+------ - ----+
2 rows in set (0.01 sec)
14. Display values in descending order
mysql> SELECT emp_name
-> FROM employee
-> ORDER BY emp_name DESC;
+----------+
I emp_name I
+----------+
I rahul I
I megha I
I manish I
I divya I
I amyra I
+------ - ---+
5 rows in set (0.02 sec)
15. Total salary
mysql> SELECT SUM(salary) AS TOTAL_SALARY
-> FROM employee;
+--------------+
I TOTAL_SALARY I
+--------------+
147000 I
+--------------+
1 row in set (0 . 01 sec)