You are on page 1of 7

create database EMPLOYEE;

Query OK, 1 row affected (0.002 sec)

use EMPLOYEE;
Database changed

show tables;
Empty set (0.001 sec)

create table Employee (Employee_Name char(20) primary key,Street char(20),City


char(20));
Query OK, 0 rows affected (0.019 sec)

desc Employee;
+---------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| Employee_Name | char(20) | NO | PRI | NULL | |
| Street | char(20) | YES | | NULL | |
| City | char(20) | YES | | NULL | |
+---------------+----------+------+-----+---------+-------+
3 rows in set (0.005 sec)

create table Works (Employee_Name char(20) primary key,Company_Name


char(25),Salary INT);
Query OK, 0 rows affected (0.015 sec)

DESC Works;
+---------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| Employee_Name | char(20) | NO | PRI | NULL | |
| Company_Name | char(25) | YES | | NULL | |
| Salary | int(11) | YES | | NULL | |
+---------------+----------+------+-----+---------+-------+
3 rows in set (0.005 sec)

create table Company (Company_Name char(25) primary key,City char(20));


Query OK, 0 rows affected (0.014 sec)

desc Company;
+--------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+-------+
| Company_Name | char(25) | NO | PRI | NULL | |
| City | char(20) | YES | | NULL | |
+--------------+----------+------+-----+---------+-------+
2 rows in set (0.005 sec)

create table Manages (Employee_Name char(20) primary key,Manager_Name char(20));


Query OK, 0 rows affected (0.017 sec)

desc Manages;
+---------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| Employee_Name | char(20) | NO | PRI | NULL | |
| Manager_Name | char(20) | YES | | NULL | |
+---------------+----------+------+-----+---------+-------+
2 rows in set (0.004 sec)

insert into Employee values ("John_Smith","5/113","New_Delhi");


Query OK, 1 row affected (0.005 sec)

insert into Employee values ("James_Borg","8/24","New_Delhi");


Query OK, 1 row affected (0.006 sec)

insert into Employee values ("Jones","3/10","Gurugram");


Query OK, 1 row affected (0.007 sec)

insert into Employee values ("Franklin_Wong","11/120","Noida");


Query OK, 1 row affected (0.005 sec)

select * from Employee;


+---------------+--------+-----------+
| Employee_Name | Street | City |
+---------------+--------+-----------+
| Franklin_Wong | 11/120 | Noida |
| James_Borg | 8/24 | New_Delhi |
| John_Smith | 5/113 | New_Delhi |
| Jones | 3/10 | Gurugram |
+---------------+--------+-----------+
4 rows in set (0.001 sec)

insert into Works values ("Franklin_Wong","First_Bank_Corporation",15000);


Query OK, 1 row affected (0.005 sec)

insert into Works values ("James_Borg","First_Bank_Corporation",25000);


Query OK, 1 row affected (0.005 sec)

insert into Works values ("John_Smith","Small_Bank_Corporation",45000);


Query OK, 1 row affected (0.005 sec)

insert into Works values ("Jones","Insurance_Corporation",30000);


Query OK, 1 row affected (0.007 sec)

select * from Works;


+---------------+------------------------+--------+
| Employee_Name | Company_Name | Salary |
+---------------+------------------------+--------+
| Franklin_Wong | First_Bank_Corporation | 15000 |
| James_Borg | First_Bank_Corporation | 25000 |
| John_Smith | Small_Bank_Corporation | 45000 |
| Jones | Insurance_Corporation | 30000 |
+---------------+------------------------+--------+
4 rows in set (0.001 sec)

insert into Company values ("First_Bank_Corporation","New_Delhi");


Query OK, 1 row affected (0.005 sec)

insert into Company values ("Small_Bank_Corporation","Gurugram");


Query OK, 1 row affected (0.005 sec)

insert into Company values ("Insurance_Corporation","Noida");


Query OK, 1 row affected (0.002 sec)

select * from Company;


+------------------------+-----------+
| Company_Name | City |
+------------------------+-----------+
| First_Bank_Corporation | New_Delhi |
| Insurance_Corporation | Noida |
| Small_Bank_Corporation | Gurugram |
+------------------------+-----------+
3 rows in set (0.001 sec)

insert into Manages values ("Franklin_Wong","Alicia_Zeleya");


Query OK, 1 row affected (0.006 sec)

insert into Manages values ("James_Borg","Jennifer_Wallace");


Query OK, 1 row affected (0.006 sec)

insert into Manages values ("John_Smith","Ramesh_Narayan");


Query OK, 1 row affected (0.005 sec)

insert into Manages values ("Jones","Joyce_English");


Query OK, 1 row affected (0.005 sec)

select * from Manages;


+---------------+------------------+
| Employee_Name | Manager_Name |
+---------------+------------------+
| Franklin_Wong | Alicia_Zeleya |
| James_Borg | Jennifer_Wallace |
| John_Smith | Ramesh_Narayan |
| Jones | Joyce_English |
+---------------+------------------+
4 rows in set (0.001 sec)

(a.)
select Employee_Name from Works where Company_Name="First_Bank_Corporation";
+---------------+
| Employee_Name |
+---------------+
| Franklin_Wong |
| James_Borg |
+---------------+
2 rows in set (0.011 sec)

(b.)
select Employee.Employee_Name,City from Employee,Works where
Works.Company_Name="First_Bank_Corporation" AND
Employee.Employee_Name=Works.Employee_Name;
+---------------+-----------+
| Employee_Name | City |
+---------------+-----------+
| Franklin_Wong | Noida |
| James_Borg | New_Delhi |
+---------------+-----------+
2 rows in set (0.014 sec)

(c.)
select * from Employee where Employee_Name IN(select Employee_Name from Works
where Company_Name="First_Bank_Corporation" AND Salary>10000);
+---------------+--------+-----------+
| Employee_Name | Street | City |
+---------------+--------+-----------+
| Franklin_Wong | 11/120 | Noida |
| James_Borg | 8/24 | New_Delhi |
+---------------+--------+-----------+
2 rows in set (0.020 sec)

(d.)
select Employee.Employee_Name from Employee,Works,Company where
Employee.Employee_Name=Works.Employee_Name AND Employee.City=Company.City AND
Works.Company_Name=Company.Company_Name;
+---------------+
| Employee_Name |
+---------------+
| James_Borg |
+---------------+
1 row in set (0.002 sec)

(f.)
select Employee_Name from Works where Company_Name<>"First_Bank_Corporation";
+---------------+
| Employee_Name |
+---------------+
| John_Smith |
| Jones |
+---------------+
2 rows in set (0.002 sec)

(g.)
select Employee_Name from Works where Salary>ALL (select Salary from Works where
Company_Name="Small_Bank_Corporation");
Empty set (0.002 sec)

(i.)
select Employee_Name from Works where Salary> (select AVG(Salary) from Works
where Works.Company_Name=Works.Company_Name);
+---------------+
| Employee_Name |
+---------------+
| John_Smith |
| Jones |
+---------------+
2 rows in set (0.016 sec)

(j.)
select Company_Name from Works GROUP BY Company_Name HAVING count(DISTINCT
Employee_Name)>= ALL (select COUNT(DISTINCT Employee_Name) from Works GROUP BY
Company_Name);
+------------------------+
| Company_Name |
+------------------------+
| First_Bank_Corporation |
+------------------------+
1 row in set (0.019 sec)

(k.)
select Company_Name from Works GROUP BY Company_Name HAVING SUM(Salary) <=ALL
(select SUM(Salary) from Works GROUP BY Company_Name);
+-----------------------+
| Company_Name |
+-----------------------+
| Insurance_Corporation |
+-----------------------+
1 row in set (0.063 sec)

(l.)
select Company_Name from Works GROUP BY Company_Name HAVING AVG(Salary) > (select
AVG(Salary) from Works where Company_Name="First_Bank_Corporation");
+------------------------+
| Company_Name |
+------------------------+
| Insurance_Corporation |
| Small_Bank_Corporation |
+------------------------+
2 rows in set (0.009 sec)

(m.)
UPDATE Employee SET City="Newton" where Employee_Name="Jones";
Query OK, 1 row affected (0.022 sec)
Rows matched: 1 Changed: 1 Warnings: 0

select * from Employee;


+---------------+--------+-----------+
| Employee_Name | Street | City |
+---------------+--------+-----------+
| Franklin_Wong | 11/120 | Noida |
| James_Borg | 8/24 | New_Delhi |
| John_Smith | 5/113 | New_Delhi |
| Jones | 3/10 | Newton |
+---------------+--------+-----------+
4 rows in set (0.001 sec)

(n.)
UPDATE Works SET Salary=Salary*1.1 where Company_Name="First_Bank_Corporation";
Query OK, 2 rows affected (0.010 sec)
Rows matched: 2 Changed: 2 Warnings: 0

select * from Works;


+---------------+------------------------+--------+
| Employee_Name | Company_Name | Salary |
+---------------+------------------------+--------+
| Franklin_Wong | First_Bank_Corporation | 16500 |
| James_Borg | First_Bank_Corporation | 27500 |
| John_Smith | Small_Bank_Corporation | 45000 |
| Jones | Insurance_Corporation | 30000 |
+---------------+------------------------+--------+
4 rows in set (0.001 sec)
(o.)
UPDATE Works SET Salary=Salary*1.1 where Employee_Name IN(select Manager_Name
from Manages) AND Company_Name="First_Bank_Corporation";
Query OK, 0 rows affected (0.057 sec)
Rows matched: 0 Changed: 0 Warnings: 0

select * from Works;


+---------------+------------------------+--------+
| Employee_Name | Company_Name | Salary |
+---------------+------------------------+--------+
| Franklin_Wong | First_Bank_Corporation | 16500 |
| James_Borg | First_Bank_Corporation | 27500 |
| John_Smith | Small_Bank_Corporation | 45000 |
| Jones | Insurance_Corporation | 30000 |
+---------------+------------------------+--------+
4 rows in set (0.001 sec)

(p.)
UPDATE Works SET Works.Salary=Works.Salary*1.03 where Works.Employee_Name
IN(select Manager_Name from Manages) AND Works.Query OK, 0 rows affected (0.001
sec)ame="First_Bank_Corporation";
Rows matched: 0 Changed: 0 Warnings: 0

UPDATE Works SET Works.Salary=Works.Salary*1.1 where Works.Employee_Name


IN(select Manager_Name from Manages) AND Works.SQuery OK, 0 rows affected (0.002
sec)ame="First_Bank_Corporation";
Rows matched: 0 Changed: 0 Warnings: 0

select * from Works;


+---------------+------------------------+--------+
| Employee_Name | Company_Name | Salary |
+---------------+------------------------+--------+
| Franklin_Wong | First_Bank_Corporation | 16500 |
| James_Borg | First_Bank_Corporation | 27500 |
| John_Smith | Small_Bank_Corporation | 45000 |
| Jones | Insurance_Corporation | 30000 |
+---------------+------------------------+--------+
4 rows in set (0.002 sec)

(q.)
select * from Works;
+---------------+------------------------+--------+
| Employee_Name | Company_Name | Salary |
+---------------+------------------------+--------+
| Franklin_Wong | First_Bank_Corporation | 16500 |
| James_Borg | First_Bank_Corporation | 27500 |
| John_Smith | Small_Bank_Corporation | 45000 |
| Jones | Insurance_Corporation | 30000 |
+---------------+------------------------+--------+
4 rows in set (0.018 sec)

DELETE from Works where Company_Name="Small_Bank_Corporation";


Query OK, 1 row affected (0.012 sec)

select * from Works;


+---------------+------------------------+--------+
| Employee_Name | Company_Name | Salary |
+---------------+------------------------+--------+
| Franklin_Wong | First_Bank_Corporation | 16500 |
| James_Borg | First_Bank_Corporation | 27500 |
| Jones | Insurance_Corporation | 30000 |
+---------------+------------------------+--------+
3 rows in set (0.001 sec)

You might also like