Assignment 4
Assignment 4
Assignment 4
mysql> select cid,sid from orders where odate between '2016-08-25' and '2016-10-23';
+------+------+
| cid | sid |
+------+------+
| 1| 1|
| 5| 2|
| 4| 4|
+------+------+
3 rows in set (0.00 sec)
s
mysql> select odate, sum(qty) from orders group by odate having max(qty)>5 order by qty desc;
+------------+----------+
| odate | sum(qty) |
+------------+----------+
| 2016-08-25 | 500 |
| 2017-01-01 | 50 |
| 2016-10-23 | 10 |
+------------+----------+
3 rows in set (0.00 sec)
1. Display the customer name and salesman name having highest orders amount in orders
table.
mysql> select cname, sname from customers, salesman, orders where customers.cid = orders.cid
and salesman.sid = orders.sid and qamnt=(select max(qamnt) from orders);
+---------+-------+
| cname | sname |
+---------+-------+
| Apoorva | Akash |
+---------+-------+
1 row in set (0.00 sec)
3. Display the odate salesman and customer city and salesman id in descending order
mysql> select cname from salesman, customers, orders where salesman.sid=orders.sid and
customers.cid=orders.cid and salesman.city= 'Delhi';
+---------+
| cname |
+---------+
| Naman |
| Apoorva |
+---------+
2 rows in set (0.00 sec)
mysql> select sname from salesman, customers, orders where salesman.sid=orders.sid and
customers.cid=orders.cid and customers.city!= 'GNoida';
+----------+
| sname |
+----------+
| Aayush |
| Abhijeet |
| Anand |
+----------+
3 rows in set (0.00 sec)
6. Show the salaries of all employees with 10% increment with present salary
7. Retrieve the no. of customers whose salesman having salary between 9000 to 16000
8. Retrieve the distinct no. Of city of customer whose salesman having salary between
20000 to 30000
10. Retrieve the name of customers who doesnt have any salesman
Assignment 7
mysql> select sname from salesman where salesman.sid not in ( select salesman.sid from
salesman, customers, orders where salesman.sid=orders.sid and customers.cid= orders.cid);
Empty set (0.01 sec)
2. Retrieve the name of each salesmen who has a customer with the same name and both
live in same city
mysql> select sname from salesman, customers, orders where salesman.sid=orders.sid and
customers.cid=orders.cid and salesman.sname=customers.cname and salesman.city=
customers.city;
Empty set (0.00 sec)
4. For each product retrieve the product name, the no of salesmen who sold that product and
their avg salary
5. For each product retrieve the product name and the no of salesmen from Delhi city who
sold that product
Assignment 8
mysql> create view ordview as select * from orders where qamnt in(select qamnt from orders
group by qamnt having count(*)>1);
Query OK, 0 rows affected (0.07 sec)
Assignment 9
mysql> create view ordview as select * from orders where qamnt in(select qamnt from orders
group by qamnt having count(*)>1);
Query OK, 0 rows affected (0.07 sec)
mysql> create view cityview as select city, count(city) from customers group by city having
count(city) in (select count(city) from salesman group by city) and city in (select city from
salesman group by city);
Query OK, 0 rows affected (0.00 sec)
mysql> create view view1 as select * from customers where class in (select class from customers
group by class having count(class)>2);
Query OK, 0 rows affected (0.00 sec)
mysql> select * from view1;
+-----+----------+----------+-------------+---------+---------+-------+---------+
| cid | cname | city | state | pincode | product | class | country |
+-----+----------+----------+-------------+---------+---------+-------+---------+
| 1 | Naman | Gnoida | UP | 201012 | GPU |B | India |
| 2 | Apoorva | GNoida | UP | 201308 | CPU |A | India |
| 3 | Abhishek | Kolkata | West Bengal | 730000 | RAM |A | India |
| 5 | Dewanshi | Agartala | Tripura | 102398 | Monitor | A | India |
| 6 | Ankit | Mumbai | Maharashtra | 998845 | Mouse | B | India |
| 7 | Shubham | Panjim | Goa | 997845 | Mouse | B | India |
+-----+----------+----------+-------------+---------+---------+-------+---------+
6 rows in set (0.00 sec)
6. Create a view to find the name of customers whose salesman lived in same city.
1. Create procedure to increase 10% salary of salesman who are having more than one
customers.
mysql> create procedure p2() select sname, 1.1*salary as new_salary from salesman, customers,
orders where salesman.sid=orders.sid and customers.cid=orders.cid group by orders.sid having
count(orders.sid)>1;
Query OK, 0 rows affected (0.00 sec)
2. Create procedures to find the name of salesman who has ordered maximum order
q_amnt.
3. Create procedure to find CID of customers of class B having more than one salesmen.
mysql> create procedure p7() select customers.cid from salesman, customers, orders where
salesman.sid=orders.sid and customers.cid=orders.cid and customers.class='B' group by
orders.cid having count(orders.cid)>1;
Query OK, 0 rows affected (0.00 sec)
4. Create procedure to find the number of customers who ordered same number of orders in
order table.
mysql> create procedure p5() select count(orders.oid) from orders, customers, salesman where
orders.cid=customers.cid and orders.sid=salesman.sid and orders.odate between "2016-09-12"
and "2017-02-01";
Query OK, 0 rows affected (0.00 sec)
mysql> create trigger t1 before insert on table1 for each row set @Amount = @Amount +
New.Amount;
Query OK, 0 rows affected (0.19 sec)
mysql>
mysql> create trigger t1 before insert on table1 for each row set New.Amount =
10*New.Amount;
Query OK, 0 rows affected (0.01 sec)
mysql> create trigger t1 before insert on table1 for each row set New.name = reverse(new.name);
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple triggers with the
same action time and event for one table'
mysql> drop trigger t1;
Query OK, 0 rows affected (0.00 sec)
mysql> create trigger t1 before insert on table1 for each row set New.name = reverse(new.name);
ERROR 1054 (42S22): Unknown column 'name' in 'NEW'
mysql> create trigger t1 before insert on table2 for each row set new.name = reverse(new.name);
Query OK, 0 rows affected (0.01 sec)
mysql> create trigger t1 before update on t3 for each row set new.salary = new.salary*10;
Query OK, 0 rows affected (0.01 sec)
Triggers on Salesman
mysql> create trigger t1 before insert on salesman for each row set
@tgttoget=@tgttoget+new.tgttoget;
Query OK, 0 rows affected (0.01 sec)
mysql> create trigger t1 before update on salesman for each row set new.salary=new.salary*10;
Query OK, 0 rows affected (0.01 sec)