Index PartB
Index PartB
Index PartB
[MySQL]
Page 1 of 24
INDEX
Page 2 of 24
Assignment I
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.
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.
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)
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)
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 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)
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.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
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)
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:
Page 12 of 24
Query OK, 1 row affected (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)
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
Table : COURSE
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:
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)
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)
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 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 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:
Page 19 of 24
mysql> insert into Department
-> values('D001','Personal',4);
Query OK, 1 row affected (0.01 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)
Page 21 of 24
Assignment V:
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)
Page 23 of 24
+------+------------+
2 rows in set (0.00 sec)
Page 24 of 24