Вы находитесь на странице: 1из 76

QUESTION I

1) Create and open database named MYORG.


2) Write a command to display the name of current month
3) Write command to display the system date
4) Write a query to find out the result of 6^3.
5) Write command to show the tables in the MYORG database.
6) Add one column state of datatype varchar and size 30 to table DEPT.
7) Create a table name EMP with following structure:

Column EmpID EmpName Designnation DOJ Sal comm DeptID


Name
Data Integer Varchar(30) Char(10) Date Integer Integer Integer
Type
Contraint Primary Not Null Check>1000 Foreign
Key Key

8) Insert the first record in table EMP (8369,SMITH,CLERK,18-12-1990,800.00,


200.00, 10)
9) Write a query to display EmpName and sal of employees whose salary are
greater than or equal to 2200
10) Write a query to display the details of employee who are not getting
commission .
11) Write a query to display employee name and salary of those
employees who don’t have their salary in rang of 2500 to 4000.
12) Write a query to display the name of employee whose name contains
“A” as third alphabet in ascending order of employee names.
13) Display the sum of salary and commissions of employees as “Total
Incentive”.
14) Show the average salary for all departments with more than 5
working people .
15) Display the distinct designation offered by the organization.
16) List the count of employees grouped by DeptID
17) Display the names of employees who join on or after “01-05-1991”
18) Display the employee record in order by DOJ.
19) Display the maximum salary of employees in each department.
20) Update all the records as add “MR.” with EmpName .
21) Display the name of employees who are working in sales department
.
22) Drop the emp table.
23) Delete all the records who is working as “CLERK”.
24) Show the minimum,maximum and average salary of managers .
25) Increase the salary of managers by 15%.
26) To display the name of those employees whose location is “delhi”.
27) To display total salary of employees of sales department.
28) To show details of employees who joined in the year “1991”.
29) Delete all the records who is working as “SALESMAN” AND SALARY
MORE THAN 1500.
30) SET THE COMMISSION AS 100 WHO ARE NOT GETTING ANY
COMMISSION.

SOLUTION
Enter password: ****

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database MYORG;

Query OK, 1 row affected (0.00 sec)

mysql> use MYORG;

Database changed

mysql> SELECT MONTH(CURDATE());

+------------------+

| MONTH(CURDATE()) |

+------------------+

| 6|

+------------------+

1 row in set (0.00 sec)

mysql> select sysdate();

+---------------------+

| sysdate() |

+---------------------+

| 2019-06-15 16:17:05 |

+---------------------+

1 row in set (0.00 sec)

mysql> select pow(6,3);

+----------+

| pow(6,3) |

+----------+

| 216 |

+----------+

1 row in set (0.00 sec)


mysql> create table DEPT(DeptID int Primary Key, DeptName varchar(25), MgrID int

, Location varchar(25));

Query OK, 0 rows affected (0.52 sec)

mysql> insert into DEPT values(10, "Sales", 8566, "Mumbai");

Query OK, 1 row affected (0.02 sec)

mysql> insert into DEPT values(20, "Personel", 8698, "Delhi");

Query OK, 1 row affected (0.00 sec)

mysql> insert into DEPT values(30, "Accounts", 8882, "Delhi");

Query OK, 1 row affected (0.06 sec)

mysql> insert into DEPT values(40, "Research", 8839, "Bangalore");

Query OK, 1 row affected (0.00 sec)

mysql> select * from Dept;

+--------+----------+-------+-----------+

| DeptID | DeptName | MgrID | Location |

+--------+----------+-------+-----------+

| 10 | Sales | 8566 | Mumbai |

| 20 | Personel | 8698 | Delhi |

| 30 | Accounts | 8882 | Delhi |

| 40 | Research | 8839 | Bangalore |

+--------+----------+-------+-----------+

4 rows in set (0.00 sec)

mysql> alter table dept add state varchar(30);


Query OK, 4 rows affected (0.14 sec)

Records: 4 Duplicates: 0 Warnings: 0

mysql> create table emp(EmpID int primary key, EmpName varchar(30) Not Null,

Designation char(10), DOJ date, sal int check(sal>1000), comm int, DeptID int re

ferences DEPT(DeptID));

Query OK, 0 rows affected (0.50 sec)

mysql> alter table emp modify sal decimal(10,2);

Query OK, 0 rows affected (0.54 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into emp values(8369, "SMITH", "CLERK", "1990-12-18", 1050.00, 200

.00, 10);

Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(8499, "ANYA", "SALESMAN", "1991-02-20", 1600.00, 3

00.00, 20);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8566, "MAHADEVAN", "MANAGER", "1991-04-02", 2985.0

0, NULL, 30);

Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(8654, "MOMIN", "SALESMAN", "1991-09-28", 1250.00,

400.00, 20);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8698, "BINA", "MANAGER", "1991-01-05", 2850.00, 25


0.00, 30);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8882, "SHIVANSH", "MANAGER", "1991-06-09", 2450.00

, NULL, 10);

Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(8888, "SCOTT", "ANALYST", "1992-12-09", 3000.00,15

0.00, 10);

Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(8839, "AMIR", "PRESIDENT", "1991-11-18", 5000.00,N

ULL, 20);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8844, "KULDEEP", "SALESMAN", "1992-04-08", 1500.00

,0.00, 30);

Query OK, 1 row affected (0.50 sec)

mysql> SELECT * FROM EMP;

+-------+-----------+-------------+------------+---------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+---------+------+--------+

| 8369 | SMITH | CLERK | 1990-12-18 | 1050.00 | 200 | 10 |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600.00 | 300 | 20 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985.00 | NULL | 30 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250.00 | 400 | 20 |

| 8698 | BINA | MANAGER | 1991-01-05 | 2850.00 | 250 | 30 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000.00 | NULL | 20 |


| 8844 | KULDEEP | SALESMAN | 1992-04-08 | 1500.00 | 0 | 30 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450.00 | NULL | 10 |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000.00 | 150 | 10 |

+-------+-----------+-------------+------------+---------+------+--------+

9 rows in set (0.00 sec)

mysql> SELECT EmpName , sal from emp where sal>=2200;

+-----------+---------+

| EmpName | sal |

+-----------+---------+

| MAHADEVAN | 2985.00 |

| BINA | 2850.00 |

| AMIR | 5000.00 |

| SHIVANSH | 2450.00 |

| SCOTT | 3000.00 |

+-----------+---------+

5 rows in set (0.00 sec)

mysql> select * from emp where comm is null;

+-------+-----------+-------------+------------+---------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+---------+------+--------+

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985.00 | NULL | 30 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000.00 | NULL | 20 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450.00 | NULL | 10 |

+-------+-----------+-------------+------------+---------+------+--------+

3 rows in set (0.00 sec)

mysql> select empname, sal from emp where sal not between 2500 and 4000;
+----------+---------+

| empname | sal |

+----------+---------+

| SMITH | 1050.00 |

| ANYA | 1600.00 |

| MOMIN | 1250.00 |

| AMIR | 5000.00 |

| KULDEEP | 1500.00 |

| SHIVANSH | 2450.00 |

+----------+---------+

6 rows in set (0.00 sec)

mysql> select empname from emp where empname like("_A%") order by empname asc;

+-----------+

| empname |

+-----------+

| MAHADEVAN |

+-----------+

1 row in set (0.00 sec)

mysql> select sal+comm as "Total Incentive" from emp where comm is not null;

+-----------------+

| Total Incentive |

+-----------------+

| 1250.00 |

| 1900.00 |

| 1650.00 |

| 3100.00 |

| 1500.00 |
| 3150.00 |

+-----------------+

6 rows in set (0.00 sec)

mysql> select distinct designation from emp;

+-------------+

| designation |

+-------------+

| CLERK |

| SALESMAN |

| MANAGER |

| PRESIDENT |

| ANALYST |

+-------------+

5 rows in set (0.00 sec)

mysql> select count(*) from emp group by deptid;

+----------+

| count(*) |

+----------+

| 3|

| 3|

| 3|

+----------+

3 rows in set (0.02 sec)

mysql> select EmpName from emp where DOJ>=1991-05-01;

+-----------+

| EmpName |
+-----------+

| SMITH |

| ANYA |

| MAHADEVAN |

| MOMIN |

| BINA |

| AMIR |

| KULDEEP |

| SHIVANSH |

| SCOTT |

+-----------+

9 rows in set, 1 warning (0.00 sec)

mysql> select * from emp order By DOJ;

+-------+-----------+-------------+------------+------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+------+------+--------+

| 8369 | SMITH | CLERK | 1990-12-18 | 1050 | 200 | 10 |

| 8698 | BINA | MANAGER | 1991-01-05 | 2850 | 250 | 30 |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 | 20 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL | 30 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL | 10 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL | 20 |

| 8844 | KULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 |

+-------+-----------+-------------+------------+------+------+--------+

9 rows in set (0.00 sec)


mysql> select * from emp

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+------+------+--------+

| 8369 | SMITH | CLERK | 1990-12-18 | 1050 | 200 | 10 |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 | 20 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL | 30 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8698 | BINA | MANAGER | 1991-01-05 | 2850 | 250 | 30 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL | 20 |

| 8844 | KULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL | 10 |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 |

+-------+-----------+-------------+------------+------+------+--------+

9 rows in set (0.00 sec)

mysql> select max(sal) from emp group by deptID;

+----------+

| max(sal) |

+----------+

| 3000 |

| 5000 |

| 2985 |

+----------+

3 rows in set (0.00 sec)

mysql> update emp set empname=concat('Mr',EmpName);

Query OK, 9 rows affected (0.00 sec)

Rows matched: 9 Changed: 9 Warnings: 0


mysql> select empname from emp,dept where Designation="SALES" and emp.DeptID=dept.DeptID;

Empty set (0.01 sec)

mysql> delete from emp where designation="CLERK";

Query OK, 1 row affected (0.01 sec)

mysql> select min(sal),max(sal),avg(sal) from emp where designation="Manager";

+----------+----------+-----------+

| min(sal) | max(sal) | avg(sal) |

+----------+----------+-----------+

| 2450 | 2985 | 2761.6667 |

+----------+----------+-----------+

1 row in set (0.00 sec)

mysql> update emp set sal=sal+0.15*sal where designation="Manager";

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3 Changed: 3 Warnings: 0

mysql> select EmpName from emp,dept where location="Delhi" and emp.deptid=dept.deptid;

+-------------+

| EmpName |

+-------------+

| MrANYA |

| MrMAHADEVAN |

| MrMOMIN |

| MrBINA |

| MrAMIR |

| MrKULDEEP |
+-------------+

6 rows in set (0.00 sec)

mysql> select sum(sal) from emp,dept where designation="SALES" and emp.deptid=dept.deptid;

+----------+

| sum(sal) |

+----------+

| NULL |

+----------+

1 row in set (0.00 sec)

mysql> select sum(sal) from emp where designation="sales";

+----------+

| sum(sal) |

+----------+

| NULL |

+----------+

1 row in set (0.00 sec)

mysql> select * from emp where year(DOJ)=1991;

+-------+-------------+-------------+------------+------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-------------+-------------+------------+------+------+--------+

| 8499 | MrANYA | SALESMAN | 1991-02-20 | 1600 | 300 | 20 |

| 8566 | MrMAHADEVAN | MANAGER | 1991-04-02 | 3433 | NULL | 30 |

| 8654 | MrMOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8698 | MrBINA | MANAGER | 1991-01-05 | 3278 | 250 | 30 |

| 8839 | MrAMIR | PRESIDENT | 1991-11-18 | 5000 | NULL | 20 |

| 8882 | MrSHIVANSH | MANAGER | 1991-06-09 | 2818 | NULL | 10 |


+-------+-------------+-------------+------------+------+------+--------+

6 rows in set (0.00 sec)

mysql> delete from emp where designation="salesman" and sal>1500;

Query OK, 1 row affected (0.00 sec)

mysql> update emp set comm=100 where comm is null;

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3 Changed: 3 Warnings: 0

mysql> select * from emp;

+-------+-------------+-------------+------------+------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-------------+-------------+------------+------+------+--------+

| 8566 | MrMAHADEVAN | MANAGER | 1991-04-02 | 3433 | 100 | 30 |

| 8654 | MrMOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8698 | MrBINA | MANAGER | 1991-01-05 | 3278 | 250 | 30 |

| 8839 | MrAMIR | PRESIDENT | 1991-11-18 | 5000 | 100 | 20 |

| 8844 | MrKULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 |

| 8882 | MrSHIVANSH | MANAGER | 1991-06-09 | 2818 | 100 | 10 |

| 8888 | MrSCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 |

+-------+-------------+-------------+------------+------+------+--------+

7 rows in set (0.00 sec)

mysql> select * from emp,dept where dept.deptid=emp.deptid;

+-------+-------------+-------------+------------+------+------+--------+--------+----------+-------+----------+-------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID | DeptID | DeptName | MgrID |
Location | state |

+-------+-------------+-------------+------------+------+------+--------+--------+----------+-------+----------+-------+
| 8566 | MrMAHADEVAN | MANAGER | 1991-04-02 | 3433 | 100 | 30 | 30 | Accounts | 8882 |
Delhi | NULL |

| 8654 | MrMOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 | 20 | Personel | 8698 |


Delhi | NULL |

| 8698 | MrBINA | MANAGER | 1991-01-05 | 3278 | 250 | 30 | 30 | Accounts | 8882 | Delhi


| NULL |

| 8839 | MrAMIR | PRESIDENT | 1991-11-18 | 5000 | 100 | 20 | 20 | Personel | 8698 | Delhi


| NULL |

| 8844 | MrKULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 | 30 | Accounts | 8882 | Delhi


| NULL |

EmpID EmpName Designation DOJ Sal Comm


Number Varchar Char(10) Date Number Number
| 8882 | MrSHIVANSH | MANAGER | 1991-06-09 | 2818 | 100 | 10 | 10 | Sales | 8566 |
Mumbai | NULL |

| 8888 | MrSCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 | 10 | Sales | 8566 | Mumbai


| NULL |

+-------+-------------+-------------+------------+------+------+--------+--------+----------+-------+----------+-------+

7 rows in set (0.00 sec)

QUESTION II
1. Write a command to display the system date .
2. Write a command to display the name of current month
3. Write command to print the day of the week of your birthday in the year
2015
4. Write the command to round off value 15.193 to nearest tens ie.20.
5. Write a query to find the out the result of 6^3.
6. Create and open database named MYORG.
7. Create a table name EMP with following structure (EMPID Primary Key )

8. Insert the following records :


EmpID EmpName Designation DOJ Sal Comm
8369 SMITH CLERK 1990-12-18 800.00 NULL
8499 ANYA SALESMAN 1991-02-20 1600.00 300.00
8521 SETH SALESMAN 1991-02-22 1250.00 500.00
8566 MAHADEVAN MANAGER 1991-04-02 2985.00 NULL
8654 MOMIN SALESMAN 1991-09-08 1250.00 NULL
8698 BINA MANAGER 1991-05-01 2850.00 NULL
8882 SHIVANSH MANAGER 1991-06-09 2450.00 NULL
8888 SCOTT ANALYST 1992-12-09 3000.00 NULL
8839 AMAR PRESIDENT 1991-11-18 5000.00 NULL

8844 KULDEEP SALESMAN 1991-09-08 1500.00 0.00

9. Write a query to display all the records with all the columns .
10.Write a query to display EmpName and sal of employees whose salary are
greater than or equal to 2200.
11.Write a query to display details of employees who are not getting
commission .
12.Write a query to display employee name and salary of those employees
who don’t have their salary in range of 2500 to 4000.
13.Write a query to display the name of employee whose name contains “a”
as third alphabet in ascending order of employee names.
14.Write a query to display the ENAME and sal with 50% of sal as DA.
15.Display the distinct job titles offered by the organization
16.Display the names of employees who are working as manager or analyst .
17.Display the name of employees who join on or after 01-05-1991.
18.Display the employee record in order by DOJ.
19.Display the distinct designation in the organization .

SOLUTION
Enter password: *********

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.16 MySQL Community Server (GPL)


Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select sysdate();

+---------------------+

| sysdate() |

+---------------------+

| 2019-06-27 19:31:46 |

+---------------------+

1 row in set (0.00 sec)

mysql> select dayofweek("2015-12-30");

+-------------------------+

| dayofweek("2015-12-30") |

+-------------------------+

| 4|

+-------------------------+

1 row in set (0.00 sec)

mysql> select round(15.193,2);

+-----------------+

| round(15.193,2) |

+-----------------+

| 15.19 |

+-----------------+
1 row in set (0.00 sec)

mysql> select round(15.193,1);

+-----------------+

| round(15.193,1) |

+-----------------+

| 15.2 |

+-----------------+

1 row in set (0.00 sec)

mysql> select pow(6,3);

+----------+

| pow(6,3) |

+----------+

| 216 |

+----------+

1 row in set (0.00 sec)

mysql> drop database MYORG;

Query OK, 2 rows affected (0.03 sec)

mysql> create database MYORG;

Query OK, 1 row affected (0.00 sec)

mysql> USE MYORG;

Database changed

mysql> create table Emp(EmpID int primary key, EmpName varchar(25), Designation char(10), DOJ date,
sal int, comm int);

Query OK, 0 rows affected (0.01 sec)


mysql> insert into Emp values(8369, "SMITH", "CLERK", "1990-12-18", 800.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8499, "ANYA", "SALESMAN", "1991-02-20", 1600.00, 300.00);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8521, "SETH", "SALESMAN", "1991-02-22", 1250.00, 500.00);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8566, "MAHADEVAN", "MANAGER", "1991-04-02", 2985.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8654, "MOMIN", "SALESMAN", "1991-09-28", 1250.00, 400.00);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8698, "BINA", "MANAGER", "1991-05-01", 2850.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8882, "SHIVANSH", "MANAGER", "1991-06-09", 2450.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8888, "SCOTT", "ANALYST", "1992-12-09", 3000.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8839, "AMIR", "PRESIDENT", "1991-11-18", 5000.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8844, "KULDEEP", "SALESMAN", "1991-09-08", 1500.00, 0.00);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM EMP;

+-------+-----------+-------------+------------+------+------+

| EmpID | EmpName | Designation | DOJ | sal | comm |

+-------+-----------+-------------+------------+------+------+

| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |

| 8521 | SETH | SALESMAN | 1991-02-22 | 1250 | 500 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |

| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |

| 8844 | KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |

+-------+-----------+-------------+------------+------+------+

10 rows in set (0.00 sec)

mysql> SELECT EMPNAME, SAL FROM EMP WHERE SAL>=2200;

+-----------+------+

| EMPNAME | SAL |

+-----------+------+

| MAHADEVAN | 2985 |

| BINA | 2850 |

| AMIR | 5000 |

| SHIVANSH | 2450 |

| SCOTT | 3000 |

+-----------+------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM EMP WHERE COMM IS NULL;

+-------+-----------+-------------+------------+------+------+

| EmpID | EmpName | Designation | DOJ | sal | comm |

+-------+-----------+-------------+------------+------+------+

| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |

| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |

+-------+-----------+-------------+------------+------+------+

6 rows in set (0.00 sec)

mysql> SELECT EMPNAME, SAL FROM EMP WHERE SAL BETWEEN ! 2500 AND 4000;

+-----------+------+

| EMPNAME | SAL |

+-----------+------+

| SMITH | 800 |

| ANYA | 1600 |

| SETH | 1250 |

| MAHADEVAN | 2985 |

| MOMIN | 1250 |

| BINA | 2850 |

| KULDEEP | 1500 |

| SHIVANSH | 2450 |

| SCOTT | 3000 |

+-----------+------+
9 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP WHERE EMPNAME LIKE("__A%") ORDER BY EMPNAME ASC;

Empty set (0.00 sec)

mysql> SELECT EMPNAME, SAL, (50*SAL)/100 AS DA FROM EMP;

+-----------+------+-----------+

| EMPNAME | SAL | DA |

+-----------+------+-----------+

| SMITH | 800 | 400.0000 |

| ANYA | 1600 | 800.0000 |

| SETH | 1250 | 625.0000 |

| MAHADEVAN | 2985 | 1492.5000 |

| MOMIN | 1250 | 625.0000 |

| BINA | 2850 | 1425.0000 |

| AMIR | 5000 | 2500.0000 |

| KULDEEP | 1500 | 750.0000 |

| SHIVANSH | 2450 | 1225.0000 |

| SCOTT | 3000 | 1500.0000 |

+-----------+------+-----------+

10 rows in set (0.00 sec)

mysql> SELECT DISTINCT DESIGNATION FROM EMP;

+-------------+

| DESIGNATION |
+-------------+

| CLERK |

| SALESMAN |

| MANAGER |

| PRESIDENT |

| ANALYST |

+-------------+

5 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP WHERE DESIGNATION IN("MANAGER", "ANALYST");

+-----------+

| EMPNAME |

+-----------+

| MAHADEVAN |

| BINA |

| SHIVANSH |

| SCOTT |

+-----------+

4 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP WHERE DOJ>=1991-05-01;

+-----------+

| EMPNAME |

+-----------+

| SMITH |

| ANYA |

| SETH |

| MAHADEVAN |

| MOMIN |
| BINA |

| AMIR |

| KULDEEP |

| SHIVANSH |

| SCOTT |

+-----------+

10 rows in set, 1 warning (0.00 sec)

mysql> SELECT * FROM EMP ORDER BY DOJ;

+-------+-----------+-------------+------------+------+------+

| EmpID | EmpName | Designation | DOJ | sal | comm |

+-------+-----------+-------------+------------+------+------+

| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |

| 8521 | SETH | SALESMAN | 1991-02-22 | 1250 | 500 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |

| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |

| 8844 | KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |

+-------+-----------+-------------+------------+------+------+

10 rows in set (0.00 sec)

mysql> SELECT DISTINCT DESIGNATION FROM EMP;

+-------------+

| DESIGNATION |

+-------------+
| CLERK |

| SALESMAN |

| MANAGER |

|
DeptID DeptName MgrID Location
Number VarChar(25) Number Char(40)
PRESIDENT |

| ANALYST |

+------
------- DeptID DeptName MgrID Location
+ 10 Sales 8566 Mumbai
5
20 Personnel 8698 Delhi
rows 30 Accounts 8882 Delhi
in set 40 Research 8839 Bangalore
(0.00
sec)

QUESTION III
1. Open database MYORG.
2. Create the following table DEPT with DeptId as Primary Key .

3. Insert the following in the Dept Table:

4. After the table Emp as add a column DeptId (Number).


5. Add foreign Key as DeptID which refers DeptId column of Dept Table
6. Update DeptID of EMP table with valid DeptID’s to link both the tables .
7. Show the minimum and maximum and average salary of managers.
8. Count the number of clerk in the organization .
9. Display the designation wise list of employees with name, sal and date of
joining.
10.Count the number of employees who are not getting commission.
11. Show the average salary for all departments with more than 5 working
people.
12.List the count of employees grouped by DeptID.
13.Display the maximum salary of employees in each department.
14.Display the name of employees along with their designation and
department name .
15.Count the number of employees working in accounts department.
16.Display the name of employees working in delhi.
17.Display the name of employee working in the same city from where they
belongs.
18.Display the name of employees whose is managing sales department.
19.Display the name of employees who are working in delhi and getting more
than 5000.
20.Display the details of employees who are working in research department.

SOLUTION
Enter password: *********

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use myorg;

Database changed

mysql> show tables;

+-----------------+

mysql> create table DEPT(DeptID int, DeptName varchar, MgrID int, Location char(40));
Query OK, 0 rows affected (0.01 sec)

+-----------------+

mysql> insert into DEPT values(10, “Sales”, 8566, “Mumbai”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> insert into DEPT values(20, “Personel”, 8698, “Delhi”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> insert into DEPT values(30, “Accounts”, 8882, “Delhi”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> insert into DEPT values(40, “Research”, 8839, “Bangalore”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> alter table EMP add DeptID(int);

Query OK, 0 row affected(0.01 sec)

+-----------------+

mysql> update emp set DeptID = 10 where EmpName = "SMITH";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 20 where EmpName = "ANYA";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 30 where EmpName = "SETH";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0


mysql> update emp set DeptID = 40 where EmpName = "MAHADEVAN";

Query OK, 1 row affected (0.02 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 10 where EmpName = "MOMIN";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 20 where EmpName = "BINA";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 30 where EmpName = "SHIVANSH";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 40 where EmpName = "SCOTT";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 10 where EmpName = "AMIR";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 20 where EmpName = "KULDEEP";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0


mysql> ALTER TABLE EMP ADD FOREIGN KEY(DEPTID) REFERENCES DEPT(DEPTID);

Query OK, 10 rows affected (0.02 sec)

Records: 10 Duplicates: 0 Warnings: 0

mysql> SELECT MIN(SAL), MAX(SAL), AVG(SAL) FROM EMP WHERE DESIGNATION="MANAGER";

+----------+----------+-----------+

| MIN(SAL) | MAX(SAL) | AVG(SAL) |

+----------+----------+-----------+

| 2450 | 2985 | 2761.6667 |

+----------+----------+-----------+

1 row in set (0.00 sec)

mysql> SELECT COUNT(DESIGNATION) FROM EMP WHERE DESIGNATION="CLERK";

+--------------------+

| COUNT(DESIGNATION) |

+--------------------+

| 1|

+--------------------+

1 row in set (0.00 sec)

mysql> SELECT EMPNAME, SAL, DOJ FROM EMP GROUP BY DESIGNATION;

+-----------+------+------------+

| EMPNAME | SAL | DOJ |

+-----------+------+------------+

| SCOTT | 3000 | 1992-12-09 |

| SMITH | 800 | 1990-12-18 |

| MAHADEVAN | 2985 | 1991-04-02 |

| AMIR | 5000 | 1991-11-18 |

| ANYA | 1600 | 1991-02-20 |


+-----------+------+------------+

5 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM EMP WHERE COMM IS NULL;

+----------+

| COUNT(*) |

+----------+

| 6|

+----------+

1 row in set (0.00 sec)

mysql> SELECT AVG(SAL) FROM EMP,DEPT GROUP BY DEPTNAME HAVING COUNT(*)>5;

+-----------+

| AVG(SAL) |

+-----------+

| 2268.5000 |

| 2268.5000 |

| 2268.5000 |

| 2268.5000 |

+-----------+

4 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM EMP GROUP BY DEPTID;

+----------+

| COUNT(*) |

+----------+

| 3|

| 3|

| 2|
| 2|

+----------+

4 rows in set (0.00 sec)

mysql> SELECT MAX(SAL) FROM EMP,DEPT WHERE DEPT.DEPTID = EMP.DEPTID GROUP BY DEPTNAME;

+----------+

| MAX(SAL) |

+----------+

| 2450 |

| 2850 |

| 3000 |

| 5000 |

+----------+

4 rows in set (0.00 sec)

mysql> SELECT EMPNAME, DESIGNATION, DEPTNAME FROM EMP,DEPT WHERE DEPT.DEPTID =


EMP.DEPTID;

+-----------+-------------+-----------+

| EMPNAME | DESIGNATION | DEPTNAME |

+-----------+-------------+-----------+

| SMITH | CLERK | SALES |

| MOMIN | SALESMAN | SALES |

| AMIR | PRESIDENT | SALES |

| ANYA | SALESMAN | PERSONEL |

| BINA | MANAGER | PERSONEL |

| KULDEEP | SALESMAN | PERSONEL |

| SETH | SALESMAN | ACCOUNTS |

| SHIVANSH | MANAGER | ACCOUNTS |


| MAHADEVAN | MANAGER | RESEARCH |

| SCOTT | ANALYST | RESEARCH |

+-----------+-------------+-----------+

10 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM EMP,DEPT WHERE DEPTNAME="ACCOUNTS" AND DEPT.DEPTID =


EMP.DEPTID;

+----------+

| COUNT(*) |

+----------+

| 2|

+----------+

1 row in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP,DEPT WHERE LOCATION = "DELHI" AND DEPT.DEPTID =
EMP.DEPTID;

+----------+

| EMPNAME |

+----------+

| ANYA |

| BINA |

| KULDEEP |

| SETH |

| SHIVANSH |

+----------+

5 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP,DEPT WHERE DEPTNAME = "SALES" AND DEPT.DEPTID =
EMP.DEPTID;

+---------+
| EMPNAME |

+---------+

| SMITH |

| MOMIN |

| AMIR |

+---------+

3 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP,DEPT WHERE LOCATION="DELHI" AND SAL>5000 AND
DEPT.DEPTID = EMP.DEPTID;

Empty set (0.00 sec)

mysql> SELECT * FROM EMP,DEPT WHERE DEPTNAME = "RESEARCH" AND DEPT.DEPTID = EMP.DEPTID;

+-------+-----------+-------------+------------+------+------+--------+--------+----------+-------+-----------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID | DeptID | DeptName | MgrID |
Location |

+-------+-----------+-------------+------------+------+------+--------+--------+----------+-------+-----------+

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL | 40 | 40 | RESEARCH | 8839 |


BANGALORE |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL | 40 | 40 | RESEARCH | 8839 |


BANGALORE |

+-------+-----------+-------------+------------+------+------+--------+--------+----------+-------+-----------+

2 rows in set (0.00 sec)


QUESTION IV
1. Create table Customer as per following table structure:
Column Name Cust ID Cust Name Cust Add Cust City Cust Phone
Datatype Number Varchar Varchar Varchar Varchar
Length 7 30 40 30 10
Constraints Primary Not Null

2. Insert 5 records with relevant information in the customer table


3. Update all the records as add “Mr.” with CustName.
4. Add one column Email of datatype varchar and size 30 to table customer.
5. Add one more column CustIncomeGroup of datatype varchar(10).
Column Name Order No. Cust No. Item Name Qty. Price
DataType Number Number Varchar Number Number
Length 5 7 30 5 6,2
Constraints Primary <=2 Not Null
6. Drop the column CustIncomeGroup from table customer.
7. Modify the column CustCity and change the size as 40 characters long.
8. Delete all records who belongs to Jaipur.
9. Create table ORDER as per following table structure . Also make CustNo. As
foreign key which refers CustId of Customer table.

10. Add 5 records as per defined constraints in order table.


11. Create a table TempCustomer from existing customer table with CustID ,
Cust Name and CustPhone columns.
12.Write command to show the tables in the MYORG database.
13.Drop the temp Customer Table .
14.Drop the Foreign Key Constraint from the order table.
15.Drop the database MYORG.

SOLUTION
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table CUSTOMER(CustID int(7) Primary Key,CustName varchar(30),Cust


Add varchar (40), CustCity varchar(30) Not Null, CustPhone varchar(10));

Query OK, 0 rows affected (0.09 sec)


mysql> insert into CUSTOMER values(12345, "Kunal”, “Bn-61 East Shalimar Bagh”, “Delhi”,
“9899605423”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(1234, "Saksham”, “DU-36 Pitampura”, “Delhi”,
“9899608459”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(123, "Mridul”, “BJ-50 Shalimar Bagh”, “Delhi”,
“9899548478”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(12, "Avika”, “137/50 Sec-22 Rohini”, “Delhi”,
“9899846423”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(1, "Rachit”, “120, Jhulelal Appartments”, “Delhi”,
“9899689754”);
Query OK, 1 row affected (0.01 sec)
mysql> update customer set CustName= concat("Mr.",CustName);
Query OK, 5 rows affected (0.03 sec)
Rows matched: 5 Changed: 5 Warnings: 0

mysql> alter table customer add EMAIL varchar(30);


Query OK, 5 rows affected (0.20 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> alter table customer add CustIncomeGroup varchar(10);


Query OK, 5 rows affected (0.25 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> alter table customer drop CustIncomeGroup;


Query OK, 5 rows affected (0.19 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> alter table customer modify CustCity varchar(40);


Query OK, 0 rows affected

mysql> delete from customer where CustCity=”Jaipur”; Query


OK, 0 rows affected (0.00 sec)

mysql> create table order(orderno int(5) Primary Key, CustNo int(7) references c
ustomer(CustID), ItemName varchar(30), Qty int(5) check(Qty>=2), Price decimal(6
,2) Not Null);

mysql> create table demand(orderno int(5) Primary Key, CustNo int(7) references
customer(CustID), ItemName varchar(30), Qty int(5) check(Qty>=2), Price decimal(
6,2) Not Null);
Query OK, 0 rows affected (0.42 sec)

mysql> insert into demand values (3451, 12345, "Rasna", 8, 200.00);


Query OK, 1 row affected (0.06 sec)
mysql> insert into demand values (3368, 1234, "Tang", 4, 650.00);
Query OK, 1 row affected (0.08 sec)

mysql> insert into demand values (6857, 123, "Nestle Dahi", 10, 800.00);
Query OK, 1 row affected (0.34 sec)

mysql> insert into demand values (6754, 12, "Blue Lays", 50, 1500.00);
Query OK, 1 row affected (0.09 sec)

mysql> insert into demand values (8754, 1, "Hide & Seek", 15, 500.00);
Query OK, 1 row affected (0.08 sec)

mysql> create view tempcustomer


-> as select CustID,CustName,CustPhone from customer;
Query OK, 0 rows affected (0.34 sec)

mysql> show tables;


+-----------------+
| Tables_in_myorg |
+-----------------+
| customer |
| demand |
| tempcustomer |
+-----------------+
3 rows in set (0.00 sec)

mysql> drop view tempname;


Query OK, 0 rows affected (0.00 sec)
mysql> alter table demand drop foreign key CustNo;
Query OK, 0 rows affected (0.12 sec)

mysql> drop database myorg;


Query OK, 2 rows affected (0.21 sec)

Column EmpID EmpName Designnation DOJ Sal comm DeptID


Name
Data Integer Varchar(30) Char(10) Date Integer Integer Integer
Type
Contraint Primary Not Null Check>1000 Foreign
Key Key

QUESTION I
31) Create and open database named MYORG.
32) Write a command to display the name of current month
33) Write command to display the system date
34) Write a query to find out the result of 6^3.
35) Write command to show the tables in the MYORG database.
36) Add one column state of datatype varchar and size 30 to table DEPT.
37) Create a table name EMP with following structure:
38) Insert the first record in table EMP (8369,SMITH,CLERK,18-12-
1990,800.00, 200.00, 10)
39) Write a query to display EmpName and sal of employees whose
salary are greater than or equal to 2200
40) Write a query to display the details of employee who are not getting
commission .
41) Write a query to display employee name and salary of those
employees who don’t have their salary in rang of 2500 to 4000.
42) Write a query to display the name of employee whose name contains
“A” as third alphabet in ascending order of employee names.
43) Display the sum of salary and commissions of employees as “Total
Incentive”.
44) Show the average salary for all departments with more than 5
working people .
45) Display the distinct designation offered by the organization.
46) List the count of employees grouped by DeptID
47) Display the names of employees who join on or after “01-05-1991”
48) Display the employee record in order by DOJ.
49) Display the maximum salary of employees in each department.
50) Update all the records as add “MR.” with EmpName .
51) Display the name of employees who are working in sales department
.
52) Drop the emp table.
53) Delete all the records who is working as “CLERK”.
54) Show the minimum,maximum and average salary of managers .
55) Increase the salary of managers by 15%.
56) To display the name of those employees whose location is “delhi”.
57) To display total salary of employees of sales department.
58) To show details of employees who joined in the year “1991”.
59) Delete all the records who is working as “SALESMAN” AND SALARY
MORE THAN 1500.
60) SET THE COMMISSION AS 100 WHO ARE NOT GETTING ANY
COMMISSION.
SOLUTION
Enter password: ****

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database MYORG;

Query OK, 1 row affected (0.00 sec)

mysql> use MYORG;

Database changed

mysql> SELECT MONTH(CURDATE());

+------------------+

| MONTH(CURDATE()) |

+------------------+

| 6|

+------------------+

1 row in set (0.00 sec)

mysql> select sysdate();


+---------------------+

| sysdate() |

+---------------------+

| 2019-06-15 16:17:05 |

+---------------------+

1 row in set (0.00 sec)

mysql> select pow(6,3);

+----------+

| pow(6,3) |

+----------+

| 216 |

+----------+

1 row in set (0.00 sec)

mysql> create table DEPT(DeptID int Primary Key, DeptName varchar(25), MgrID int

, Location varchar(25));

Query OK, 0 rows affected (0.52 sec)

mysql> insert into DEPT values(10, "Sales", 8566, "Mumbai");

Query OK, 1 row affected (0.02 sec)

mysql> insert into DEPT values(20, "Personel", 8698, "Delhi");

Query OK, 1 row affected (0.00 sec)

mysql> insert into DEPT values(30, "Accounts", 8882, "Delhi");

Query OK, 1 row affected (0.06 sec)


mysql> insert into DEPT values(40, "Research", 8839, "Bangalore");

Query OK, 1 row affected (0.00 sec)

mysql> select * from Dept;

+--------+----------+-------+-----------+

| DeptID | DeptName | MgrID | Location |

+--------+----------+-------+-----------+

| 10 | Sales | 8566 | Mumbai |

| 20 | Personel | 8698 | Delhi |

| 30 | Accounts | 8882 | Delhi |

| 40 | Research | 8839 | Bangalore |

+--------+----------+-------+-----------+

4 rows in set (0.00 sec)

mysql> alter table dept add state varchar(30);

Query OK, 4 rows affected (0.14 sec)

Records: 4 Duplicates: 0 Warnings: 0

mysql> create table emp(EmpID int primary key, EmpName varchar(30) Not Null,

Designation char(10), DOJ date, sal int check(sal>1000), comm int, DeptID int re

ferences DEPT(DeptID));

Query OK, 0 rows affected (0.50 sec)

mysql> alter table emp modify sal decimal(10,2);

Query OK, 0 rows affected (0.54 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into emp values(8369, "SMITH", "CLERK", "1990-12-18", 1050.00, 200

.00, 10);
Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(8499, "ANYA", "SALESMAN", "1991-02-20", 1600.00, 3

00.00, 20);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8566, "MAHADEVAN", "MANAGER", "1991-04-02", 2985.0

0, NULL, 30);

Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(8654, "MOMIN", "SALESMAN", "1991-09-28", 1250.00,

400.00, 20);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8698, "BINA", "MANAGER", "1991-01-05", 2850.00, 25

0.00, 30);

Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8882, "SHIVANSH", "MANAGER", "1991-06-09", 2450.00

, NULL, 10);

Query OK, 1 row affected (0.01 sec)

mysql> insert into emp values(8888, "SCOTT", "ANALYST", "1992-12-09", 3000.00,15

0.00, 10);

Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(8839, "AMIR", "PRESIDENT", "1991-11-18",


5000.00,Nhuguihhhuigkgugrowcyg

ULL, 20);
Query OK, 1 row affected (0.03 sec)

mysql> insert into emp values(8844, "KULDEEP", "SALESMAN", "1992-04-08", 1500.00

,0.00, 30);

Query OK, 1 row affected (0.50 sec)

mysql> SELECT * FROM EMP;

+-------+-----------+-------------+------------+---------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+---------+------+--------+

| 8369 | SMITH | CLERK | 1990-12-18 | 1050.00 | 200 | 10 |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600.00 | 300 | 20 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985.00 | NULL | 30 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250.00 | 400 | 20 |

| 8698 | BINA | MANAGER | 1991-01-05 | 2850.00 | 250 | 30 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000.00 | NULL | 20 |

| 8844 | KULDEEP | SALESMAN | 1992-04-08 | 1500.00 | 0 | 30 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450.00 | NULL | 10 |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000.00 | 150 | 10 |

+-------+-----------+-------------+------------+---------+------+--------+

9 rows in set (0.00 sec)

mysql> SELECT EmpName , sal from emp where sal>=2200;

+-----------+---------+

| EmpName | sal |

+-----------+---------+

| MAHADEVAN | 2985.00 |

| BINA | 2850.00 |

| AMIR | 5000.00 |
| SHIVANSH | 2450.00 |

| SCOTT | 3000.00 |

+-----------+---------+

5 rows in set (0.00 sec)

mysql> select * from emp where comm is null;

+-------+-----------+-------------+------------+---------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+---------+------+--------+

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985.00 | NULL | 30 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000.00 | NULL | 20 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450.00 | NULL | 10 |

+-------+-----------+-------------+------------+---------+------+--------+

3 rows in set (0.00 sec)

mysql> select empname, sal from emp where sal not between 2500 and 4000;

+----------+---------+

| empname | sal |

+----------+---------+

| SMITH | 1050.00 |

| ANYA | 1600.00 |

| MOMIN | 1250.00 |

| AMIR | 5000.00 |

| KULDEEP | 1500.00 |

| SHIVANSH | 2450.00 |

+----------+---------+

6 rows in set (0.00 sec)

mysql> select empname from emp where empname like("_A%") order by empname asc;
+-----------+

| empname |

+-----------+

| MAHADEVAN |

+-----------+

1 row in set (0.00 sec)

mysql> select sal+comm as "Total Incentive" from emp where comm is not null;

+-----------------+

| Total Incentive |

+-----------------+

| 1250.00 |

| 1900.00 |

| 1650.00 |

| 3100.00 |

| 1500.00 |

| 3150.00 |

+-----------------+

6 rows in set (0.00 sec)

mysql> select distinct designation from emp;

+-------------+

| designation |

+-------------+

| CLERK |

| SALESMAN |

| MANAGER |

| PRESIDENT |

| ANALYST |
+-------------+

5 rows in set (0.00 sec)

mysql> select count(*) from emp group by deptid;

+----------+

| count(*) |

+----------+

| 3|

| 3|

| 3|

+----------+

3 rows in set (0.02 sec)

mysql> select EmpName from emp where DOJ>=1991-05-01;

+-----------+

| EmpName |

+-----------+

| SMITH |

| ANYA |

| MAHADEVAN |

| MOMIN |

| BINA |

| AMIR |

| KULDEEP |

| SHIVANSH |

| SCOTT |

+-----------+

9 rows in set, 1 warning (0.00 sec)


mysql> select * from emp order By DOJ;

+-------+-----------+-------------+------------+------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+------+------+--------+

| 8369 | SMITH | CLERK | 1990-12-18 | 1050 | 200 | 10 |

| 8698 | BINA | MANAGER | 1991-01-05 | 2850 | 250 | 30 |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 | 20 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL | 30 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL | 10 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL | 20 |

| 8844 | KULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 |

+-------+-----------+-------------+------------+------+------+--------+

9 rows in set (0.00 sec)

mysql> select * from emp

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-----------+-------------+------------+------+------+--------+

| 8369 | SMITH | CLERK | 1990-12-18 | 1050 | 200 | 10 |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 | 20 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL | 30 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8698 | BINA | MANAGER | 1991-01-05 | 2850 | 250 | 30 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL | 20 |

| 8844 | KULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL | 10 |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 |


+-------+-----------+-------------+------------+------+------+--------+

9 rows in set (0.00 sec)

mysql> select max(sal) from emp group by deptID;

+----------+

| max(sal) |

+----------+

| 3000 |

| 5000 |

| 2985 |

+----------+

3 rows in set (0.00 sec)

mysql> update emp set empname=concat('Mr',EmpName);

Query OK, 9 rows affected (0.00 sec)

Rows matched: 9 Changed: 9 Warnings: 0

mysql> select empname from emp,dept where Designation="SALES" and emp.DeptID=dept.DeptID;

Empty set (0.01 sec)

mysql> delete from emp where designation="CLERK";

Query OK, 1 row affected (0.01 sec)

mysql> select min(sal),max(sal),avg(sal) from emp where designation="Manager";

+----------+----------+-----------+

| min(sal) | max(sal) | avg(sal) |

+----------+----------+-----------+

| 2450 | 2985 | 2761.6667 |

+----------+----------+-----------+
1 row in set (0.00 sec)

mysql> update emp set sal=sal+0.15*sal where designation="Manager";

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3 Changed: 3 Warnings: 0

mysql> select EmpName from emp,dept where location="Delhi" and emp.deptid=dept.deptid;

+-------------+

| EmpName |

+-------------+

| MrANYA |

| MrMAHADEVAN |

| MrMOMIN |

| MrBINA |

| MrAMIR |

| MrKULDEEP |

+-------------+

6 rows in set (0.00 sec)

mysql> select sum(sal) from emp,dept where designation="SALES" and emp.deptid=dept.deptid;

+----------+

| sum(sal) |

+----------+

| NULL |

+----------+

1 row in set (0.00 sec)

mysql> select sum(sal) from emp where designation="sales";

+----------+
| sum(sal) |

+----------+

| NULL |

+----------+

1 row in set (0.00 sec)

mysql> select * from emp where year(DOJ)=1991;

+-------+-------------+-------------+------------+------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |

+-------+-------------+-------------+------------+------+------+--------+

| 8499 | MrANYA | SALESMAN | 1991-02-20 | 1600 | 300 | 20 |

| 8566 | MrMAHADEVAN | MANAGER | 1991-04-02 | 3433 | NULL | 30 |

| 8654 | MrMOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8698 | MrBINA | MANAGER | 1991-01-05 | 3278 | 250 | 30 |

| 8839 | MrAMIR | PRESIDENT | 1991-11-18 | 5000 | NULL | 20 |

| 8882 | MrSHIVANSH | MANAGER | 1991-06-09 | 2818 | NULL | 10 |

+-------+-------------+-------------+------------+------+------+--------+

6 rows in set (0.00 sec)

mysql> delete from emp where designation="salesman" and sal>1500;

Query OK, 1 row affected (0.00 sec)

mysql> update emp set comm=100 where comm is null;

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3 Changed: 3 Warnings: 0

mysql> select * from emp;

+-------+-------------+-------------+------------+------+------+--------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID |


+-------+-------------+-------------+------------+------+------+--------+

| 8566 | MrMAHADEVAN | MANAGER | 1991-04-02 | 3433 | 100 | 30 |

| 8654 | MrMOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 |

| 8698 | MrBINA | MANAGER | 1991-01-05 | 3278 | 250 | 30 |

| 8839 | MrAMIR | PRESIDENT | 1991-11-18 | 5000 | 100 | 20 |

| 8844 | MrKULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 |

| 8882 | MrSHIVANSH | MANAGER | 1991-06-09 | 2818 | 100 | 10 |

| 8888 | MrSCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 |

+-------+-------------+-------------+------------+------+------+--------+

7 rows in set (0.00 sec)

mysql> select * from emp,dept where dept.deptid=emp.deptid;

+-------+-------------+-------------+------------+------+------+--------+--------+----------+-------+----------+-------+

| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID | DeptID | DeptName | MgrID |
Location | state |

+-------+-------------+-------------+------------+------+------+--------+--------+----------+-------+----------+-------+

| 8566 | MrMAHADEVAN | MANAGER | 1991-04-02 | 3433 | 100 | 30 | 30 | Accounts | 8882 |


Delhi | NULL |

| 8654 | MrMOMIN | SALESMAN | 1991-09-28 | 1250 | 400 | 20 | 20 | Personel | 8698 |


Delhi | NULL |

| 8698 | MrBINA | MANAGER | 1991-01-05 | 3278 | 250 | 30 | 30 | Accounts | 8882 | Delhi


| NULL |

| 8839 | MrAMIR | PRESIDENT | 1991-11-18 | 5000 | 100 | 20 | 20 | Personel | 8698 | Delhi


| NULL |

| 8844 | MrKULDEEP | SALESMAN | 1992-04-08 | 1500 | 0 | 30 | 30 | Accounts | 8882 | Delhi


| NULL |

| 8882 | MrSHIVANSH | MANAGER | 1991-06-09 | 2818 | 100 | 10 | 10 | Sales | 8566 |


Mumbai | NULL |

| 8888 | MrSCOTT | ANALYST | 1992-12-09 | 3000 | 150 | 10 | 10 | Sales | 8566 | Mumbai


| NULL |

+-------+-------------+-------------+------------+------+------+--------+--------+----------+-------+----------+-------+

7 rows in set (0.00 sec)


QUESTION II
20. Write a command to display the system date .
21.Write a command to display the name of current month
22.Write command to print the day of the week of your birthday in the year
2015
23.Write the command to round off value 15.193 to nearest tens ie.20.
24.Write a query to find the out the result of 6^3.

EmpID EmpName Designation DOJ Sal Comm


Number Varchar Char(10) Date Number Number
25.Create and open database named MYORG.
26.Create a table name EMP with following structure (EMPID Primary Key )

27. Insert the following records :


EmpID EmpName Designation DOJ Sal Comm
8369 SMITH CLERK 1990-12-18 800.00 NULL
8499 ANYA SALESMAN 1991-02-20 1600.00 300.00
8521 SETH SALESMAN 1991-02-22 1250.00 500.00
8566 MAHADEVAN MANAGER 1991-04-02 2985.00 NULL
8654 MOMIN SALESMAN 1991-09-08 1250.00 NULL
8698 BINA MANAGER 1991-05-01 2850.00 NULL
8882 SHIVANSH MANAGER 1991-06-09 2450.00 NULL
8888 SCOTT ANALYST 1992-12-09 3000.00 NULL
8839 AMAR PRESIDENT 1991-11-18 5000.00 NULL

8844 KULDEEP SALESMAN 1991-09-08 1500.00 0.00

28. Write a query to display all the records with all the columns .
29.Write a query to display EmpName and sal of employees whose salary are
greater than or equal to 2200.
30.Write a query to display details of employees who are not getting
commission .
31.Write a query to display employee name and salary of those employees
who don’t have their salary in range of 2500 to 4000.
32.Write a query to display the name of employee whose name contains “a”
as third alphabet in ascending order of employee names.
33.Write a query to display the ENAME and sal with 50% of sal as DA.
34.Display the distinct job titles offered by the organization
35.Display the names of employees who are working as manager or analyst .
36.Display the name of employees who join on or after 01-05-1991.
37.Display the employee record in order by DOJ.
38.Display the distinct designation in the organization .

SOLUTION
Enter password: *********

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select sysdate();

+---------------------+

| sysdate() |

+---------------------+

| 2019-06-27 19:31:46 |

+---------------------+

1 row in set (0.00 sec)


mysql> select dayofweek("2015-12-30");

+-------------------------+

| dayofweek("2015-12-30") |

+-------------------------+

| 4|

+-------------------------+

1 row in set (0.00 sec)

mysql> select round(15.193,2);

+-----------------+

| round(15.193,2) |

+-----------------+

| 15.19 |

+-----------------+

1 row in set (0.00 sec)

mysql> select round(15.193,1);

+-----------------+

| round(15.193,1) |

+-----------------+

| 15.2 |

+-----------------+

1 row in set (0.00 sec)

mysql> select pow(6,3);

+----------+

| pow(6,3) |

+----------+
| 216 |

+----------+

1 row in set (0.00 sec)

mysql> drop database MYORG;

Query OK, 2 rows affected (0.03 sec)

mysql> create database MYORG;

Query OK, 1 row affected (0.00 sec)

mysql> USE MYORG;

Database changed

mysql> create table Emp(EmpID int primary key, EmpName varchar(25), Designation char(10), DOJ date,
sal int, comm int);

Query OK, 0 rows affected (0.01 sec)

mysql> insert into Emp values(8369, "SMITH", "CLERK", "1990-12-18", 800.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8499, "ANYA", "SALESMAN", "1991-02-20", 1600.00, 300.00);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8521, "SETH", "SALESMAN", "1991-02-22", 1250.00, 500.00);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8566, "MAHADEVAN", "MANAGER", "1991-04-02", 2985.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8654, "MOMIN", "SALESMAN", "1991-09-28", 1250.00, 400.00);
Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8698, "BINA", "MANAGER", "1991-05-01", 2850.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8882, "SHIVANSH", "MANAGER", "1991-06-09", 2450.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8888, "SCOTT", "ANALYST", "1992-12-09", 3000.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8839, "AMIR", "PRESIDENT", "1991-11-18", 5000.00, NULL);

Query OK, 1 row affected (0.00 sec)

mysql> insert into Emp values(8844, "KULDEEP", "SALESMAN", "1991-09-08", 1500.00, 0.00);

Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM EMP;

+-------+-----------+-------------+------------+------+------+

| EmpID | EmpName | Designation | DOJ | sal | comm |

+-------+-----------+-------------+------------+------+------+

| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |

| 8521 | SETH | SALESMAN | 1991-02-22 | 1250 | 500 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |

| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |

| 8844 | KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |


| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |

+-------+-----------+-------------+------------+------+------+

10 rows in set (0.00 sec)

mysql> SELECT EMPNAME, SAL FROM EMP WHERE SAL>=2200;

+-----------+------+

| EMPNAME | SAL |

+-----------+------+

| MAHADEVAN | 2985 |

| BINA | 2850 |

| AMIR | 5000 |

| SHIVANSH | 2450 |

| SCOTT | 3000 |

+-----------+------+

5 rows in set (0.00 sec)

mysql> SELECT * FROM EMP WHERE COMM IS NULL;

+-------+-----------+-------------+------------+------+------+

| EmpID | EmpName | Designation | DOJ | sal | comm |

+-------+-----------+-------------+------------+------+------+

| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |

| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |

| 8882 | SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |

+-------+-----------+-------------+------------+------+------+

6 rows in set (0.00 sec)


mysql> SELECT EMPNAME, SAL FROM EMP WHERE SAL BETWEEN ! 2500 AND 4000;

+-----------+------+

| EMPNAME | SAL |

+-----------+------+

| SMITH | 800 |

| ANYA | 1600 |

| SETH | 1250 |

| MAHADEVAN | 2985 |

| MOMIN | 1250 |

| BINA | 2850 |

| KULDEEP | 1500 |

| SHIVANSH | 2450 |

| SCOTT | 3000 |

+-----------+------+

9 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP WHERE EMPNAME LIKE("__A%") ORDER BY EMPNAME ASC;

Empty set (0.00 sec)

mysql> SELECT EMPNAME, SAL, (50*SAL)/100 AS DA FROM EMP;

+-----------+------+-----------+

| EMPNAME | SAL | DA |

+-----------+------+-----------+

| SMITH | 800 | 400.0000 |

| ANYA | 1600 | 800.0000 |

| SETH | 1250 | 625.0000 |

| MAHADEVAN | 2985 | 1492.5000 |

| MOMIN | 1250 | 625.0000 |


| BINA | 2850 | 1425.0000 |

| AMIR | 5000 | 2500.0000 |

| KULDEEP | 1500 | 750.0000 |

| SHIVANSH | 2450 | 1225.0000 |

| SCOTT | 3000 | 1500.0000 |

+-----------+------+-----------+

10 rows in set (0.00 sec)

mysql> SELECT DISTINCT DESIGNATION FROM EMP;

+-------------+

| DESIGNATION |

+-------------+

| CLERK |

| SALESMAN |

| MANAGER |

| PRESIDENT |

| ANALYST |

+-------------+

5 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP WHERE DESIGNATION IN("MANAGER", "ANALYST");

+-----------+

| EMPNAME |

+-----------+

| MAHADEVAN |
| BINA |

| SHIVANSH |

| SCOTT |

+-----------+

4 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP WHERE DOJ>=1991-05-01;

+-----------+

| EMPNAME |

+-----------+

| SMITH |

| ANYA |

| SETH |

| MAHADEVAN |

| MOMIN |

| BINA |

| AMIR |

| KULDEEP |

| SHIVANSH |

| SCOTT |

+-----------+

10 rows in set, 1 warning (0.00 sec)

mysql> SELECT * FROM EMP ORDER BY DOJ;

+-------+-----------+-------------+------------+------+------+

| EmpID | EmpName | Designation | DOJ | sal | comm |

+-------+-----------+-------------+------------+------+------+

| 8369 | SMITH | CLERK | 1990-12-18 | 800 | NULL |

| 8499 | ANYA | SALESMAN | 1991-02-20 | 1600 | 300 |


| 8521 | SETH | SALESMAN | 1991-02-22 | 1250 | 500 |

| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL |

| 8698 | BINA | MANAGER | 1991-05-01 | 2850 | NULL |

|
8882 DeptID DeptName MgrID Location
| Number VarChar(25) Number Char(40)
SHIVANSH | MANAGER | 1991-06-09 | 2450 | NULL |

| 8844 | KULDEEP | SALESMAN | 1991-09-08 | 1500 | 0 |

| 8654 | MOMIN | SALESMAN | 1991-09-28 | 1250 | 400 |

| 8839 | AMIR | PRESIDENT | 1991-11-18 | 5000 | NULL |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL |

+-------+-----------+-------------+------------+------+------+

10 rows in set (0.00 sec)

mysql> SELECT DISTINCT DESIGNATION FROM EMP;

+-------------+

| DESIGNATION |

+-------------+

| CLERK |

| SALESMAN |

| MANAGER |

| PRESIDENT |

| ANALYST |

+-------------+

5 rows in set (0.00 sec)

QUESTION III
21.Open database MYORG.
22.Create the following table DEPT with DeptId as Primary Key .
23.Insert the following in the Dept Table:

24.After the table Emp as add a column DeptId (Number).


25.Add foreign Key as DeptID which refers DeptId column of Dept Table
26.Update DeptID of EMP table with valid DeptID’s to link both the tables .
27. Show the minimum and maximum and average salary of managers.
28.Count the number of clerk in the organization .
29.Display the designation wise list of employees with name, sal and date of
joining.
DeptID DeptName MgrID Location
10 Sales 8566 Mumbai
20 Personnel 8698 Delhi
30 Accounts 8882 Delhi
40 Research 8839 Bangalore
30.Count the number of employees who are not getting commission.
31. Show the average salary for all departments with more than 5 working
people.
32.List the count of employees grouped by DeptID.
33.Display the maximum salary of employees in each department.
34.Display the name of employees along with their designation and
department name .
35.Count the number of employees working in accounts department.
36.Display the name of employees working in delhi.
37.Display the name of employee working in the same city from where they
belongs.
38.Display the name of employees whose is managing sales department.
39.Display the name of employees who are working in delhi and getting more
than 5000.
40.Display the details of employees who are working in research department.

SOLUTION
Enter password: *********

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1


Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use myorg;

Database changed

mysql> show tables;

+-----------------+

mysql> create table DEPT(DeptID int, DeptName varchar, MgrID int, Location char(40));

Query OK, 0 rows affected (0.01 sec)

+-----------------+

mysql> insert into DEPT values(10, “Sales”, 8566, “Mumbai”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> insert into DEPT values(20, “Personel”, 8698, “Delhi”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> insert into DEPT values(30, “Accounts”, 8882, “Delhi”);

Query OK, 1 row affected(0.00 sec)

+-----------------+

mysql> insert into DEPT values(40, “Research”, 8839, “Bangalore”);

Query OK, 1 row affected(0.00 sec)

+-----------------+
mysql> alter table EMP add DeptID(int);

Query OK, 0 row affected(0.01 sec)

+-----------------+

mysql> update emp set DeptID = 10 where EmpName = "SMITH";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 20 where EmpName = "ANYA";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 30 where EmpName = "SETH";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 40 where EmpName = "MAHADEVAN";

Query OK, 1 row affected (0.02 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 10 where EmpName = "MOMIN";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 20 where EmpName = "BINA";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 30 where EmpName = "SHIVANSH";


Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 40 where EmpName = "SCOTT";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 10 where EmpName = "AMIR";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update emp set DeptID = 20 where EmpName = "KULDEEP";

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> ALTER TABLE EMP ADD FOREIGN KEY(DEPTID) REFERENCES DEPT(DEPTID);

Query OK, 10 rows affected (0.02 sec)

Records: 10 Duplicates: 0 Warnings: 0

mysql> SELECT MIN(SAL), MAX(SAL), AVG(SAL) FROM EMP WHERE DESIGNATION="MANAGER";

+----------+----------+-----------+

| MIN(SAL) | MAX(SAL) | AVG(SAL) |

+----------+----------+-----------+

| 2450 | 2985 | 2761.6667 |

+----------+----------+-----------+

1 row in set (0.00 sec)

mysql> SELECT COUNT(DESIGNATION) FROM EMP WHERE DESIGNATION="CLERK";

+--------------------+
| COUNT(DESIGNATION) |

+--------------------+

| 1|

+--------------------+

1 row in set (0.00 sec)

mysql> SELECT EMPNAME, SAL, DOJ FROM EMP GROUP BY DESIGNATION;

+-----------+------+------------+

| EMPNAME | SAL | DOJ |

+-----------+------+------------+

| SCOTT | 3000 | 1992-12-09 |

| SMITH | 800 | 1990-12-18 |

| MAHADEVAN | 2985 | 1991-04-02 |

| AMIR | 5000 | 1991-11-18 |

| ANYA | 1600 | 1991-02-20 |

+-----------+------+------------+

5 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM EMP WHERE COMM IS NULL;

+----------+

| COUNT(*) |

+----------+

| 6|

+----------+

1 row in set (0.00 sec)

mysql> SELECT AVG(SAL) FROM EMP,DEPT GROUP BY DEPTNAME HAVING COUNT(*)>5;

+-----------+

| AVG(SAL) |
+-----------+

| 2268.5000 |

| 2268.5000 |

| 2268.5000 |

| 2268.5000 |

+-----------+

4 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM EMP GROUP BY DEPTID;

+----------+

| COUNT(*) |

+----------+

| 3|

| 3|

| 2|

| 2|

+----------+

4 rows in set (0.00 sec)

mysql> SELECT MAX(SAL) FROM EMP,DEPT WHERE DEPT.DEPTID = EMP.DEPTID GROUP BY DEPTNAME;

+----------+

| MAX(SAL) |

+----------+

| 2450 |

| 2850 |

| 3000 |

| 5000 |

+----------+
4 rows in set (0.00 sec)

mysql> SELECT EMPNAME, DESIGNATION, DEPTNAME FROM EMP,DEPT WHERE DEPT.DEPTID =


EMP.DEPTID;

+-----------+-------------+-----------+

| EMPNAME | DESIGNATION | DEPTNAME |

+-----------+-------------+-----------+

| SMITH | CLERK | SALES |

| MOMIN | SALESMAN | SALES |

| AMIR | PRESIDENT | SALES |

| ANYA | SALESMAN | PERSONEL |

| BINA | MANAGER | PERSONEL |

| KULDEEP | SALESMAN | PERSONEL |

| SETH | SALESMAN | ACCOUNTS |

| SHIVANSH | MANAGER | ACCOUNTS |

| MAHADEVAN | MANAGER | RESEARCH |

| SCOTT | ANALYST | RESEARCH |

+-----------+-------------+-----------+

10 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM EMP,DEPT WHERE DEPTNAME="ACCOUNTS" AND DEPT.DEPTID =


EMP.DEPTID;

+----------+

| COUNT(*) |

+----------+

| 2|

+----------+

1 row in set (0.00 sec)


mysql> SELECT EMPNAME FROM EMP,DEPT WHERE LOCATION = "DELHI" AND DEPT.DEPTID =
EMP.DEPTID;

+----------+

| EMPNAME |

+----------+

| ANYA |

| BINA |

| KULDEEP |

| SETH |

| SHIVANSH |

+----------+

5 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP,DEPT WHERE DEPTNAME = "SALES" AND DEPT.DEPTID =
EMP.DEPTID;

+---------+

| EMPNAME |

+---------+

| SMITH |

| MOMIN |

| AMIR |

+---------+

3 rows in set (0.00 sec)

mysql> SELECT EMPNAME FROM EMP,DEPT WHERE LOCATION="DELHI" AND SAL>5000 AND
DEPT.DEPTID = EMP.DEPTID;

Empty set (0.00 sec)

mysql> SELECT * FROM EMP,DEPT WHERE DEPTNAME = "RESEARCH" AND DEPT.DEPTID = EMP.DEPTID;

+-------+-----------+-------------+------------+------+------+--------+--------+----------+-------+-----------+
| EmpID | EmpName | Designation | DOJ | sal | comm | DeptID | DeptID | DeptName | MgrID |
Location |

+-------+-----------+-------------+------------+------+------+--------+--------+----------+-------+-----------+

Column Name Cust ID Cust Name Cust Add Cust City Cust Phone
Datatype Number Varchar Varchar Varchar Varchar
Length 7 30 40 30 10
Constraints Primary Not Null
| 8566 | MAHADEVAN | MANAGER | 1991-04-02 | 2985 | NULL | 40 | 40 | RESEARCH | 8839 |
BANGALORE |

| 8888 | SCOTT | ANALYST | 1992-12-09 | 3000 | NULL | 40 | 40 | RESEARCH | 8839 |


BANGALORE |

+-------+-----------+-------------+------------+------+------+--------+--------+----------+-------+-----------+

2 rows in set (0.00 sec)

QUESTION IV
16.Create table Customer as per following table structure:

17.Insert 5 records with relevant information in the customer table


18.Update all the records as add “Mr.” with CustName.
19.Add one column Email of datatype varchar and size 30 to table customer.
20. Add one more column CustIncomeGroup of datatype varchar(10).
21.Drop the column CustIncomeGroup from table customer.
22.Modify the column CustCity and change the size as 40 characters long.
23. Delete all records who belongs to Jaipur.
24.Create table ORDER as per following table structure . Also make CustNo. As
foreign key which refers CustId of Customer table.

25. Add 5 records as per defined constraints in order table.


26. Create a table TempCustomer from existing customer table with CustID ,
Cust Name and CustPhone columns.
27.Write command to show the tables in the MYORG database.
28.Drop the temp Customer Table .
29.Drop the Foreign Key Constraint from the order table.
30.Drop the database MYORG.

SOLUTION
Column Name Order No. Cust No. Item Name Qty. Price
DataType Number Number Varchar Number Number
Length 5 7 30 5 6,2
Constraints Primary <=2 Not Null
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table CUSTOMER(CustID int(7) Primary Key,CustName varchar(30),Cust


Add varchar (40), CustCity varchar(30) Not Null, CustPhone varchar(10));

Query OK, 0 rows affected (0.09 sec)


mysql> insert into CUSTOMER values(12345, "Kunal”, “Bn-61 East Shalimar Bagh”, “Delhi”,
“9899605423”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(1234, "Saksham”, “DU-36 Pitampura”, “Delhi”,
“9899608459”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(123, "Mridul”, “BJ-50 Shalimar Bagh”, “Delhi”,
“9899548478”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(12, "Avika”, “137/50 Sec-22 Rohini”, “Delhi”,
“9899846423”);

Query OK, 1 row affected (0.01 sec)


mysql> insert into CUSTOMER values(1, "Rachit”, “120, Jhulelal Appartments”, “Delhi”,
“9899689754”);

Query OK, 1 row affected (0.01 sec)


mysql> update customer set CustName= concat("Mr.",CustName);
Query OK, 5 rows affected (0.03 sec)
Rows matched: 5 Changed: 5 Warnings: 0

mysql> alter table customer add EMAIL varchar(30);


Query OK, 5 rows affected (0.20 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> alter table customer add CustIncomeGroup varchar(10);


Query OK, 5 rows affected (0.25 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> alter table customer drop CustIncomeGroup;


Query OK, 5 rows affected (0.19 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> alter table customer modify CustCity varchar(40);
Query OK, 0 rows affected

mysql> delete from customer where CustCity=”Jaipur”; Query


OK, 0 rows affected (0.00 sec)

mysql> create table order(orderno int(5) Primary Key, CustNo int(7) references c
ustomer(CustID), ItemName varchar(30), Qty int(5) check(Qty>=2), Price decimal(6
,2) Not Null);

mysql> create table demand(orderno int(5) Primary Key, CustNo int(7) references
customer(CustID), ItemName varchar(30), Qty int(5) check(Qty>=2), Price decimal(
6,2) Not Null);
Query OK, 0 rows affected (0.42 sec)

mysql> insert into demand values (3451, 12345, "Rasna", 8, 200.00);


Query OK, 1 row affected (0.06 sec)

mysql> insert into demand values (3368, 1234, "Tang", 4, 650.00);


Query OK, 1 row affected (0.08 sec)

mysql> insert into demand values (6857, 123, "Nestle Dahi", 10, 800.00);
Query OK, 1 row affected (0.34 sec)

mysql> insert into demand values (6754, 12, "Blue Lays", 50, 1500.00);
Query OK, 1 row affected (0.09 sec)

mysql> insert into demand values (8754, 1, "Hide & Seek", 15, 500.00);
Query OK, 1 row affected (0.08 sec)

mysql> create view tempcustomer


-> as select CustID,CustName,CustPhone from customer;
Query OK, 0 rows affected (0.34 sec)

mysql> show tables;


+-----------------+
| Tables_in_myorg |
+-----------------+
| customer |
| demand |
| tempcustomer |
+-----------------+
3 rows in set (0.00 sec)

mysql> drop view tempname;


Query OK, 0 rows affected (0.00 sec)

mysql> alter table demand drop foreign key CustNo;


Query OK, 0 rows affected (0.12 sec)

mysql> drop database myorg;


Query OK, 2 rows affected (0.21 sec)

Вам также может понравиться