You are on page 1of 24

PART B

[MySQL]

Page 1 of 24
INDEX

SNO Topic Page No.

1. MySQL Assignment – I (Based on Single Table)

2. MySQL Assignment – II (Based on Multiple Tables)

3. MySQL Assignment – III (Based on Multiple Tables)

4. MySQL Assignment – IV (Based on Multiple Tables)

5. MySQL Assignment – V (Based on Multiple Tables)

Page 2 of 24
Assignment I

Create following table ‘EMP’ and write SQL commands for A) to V)

ENO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7499 Ashok Singhal Salesman 7968 1981-02-20 4000 0 30
7521 Rohit Rana Salesman 7968 1981-02-22 5250 500 30
7566 Jyoti LAmba Manager 7839 1981-04-02 4500 NULL 20
7782 Chetan Gupta Manager 7839 1981-06-09 3000 NULL 10
7788 Sudhir Rawat Analyst 7566 1981-04-19 5000 NULL 20
7839 Kavita Sharma President NULL 1981-11-17 6000 NULL 10
7900 Jagdeep Rana Clerk 7968 1981-12-03 4500 NULL 30

A) To select all the columns of the above table

B) To list the name and employee number from the above table.
C)
To display the employee name and the incremented value of Sal as 30% increase in Sal.

D) To list the employee name and its annual salary(Annual salary=12*Sal +100)

E) Display the name of the employee and the salary where commission is null.

F) To list the distinct department number from the table.

G) To list the unique jobs from the table.

H) To list the records where salary is less than the commission.

I) To list the job, salary and names of the employees starting with ‘S’

J) To list all the details of employees getting a salary more than 3100.

K) To list all the columns in ascending order of Hiredate.

L) To list all the columns in ascending order of DeptNo and descending order of salary.

Display the names of the employees and their department number of all employees in
M)
department 20 and 30 in alphabetical order.
N) To list name and hiredate of all the employees who are hired in 1981.
O) To list all the employees who do not have manager.
P) To list the names of the employees whose second letter in their name is ‘a’.
Q) To display job wise maximum and minimum salary.
Page 3 of 24
R) To count the number of each department.
S) To Increase the salaries of all employees by 10%
T) To Increase the salaries of all employees who have joined in the year 1981 by 5%
U) To reduce the salaries of those salesmen whose commission is 0 by 5%
V) To remove the record of those salesmen whose commission is zero.

SOLUTION:
mysql> create table EMP
-> (
-> ENO int Primary Key,
-> Ename varchar(30) not null,
-> Job char(9) not null,
-> MGR int,
-> Hiredate Date not null,
-> Sal int not null,
-> Comm int,
-> Deptno int not null
-> );
Query OK, 0 rows affected (0.03 sec)

mysql> insert into emp


-> values(7499,'Ashok Singhal','Salesman',7968,'1981-02-20',4000,0,30);
Query OK, 1 row affected (0.01 sec)

mysql> insert into emp


-> values(7521,'Rohit Rana','Salesman',7968,'1981-02-22',5250,500,30);
Query OK, 1 row affected (0.01 sec)

mysql> insert into emp


-> values(7566,'Jyoti Lamba','Manager',7839,'1981-04-02',4500,NULL,20);
Query OK, 1 row affected (0.01 sec)

mysql> insert into emp


-> values(7782,'Chetan Gupta','Manager',7839,'1981-06-09',3000,NULL,10);
Query OK, 1 row affected (0.01 sec)

mysql> insert into emp


-> values(7788,'Sudhir Rawat','Analyst',7566,'1981-04-19',5000,NULL,20);
Query OK, 1 row affected (0.01 sec)

mysql> insert into emp


-> values(7839,'Kavita Sharma','PRESIDENT',NULL,'1981-11-17',6000,NULL,10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp


-> values(7900,'Jagdeep Rana','Clerk',7968,'1981-12-03',4500,NULL,30);
Query OK, 1 row affected (0.01 sec)

mysql> Select * From Emp;


+------+---------------+-----------+------+------------+------+------+--------+
Page 4 of 24
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4000 | 0 | 30 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 5250 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 4500 | NULL | 20 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3000 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5000 | NULL | 20 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6000 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 4500 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

mysql> Select Ename, Eno from Emp;


+---------------+------+
| Ename | Eno |
+---------------+------+
| Ashok Singhal | 7499 |
| Rohit Rana | 7521 |
| Jyoti Lamba | 7566 |
| Chetan Gupta | 7782 |
| Sudhir Rawat | 7788 |
| Kavita Sharma | 7839 |
| Jagdeep Rana | 7900 |
+---------------+------+
7 rows in set (0.00 sec)

mysql> Select Ename, Sal*1.3 "Increased Salary" From Emp;


+---------------+------------------+
| Ename | Increased Salary |
+---------------+------------------+
| Ashok Singhal | 5200.0 |
| Rohit Rana | 6825.0 |
| Jyoti Lamba | 5850.0 |
| Chetan Gupta | 3900.0 |
| Sudhir Rawat | 6500.0 |
| Kavita Sharma | 7800.0 |
| Jagdeep Rana | 5850.0 |
+---------------+------------------+
7 rows in set (0.00 sec)

mysql> Select Ename, Sal*1.3 as "Increased Salary" From Emp;


+---------------+------------------+
| Ename | Increased Salary |
+---------------+------------------+
| Ashok Singhal | 5200.0 |
| Rohit Rana | 6825.0 |
| Jyoti Lamba | 5850.0 |
| Chetan Gupta | 3900.0 |
| Sudhir Rawat | 6500.0 |
| Kavita Sharma | 7800.0 |
| Jagdeep Rana | 5850.0 |
+---------------+------------------+
7 rows in set (0.00 sec)

mysql> Select Ename, Sal*12 + 100 as "Annual Salary" From Emp;


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

Page 5 of 24
| Ename | Annual Salary |
+---------------+---------------+
| Ashok Singhal | 48100 |
| Rohit Rana | 63100 |
| Jyoti Lamba | 54100 |
| Chetan Gupta | 36100 |
| Sudhir Rawat | 60100 |
| Kavita Sharma | 72100 |
| Jagdeep Rana | 54100 |
+---------------+---------------+
7 rows in set (0.00 sec)

mysql> Select Ename, Sal*12 + 100 "Annual Salary" From Emp;


+---------------+---------------+
| Ename | Annual Salary |
+---------------+---------------+
| Ashok Singhal | 48100 |
| Rohit Rana | 63100 |
| Jyoti Lamba | 54100 |
| Chetan Gupta | 36100 |
| Sudhir Rawat | 60100 |
| Kavita Sharma | 72100 |
| Jagdeep Rana | 54100 |
+---------------+---------------+
7 rows in set (0.00 sec)

mysql> Select Ename, Sal from Emp where comm is Null;


+---------------+------+
| Ename | Sal |
+---------------+------+
| Jyoti Lamba | 4500 |
| Chetan Gupta | 3000 |
| Sudhir Rawat | 5000 |
| Kavita Sharma | 6000 |
| Jagdeep Rana | 4500 |
+---------------+------+
5 rows in set (0.00 sec)

mysql> Select Distinct Deptno from Emp;


+--------+
| Deptno |
+--------+
| 30 |
| 20 |
| 10 |
+--------+
3 rows in set (0.00 sec)

mysql> Select Distinct Job from Emp;


+-----------+
| Job |
+-----------+
| Salesman |
| Manager |
| Analyst |
| PRESIDENT |
| Clerk |
+-----------+
5 rows in set (0.00 sec)

Page 6 of 24
mysql> Select * From Emp where Sal<Comm;
Empty set (0.00 sec)

mysql> Select Ename, Job, Sal From Emp where Ename like "S%";
+--------------+---------+------+
| Ename | Job | Sal |
+--------------+---------+------+
| Sudhir Rawat | Analyst | 5000 |
+--------------+---------+------+
1 row in set (0.00 sec)

mysql> Select * From Emp where Sal>3100;


+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4000 | 0 | 30 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 5250 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 4500 | NULL | 20 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5000 | NULL | 20 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6000 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 4500 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
6 rows in set (0.00 sec)

mysql> Select * From Emp order by Hiredate;


+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4000 | 0 | 30 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 5250 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 4500 | NULL | 20 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5000 | NULL | 20 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3000 | NULL | 10 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6000 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 4500 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

mysql> Select * From Emp order by Deptno, Sal Desc;


+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6000 | NULL | 10 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3000 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5000 | NULL | 20 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 4500 | NULL | 20 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 5250 | 500 | 30 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 4500 | NULL | 30 |
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4000 | 0 | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

mysql> Select Ename, Deptno From Emp where Deptno in (20, 30) order by Ename;

Page 7 of 24
+---------------+--------+
| Ename | Deptno |
+---------------+--------+
| Ashok Singhal | 30 |
| Jagdeep Rana | 30 |
| Jyoti Lamba | 20 |
| Rohit Rana | 30 |
| Sudhir Rawat | 20 |
+---------------+--------+
5 rows in set (0.00 sec)

mysql> Select Ename, Hiredate from Emp where Hiredate between "1981-01-01" and "1981-
12-31";
+---------------+------------+
| Ename | Hiredate |
+---------------+------------+
| Ashok Singhal | 1981-02-20 |
| Rohit Rana | 1981-02-22 |
| Jyoti Lamba | 1981-04-02 |
| Chetan Gupta | 1981-06-09 |
| Sudhir Rawat | 1981-04-19 |
| Kavita Sharma | 1981-11-17 |
| Jagdeep Rana | 1981-12-03 |
+---------------+------------+
7 rows in set (0.00 sec)

mysql> Select Ename from Emp where Mgr is Null;


+---------------+
| Ename |
+---------------+
| Kavita Sharma |
+---------------+
1 row in set (0.00 sec)

mysql> Select Ename from Emp where Ename like "_A%";


+---------------+
| Ename |
+---------------+
| Kavita Sharma |
| Jagdeep Rana |
+---------------+
2 rows in set (0.00 sec)

mysql> Select Job, Max(Sal),Min(Sal) from Emp Group by Job;


+-----------+----------+----------+
| Job | Max(Sal) | Min(Sal) |
+-----------+----------+----------+
| Salesman | 5250 | 4000 |
| Manager | 4500 | 3000 |
| Analyst | 5000 | 5000 |
| PRESIDENT | 6000 | 6000 |
| Clerk | 4500 | 4500 |
+-----------+----------+----------+
5 rows in set (0.00 sec)

mysql> Select Deptno, Count(*) From Emp group by Deptno;

Page 8 of 24
+--------+----------+
| Deptno | Count(*) |
+--------+----------+
| 30 | 3 |
| 20 | 2 |
| 10 | 2 |
+--------+----------+
3 rows in set (0.00 sec)

mysql> Update Emp Set Sal=Sal*1.1;


Query OK, 7 rows affected (0.01 sec)
Rows matched: 7 Changed: 7 Warnings: 0

mysql> Select * from emp;


+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4400 | 0 | 30 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 5775 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 4950 | NULL | 20 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3300 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5500 | NULL | 20 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6600 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 4950 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

mysql> Update Emp Set Sal=Sal*1.05 where Hiredate between "1981-01-01" and "1981-12-
31";
Query OK, 7 rows affected (0.01 sec)
Rows matched: 7 Changed: 7 Warnings: 0

mysql> Select * from emp;


+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4620 | 0 | 30 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 6064 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 5198 | NULL | 20 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3465 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5775 | NULL | 20 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6930 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 5198 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

mysql> Update Emp Set Sal=Sal*0.95 where Job="Salesman" and Comm=0;


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Page 9 of 24
mysql> Select * from emp;
+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7499 | Ashok Singhal | Salesman | 7968 | 1981-02-20 | 4389 | 0 | 30 |
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 6064 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 5198 | NULL | 20 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3465 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5775 | NULL | 20 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6930 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 5198 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

mysql> Delete from Emp where Job="Salesman" and Comm=0;


Query OK, 1 row affected (0.01 sec)

mysql> Select * from emp;


+------+---------------+-----------+------+------------+------+------+--------+
| ENO | Ename | Job | MGR | Hiredate | Sal | Comm | Deptno |
+------+---------------+-----------+------+------------+------+------+--------+
| 7521 | Rohit Rana | Salesman | 7968 | 1981-02-22 | 6064 | 500 | 30 |
| 7566 | Jyoti Lamba | Manager | 7839 | 1981-04-02 | 5198 | NULL | 20 |
| 7782 | Chetan Gupta | Manager | 7839 | 1981-06-09 | 3465 | NULL | 10 |
| 7788 | Sudhir Rawat | Analyst | 7566 | 1981-04-19 | 5775 | NULL | 20 |
| 7839 | Kavita Sharma | PRESIDENT | NULL | 1981-11-17 | 6930 | NULL | 10 |
| 7900 | Jagdeep Rana | Clerk | 7968 | 1981-12-03 | 5198 | NULL | 30 |
+------+---------------+-----------+------+------------+------+------+--------+
6 rows in set (0.00 sec)

Page 10 of 24
Assignment II:

Create following tables and write SQL commands for A) to D) and Output for E)to
G). [Note that the output should be written on the basis of the original table
without considering the changes done]:
Table : ITEM
INSTOC
ICODE INAME COMPANY SCODE UNITPRICE
K
P101 Printer Laser HP S05 150 14000
C102 Computer Desktop DELL S01 320 38000
L101 Laptop LENOVO S01 120 62000
S101 Scanner HP S05 170 7000
A101 Keyboard TVS S06 540 700
A102 Mouse DELL S03 230 250
A103 JoyStick FOURCE S06 130 2500

Table : SUPPLIER
SCODE SNAME LOCATION
S01 Chahat Sinha Gurgaon
S03 Deepraj Noida
S06 Seena Garg Delhi
S05 Pitamber Delhi

To display ICODE, INAME and COMPANY of all of the items from DELL and FOURCE
A)
companies from the table ITEM.
To display the complete information from the table ITEM for records whose
B)
UNITPRICE is costlier than 10000 but not from the company “HP”.
C) To display all the unique values of SCODE from table ITEM.
To increase the value of UNITPRICE by 5% and INSTOCK by 20 for all the items which
D)
are not from company "TVS".
SELECT MIN(UNITPRICE),SCODE FROM ITEM WHERE SCODE='S01' OR SCODE='S05'
E)
GROUP BY SCODE;
SELECT INAME, SNAME, S.SCODE
F) FROM ITEM I, SUPPLIER S
WHERE I.SCODE=S.SCODE AND INSTOCK<150;
G) SELECT COUNT(*) FROM SUPPLIER;

SOLUTION:

mysql> create table ITEM


-> (
-> ICODE Char(4) Primary Key,
-> INAME varchar(30) Unique Not Null,
-> Company Char(6) Not Null,
-> SCODE char(3),
-> INSTOCK int not null,
-> UNITPRICE int not null
Page 11 of 24
-> );
Query OK, 0 rows affected (0.03 sec)

mysql> create table Supplier


-> (
-> SCODE char(4) Primary Key,
-> SNAME varchar(30) not null,
-> Location char(9) not null
-> );
Query OK, 0 rows affected (0.07 sec)

mysql> Alter table ITEM ADD Foreign Key(SCODE) references Supplier(Scode);


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

mysql> Insert into Supplier values("S01","Chahat Sinha","Gurgaon");


Query OK, 1 row affected (0.01 sec)

mysql> Insert into Supplier values("S03","Deepraj","Noida");


Query OK, 1 row affected (0.01 sec)

mysql> Insert into Supplier values("S06","Seena Garg","Delhi");


Query OK, 1 row affected (0.00 sec)

mysql> Insert into Supplier values("S05","Pitamber","Delhi");


Query OK, 1 row affected (0.00 sec)

mysql> select * from supplier;


+-------+--------------+----------+
| SCODE | SNAME | Location |
+-------+--------------+----------+
| S01 | Chahat Sinha | Gurgaon |
| S03 | Deepraj | Noida |
| S05 | Pitamber | Delhi |
| S06 | Seena Garg | Delhi |
+-------+--------------+----------+
4 rows in set (0.00 sec)

mysql> Insert into ITEM


-> values('P101','Printer Laser','HP','S05',150,14000);
Query OK, 1 row affected (0.01 sec)

mysql> Insert into ITEM


-> values('C102','Computer Desktop','DELL','S01',320,38000);
Query OK, 1 row affected (0.01 sec)

mysql> Insert into ITEM


-> values('L101','Laptop','LENOVO','S01',120,62000);
Query OK, 1 row affected (0.01 sec)

mysql> Insert into ITEM


-> values('S101','Scanner','HP','S05',170,7000);
Query OK, 1 row affected (0.01 sec)

mysql> Insert into ITEM


-> values('A101','Keyboard','TVS','S06',540,700);
Query OK, 1 row affected (0.01 sec)

mysql> Insert into ITEM


-> values('A102','Mouse','DELL','S03',230,250);

Page 12 of 24
Query OK, 1 row affected (0.01 sec)

mysql> Insert into ITEM


-> values('A103','JoyStick','Fource','S06',130,2500);
Query OK, 1 row affected (0.01 sec)

mysql> select * from Item;


+-------+------------------+---------+-------+---------+-----------+
| ICODE | INAME | Company | SCODE | INSTOCK | UNITPRICE |
+-------+------------------+---------+-------+---------+-----------+
| A101 | Keyboard | TVS | S06 | 540 | 700 |
| A102 | Mouse | DELL | S03 | 230 | 250 |
| A103 | JoyStick | Fource | S06 | 130 | 2500 |
| C102 | Computer Desktop | DELL | S01 | 320 | 38000 |
| L101 | Laptop | LENOVO | S01 | 120 | 62000 |
| P101 | Printer Laser | HP | S05 | 150 | 14000 |
| S101 | Scanner | HP | S05 | 170 | 7000 |
+-------+------------------+---------+-------+---------+-----------+
7 rows in set (0.00 sec)

mysql> Describe Item;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| ICODE | char(4) | NO | PRI | NULL | |
| INAME | varchar(30) | NO | UNI | NULL | |
| Company | char(6) | NO | | NULL | |
| SCODE | char(3) | YES | MUL | NULL | |
| INSTOCK | int | NO | | NULL | |
| UNITPRICE | int | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

mysql> Select ICODE, INAME, Company From Item where Company in ('DELL','Foorce');
+-------+------------------+---------+
| ICODE | INAME | Company |
+-------+------------------+---------+
| A102 | Mouse | DELL |
| C102 | Computer Desktop | DELL |
+-------+------------------+---------+
2 rows in set (0.00 sec)

mysql> Select * From Item


-> where Unitprice>10000 and Company != "HP";
+-------+------------------+---------+-------+---------+-----------+
| ICODE | INAME | Company | SCODE | INSTOCK | UNITPRICE |
+-------+------------------+---------+-------+---------+-----------+
| C102 | Computer Desktop | DELL | S01 | 320 | 38000 |
| L101 | Laptop | LENOVO | S01 | 120 | 62000 |
+-------+------------------+---------+-------+---------+-----------+
2 rows in set (0.00 sec)
mysql> Select Distinct Scode from Item;
+-------+
| Scode |
+-------+
| S01 |
| S03 |
| S05 |
Page 13 of 24
| S06 |
+-------+
4 rows in set (0.00 sec)

mysql> Update Item


-> Set Unitprice=Unitprice*1.05, Instock=Instock+20
-> where Company!='TVS';
Query OK, 6 rows affected (0.01 sec)
Rows matched: 6 Changed: 6 Warnings: 0

mysql> Select * from item;


+-------+------------------+---------+-------+---------+-----------+
| ICODE | INAME | Company | SCODE | INSTOCK | UNITPRICE |
+-------+------------------+---------+-------+---------+-----------+
| A101 | Keyboard | TVS | S06 | 540 | 700 |
| A102 | Mouse | DELL | S03 | 250 | 263 |
| A103 | JoyStick | Fource | S06 | 150 | 2625 |
| C102 | Computer Desktop | DELL | S01 | 340 | 39900 |
| L101 | Laptop | LENOVO | S01 | 140 | 65100 |
| P101 | Printer Laser | HP | S05 | 170 | 14700 |
| S101 | Scanner | HP | S05 | 190 | 7350 |
+-------+------------------+---------+-------+---------+-----------+
7 rows in set (0.00 sec)

mysql> SELECT MIN(UNITPRICE),SCODE FROM ITEM WHERE SCODE='S01' OR SCODE='S05' GROUP BY


SCODE;
+----------------+-------+
| MIN(UNITPRICE) | SCODE |
+----------------+-------+
| 39900 | S01 |
| 7350 | S05 |
+----------------+-------+
2 rows in set (0.00 sec)

mysql> SELECT INAME, SNAME, S.SCODE


-> FROM ITEM I, SUPPLIER S
-> WHERE I.SCODE=S.SCODE AND INSTOCK<150;
+--------+--------------+-------+
| INAME | SNAME | SCODE |
+--------+--------------+-------+
| Laptop | Chahat Sinha | S01 |
+--------+--------------+-------+
1 row in set (0.00 sec)

mysql> SELECT COUNT(*) FROM SUPPLIER;


+----------+
| COUNT(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)

Page 14 of 24
Assignment III:
Create following tables and write SQL commands for A) to D) and Output for E) to G).
[Note that the output should be written on the basis of the original table without
considering the changes done]:
Table : TRAINER

TID TNAME CITY HIREDATE SALARY


101 Sunaina Mumbai 1998-10-15 90000
102 Anamika Delhi 1994-12-24 80000
103 Deepti Chandigarh 2001-12-21 82000
104 Meenakshi Delhi 2002-12-25 78000
105 Richa Mumbai 1996-01-12 95000
106 Maniprabha Chennai 2001-12-12 69000

Table : COURSE

CID CNAME FEES STARTDATE TID


C201 AGDCA 12000 2018-07-02 101
C202 ADCA 15000 2018-07-15 103
C203 DCA 10000 2018-10-01 102
C204 DDTP 9000 2018-09-15 104
C205 DHN 20000 2018-08-01 101
C206 O LEVEL 18000 2018-07-25 105

A) Display the Trainer Name, City & Salary in descending order of their Hiredate.
B) To display the TNAME and CITY of Trainer who joined the Institute in the month of
December 2001.
C) To display TNAME, HIREDATE, CNAME, STARTDATE from tables TRAINER and
COURSE of all those courses whose FEES is less than or equal to 10000.
D) To display the number of Trainers from each city.
E) SELECT TID, TNAME FROM TRAINER WHERE CITY NOT IN(‘DELHI’, ‘MUMBAI’);
F) SELECT DISTINCT TID FROM COURSE;
G) SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY TID HAVING
COUNT(*)>1;

Solution:

mysql> create table Trainer


-> (

Page 15 of 24
-> TID int Primary Key,
-> TNAME varchar(20) not null,
-> City varchar(15) not null,
-> Hiredate Date,
-> Salary int
-> );
Query OK, 0 rows affected (0.12 sec)

mysql> create table Course


-> (
-> CID char(4) Primary Key,
-> CNAME char(8) Unique Not Null,
-> FEES int not null,
-> Startdate Date not null,
-> TID int,
-> Foreign Key(TID) references Trainer(TID)
-> );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into Trainer


-> values(101,"Sunaina","Mumbai","1998-10-15",90000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Trainer


-> values(102,"Anamika","Delhi","1994-12-24",80000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Trainer


-> values(103,"Deepti","Chandigarh","2001-12-21",82000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Course


-> values("C201","AGDCA",12000,"2018-07-02",101);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Course


-> values("C202","ADCA",15000,"2018-07-15",103);
Query OK, 1 row affected (0.01 sec)

mysql> insert into course


-> values("C203",'DCA',10000,'2018-10-01',102);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Trainer


-> values(104,'Meenakshi','Delhi','2002-12-25',78000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Trainer


-> values(105,'Richa','Mumbai','1996-01-12',95000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Trainer


-> values(106,'Maniprabha','Chennai','2001-12-12',69000);
Query OK, 1 row affected (0.01 sec)

mysql> select * from Trainer;


+-----+------------+------------+------------+--------+
| TID | TNAME | City | Hiredate | Salary |
+-----+------------+------------+------------+--------+
| 101 | Sunaina | Mumbai | 1998-10-15 | 90000 |

Page 16 of 24
| 102 | Anamika | Delhi | 1994-12-24 | 80000 |
| 103 | Deepti | Chandigarh | 2001-12-21 | 82000 |
| 104 | Meenakshi | Delhi | 2002-12-25 | 78000 |
| 105 | Richa | Mumbai | 1996-01-12 | 95000 |
| 106 | Maniprabha | Chennai | 2001-12-12 | 69000 |
+-----+------------+------------+------------+--------+
6 rows in set (0.00 sec)

mysql> insert into Course


-> values('C204','DDTP',9000,'2018-09-15',104);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Course


-> values('C205','DHN',20000,'2018-08-01',101);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Course


-> values('C206','O LEVEL',18000,'2018-07-25',105);
Query OK, 1 row affected (0.01 sec)

mysql> select * from Course;


+------+---------+-------+------------+------+
| CID | CNAME | FEES | Startdate | TID |
+------+---------+-------+------------+------+
| C201 | AGDCA | 12000 | 2018-07-02 | 101 |
| C202 | ADCA | 15000 | 2018-07-15 | 103 |
| C203 | DCA | 10000 | 2018-10-01 | 102 |
| C204 | DDTP | 9000 | 2018-09-15 | 104 |
| C205 | DHN | 20000 | 2018-08-01 | 101 |
| C206 | O LEVEL | 18000 | 2018-07-25 | 105 |
+------+---------+-------+------------+------+
6 rows in set (0.00 sec)

mysql> Select TNAME, CITY, SALARY From Trainer


-> order by Hiredate Desc;
+------------+------------+--------+
| TNAME | CITY | SALARY |
+------------+------------+--------+
| Meenakshi | Delhi | 78000 |
| Deepti | Chandigarh | 82000 |
| Maniprabha | Chennai | 69000 |
| Sunaina | Mumbai | 90000 |
| Richa | Mumbai | 95000 |
| Anamika | Delhi | 80000 |
+------------+------------+--------+
6 rows in set (0.00 sec)

mysql> Select Tname, City From Trainer


-> where Hiredate like '2001-12%';
+------------+------------+
| Tname | City |
+------------+------------+
| Deepti | Chandigarh |
| Maniprabha | Chennai |
+------------+------------+
2 rows in set (0.00 sec)

mysql> Select T.Tname, T.Hiredate, C.Cname, C.Startdate


-> From Trainer T natural join Course C
-> where C.Fees<=10000;

Page 17 of 24
+-----------+------------+-------+------------+
| Tname | Hiredate | Cname | Startdate |
+-----------+------------+-------+------------+
| Anamika | 1994-12-24 | DCA | 2018-10-01 |
| Meenakshi | 2002-12-25 | DDTP | 2018-09-15 |
+-----------+------------+-------+------------+
2 rows in set (0.00 sec)

mysql> Select T.Tname, T.Hiredate, C.Cname, C.Startdate


-> From Trainer T, Course C
-> where T.TID=C.TID and C.Fees<=10000;
+-----------+------------+-------+------------+
| Tname | Hiredate | Cname | Startdate |
+-----------+------------+-------+------------+
| Anamika | 1994-12-24 | DCA | 2018-10-01 |
| Meenakshi | 2002-12-25 | DDTP | 2018-09-15 |
+-----------+------------+-------+------------+
2 rows in set (0.00 sec)

mysql> Select City, Count(*) From Trainer


-> Group by City;
+------------+----------+
| City | Count(*) |
+------------+----------+
| Mumbai | 2 |
| Delhi | 2 |
| Chandigarh | 1 |
| Chennai | 1 |
+------------+----------+
4 rows in set (0.00 sec)

mysql> SELECT TID, TNAME FROM TRAINER WHERE CITY NOT IN('DELHI', 'MUMBAI');
+-----+------------+
| TID | TNAME |
+-----+------------+
| 103 | Deepti |
| 106 | Maniprabha |
+-----+------------+
2 rows in set (0.00 sec)

mysql> SELECT DISTINCT TID FROM COURSE;


+------+
| TID |
+------+
| 101 |
| 102 |
| 103 |
| 104 |
| 105 |
+------+
5 rows in set (0.00 sec)

mysql> SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY TID HAVING COUNT(*)>1;
+------+----------+-----------+
| TID | COUNT(*) | MIN(FEES) |
+------+----------+-----------+
| 101 | 2 | 12000 |
+------+----------+-----------+
1 row in set (0.00 sec)

Page 18 of 24
Assignment IV:

Write queries (a) to (d) based on the tables EMPLOYEE and DEPARTMENT given below:
Table : EMPLOYEE
EMPID NAME DOB DEPTID DESIG SALARY
120 Alisha 1978-01-23 D001 Manager 75000
123 Nitin 1977-10-10 D002 AO 59000
129 Navjot 1971-07-12 D003 Supervisor 40000
130 Jimmy 1980-12-30 D004 Sales Rep 60000
131 Faiz 1984-04-06 D001 Dep Manager 65000

Table: DEPARTMENT
DEPTID DEPTNAME FLOORNO
D001 Personal 4
D002 Admin 10
D003 Production 1
D004 Sales 3
A) To display the average salary of all employees, department wise.
B) To display name and respective department name of each employee whose salary is
more than 50000.
C) To display the names of employees whose salary is not known, in alphabetical order.
D) To display DEPTID from the table EMPLOYEE without repetition.

Solution:

mysql> create table Employee


-> (
-> EMPID int Primary Key,
-> Name varchar(15) Not Null,
-> DOB Date,
-> DEPTID char(4),
-> Desig varchar(13) not null,
-> Salary int
-> );
Query OK, 0 rows affected (0.07 sec)

mysql> create table Department


-> (
-> DeptID char(4) Primary Key,
-> DeptName char(11) not null,
-> FloorNo int
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> Alter table Employee ADD


-> Foreign Key(DEPTID) references Department(DEPTID);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

Page 19 of 24
mysql> insert into Department
-> values('D001','Personal',4);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Department


-> values('D002','Admin',10);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Department


-> values('D003','Production',1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Department


-> values('D004','Sales',3);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Employee


-> values(120, 'Alisha','1978-01-23','D001','Manager',75000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Employee


-> values(123, 'Nitin','1977-10-10','D002','AO',59000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Employee


-> values(129, 'Navjot','1971-07-12','D003','Supervisor',40000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Employee


-> values(130, 'Jimmy','1980-12-30','D004','Sales Rep',60000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Employee


-> values(131, 'Faiz','1984-04-06','D001','Dep Manager',65000);
Query OK, 1 row affected (0.01 sec)

mysql> select * from Department;


+--------+------------+---------+
| DeptID | DeptName | FloorNo |
+--------+------------+---------+
| D001 | Personal | 4 |
| D002 | Admin | 10 |
| D003 | Production | 1 |
| D004 | Sales | 3 |
+--------+------------+---------+
4 rows in set (0.00 sec)

mysql> select * from Employee;


+-------+--------+------------+--------+-------------+--------+
| EMPID | Name | DOB | DEPTID | Desig | Salary |
+-------+--------+------------+--------+-------------+--------+
| 120 | Alisha | 1978-01-23 | D001 | Manager | 75000 |
| 123 | Nitin | 1977-10-10 | D002 | AO | 59000 |
| 129 | Navjot | 1971-07-12 | D003 | Supervisor | 40000 |
| 130 | Jimmy | 1980-12-30 | D004 | Sales Rep | 60000 |
| 131 | Faiz | 1984-04-06 | D001 | Dep Manager | 65000 |
+-------+--------+------------+--------+-------------+--------+
5 rows in set (0.00 sec)

Page 20 of 24
mysql> Select DeptID, Avg(Salary)
-> From Employee
-> Group By DeptID;

+--------+-------------+
| DeptID | Avg(Salary) |
+--------+-------------+
| D001 | 70000.0000 |
| D002 | 59000.0000 |
| D003 | 40000.0000 |
| D004 | 60000.0000 |
+--------+-------------+
4 rows in set (0.00 sec)

mysql> Select E.Name, D.DeptName


-> From Employee E, Department D
-> where E.DeptID=D.DeptID and E.Salary>50000;
+--------+----------+
| Name | DeptName |
+--------+----------+
| Alisha | Personal |
| Nitin | Admin |
| Jimmy | Sales |
| Faiz | Personal |
+--------+----------+
4 rows in set (0.00 sec)

mysql> Select E.Name, D.DeptName


-> From Employee E Natural Join Department D
-> where E.Salary>50000;
+--------+----------+
| Name | DeptName |
+--------+----------+
| Alisha | Personal |
| Nitin | Admin |
| Jimmy | Sales |
| Faiz | Personal |
+--------+----------+
4 rows in set (0.00 sec)

mysql> Select Name From Employee


-> where Salary is null
-> order by Name;
Empty set (0.00 sec)

mysql> Select distinct DeptId From Department;


+--------+
| DeptId |
+--------+
| D001 |
| D002 |
| D003 |
| D004 |
+--------+
4 rows in set (0.00 sec)

Page 21 of 24
Assignment V:

Write queries (a) to (d) based on the tables given below:

Table : SUPPLIER
SNO SNAME CITY
1 ABC PVT NEW DELHI
2 INDIA ENTERPRISES JAIPUR
3 DEEP LTD UDAIPUR
4 G&G CORP JAIPUR

Table: ITEM
CODE INAME PRICE SNO
C1 COLD DRINK 150 2
B2 BISCUITS 100 3
T3 TEA 200 1

A) Display Item code and its name whose price is more than 100 in descending order of
price.
B) Show Item names with their respective supplier names.
C) Display the name and the supplier number who has supplied the item with the item
code 'B2'.
D) List the names and the prices of the items along with their supplier names, which
have been supplied by the supplier of Jaipur.

Solution:
mysql> Create Table Supplier
-> (
-> SNO int Primary Key,
-> SNAME varchar(20) unique not null,
-> City varchar(10)
-> );
Query OK, 0 rows affected (0.05 sec)

mysql> insert into Supplier


-> values(1,'ABC PVT','New Delhi');
Query OK, 1 row affected (0.02 sec)

mysql> insert into Supplier


-> values(2,'INDIA ENTERPRISES','JAIPUR');
Query OK, 1 row affected (0.01 sec)

mysql> insert into Supplier


-> values(3,'DEEP LTD','UDAIPUR');
Query OK, 1 row affected (0.01 sec)
Page 22 of 24
mysql> insert into Supplier
-> values(4,'G&G CORP','JAIPUR');
Query OK, 1 row affected (0.01 sec)

mysql> Create Table ITEM


-> (
-> Code char(2) Primary Key,
-> INAME varchar(10) not null,
-> Price int,
-> SNO int,
-> Foreign Key(SNO) references Supplier(SNO)
-> );
Query OK, 0 rows affected (0.07 sec)

mysql> insert into Item


-> values('C1','Cold Drink',150,2);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Item


-> values('B2','Biscuits',100,3);
Query OK, 1 row affected (0.01 sec)

mysql> insert into Item


-> values('T3','TEA',200,1);
Query OK, 1 row affected (0.01 sec)

mysql> select * From Supplier;


+-----+-------------------+-----------+
| SNO | SNAME | City |
+-----+-------------------+-----------+
| 1 | ABC PVT | New Delhi |
| 2 | INDIA ENTERPRISES | JAIPUR |
| 3 | DEEP LTD | UDAIPUR |
| 4 | G&G CORP | JAIPUR |
+-----+-------------------+-----------+
4 rows in set (0.00 sec)

mysql> select * From Item;


+------+------------+-------+------+
| Code | INAME | Price | SNO |
+------+------------+-------+------+
| B2 | Biscuits | 100 | 3 |
| C1 | Cold Drink | 150 | 2 |
| T3 | TEA | 200 | 1 |
+------+------------+-------+------+
3 rows in set (0.00 sec)

mysql> Select Code, IName From Item


-> where Price>100
-> Order By Price Desc;
+------+------------+
| Code | IName |
+------+------------+
| T3 | TEA |
| C1 | Cold Drink |

Page 23 of 24
+------+------------+
2 rows in set (0.00 sec)

mysql> Select I.INAME, S.SNAME


-> From Supplier S Natural Join Item I;
+------------+-------------------+
| INAME | SNAME |
+------------+-------------------+
| Biscuits | DEEP LTD |
| Cold Drink | INDIA ENTERPRISES |
| TEA | ABC PVT |
+------------+-------------------+
3 rows in set (0.00 sec)

mysql> Select I.INAME, S.SNAME


-> From Supplier S,Item I
-> where S.SNO=I.SNO;
+------------+-------------------+
| INAME | SNAME |
+------------+-------------------+
| Biscuits | DEEP LTD |
| Cold Drink | INDIA ENTERPRISES |
| TEA | ABC PVT |
+------------+-------------------+
3 rows in set (0.00 sec)

mysql> Select I.INAME, I.PRICE, S.SNAME


-> From Item I, Supplier S
-> where I.SNO=S.SNO and S.City='Jaipur';
+------------+-------+-------------------+
| INAME | PRICE | SNAME |
+------------+-------+-------------------+
| Cold Drink | 150 | INDIA ENTERPRISES |
+------------+-------+-------------------+
1 row in set (0.00 sec)

mysql> Select I.INAME, I.PRICE, S.SNAME


-> From Item I Natural Join Supplier S
-> where S.City='Jaipur';
+------------+-------+-------------------+
| INAME | PRICE | SNAME |
+------------+-------+-------------------+
| Cold Drink | 150 | INDIA ENTERPRISES |
+------------+-------+-------------------+
1 row in set (0.00 sec)

Page 24 of 24

You might also like