0% found this document useful (0 votes)
37 views9 pages

DBMS 9

1. The document contains examples of using views, subqueries, procedures and group by/order by clauses in SQL. 2. It creates a view to show salesman details, demonstrates different types of subqueries, and creates procedures to call tables and perform functions. 3. Examples group and order data from tables in various ways such as by city, salesman, and product.

Uploaded by

abhinavbisht7o7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

DBMS 9

1. The document contains examples of using views, subqueries, procedures and group by/order by clauses in SQL. 2. It creates a view to show salesman details, demonstrates different types of subqueries, and creates procedures to call tables and perform functions. 3. Examples group and order data from tables in various ways such as by city, salesman, and product.

Uploaded by

abhinavbisht7o7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ASSIGNMENT -9

INTRODUCTION TO VIEW
Q.1 Create a view which shows the detail of salesman with his salary.
mysql> create view viewsal as
-> select sname , salary from salesman;
Query OK, 0 rows affected (0.31 sec)

mysql> select * from viewsal;


+--------------+--------+
| sname | salary |
+--------------+--------+
| AJAY PATEL | 10000 |
| CHINTAN SHAN | 20000 |
| VINAY MEHRA | 6000 |
| JAY PANDEY | 5000 |
| JIMIT DAVE | 45000 |
| MANAN GANDHI | 5000 |
+--------------+--------+

Q.2 Drop a view.

mysql> drop view viewsal;


Query OK, 0 rows affected (0.06 sec)
ASSIGNMENT -7
DIFFERENT TYPES OF SUB-QUERIES
Q.1 Display customer name where customer no. is highest.
mysql> select cname
-> from customers
-> where cid= (select max(cid) from customers);
+-------+
| cname |
+-------+
| BIMAL |
+-------+
1 row in set (0.06 sec)

Q.2 Display customer name whose salesman staying in Ahmedabad and


surat.
mysql> select c.cname
-> from customers c, orders1 o , salesman s
-> where c.cid=o.cid and s.sid=o.sid
-> and s.city in('Ahmedabad', 'surat');
+-------------+
| cname |
+-------------+
| SAPAN SHAH |
| SMIRITI |
| HARSHAL |
| SAPAN SHAH |
+-------------+
4 rows in set (0.00 sec)

3. Display order information for which order taken earliest and last.
mysql> select * from orders1 where odate=(select max(odate) from orders1);
+------+------+-------+-----------+------+------------+-------+
| OID | CID | SID | PRODUCT | QTY | ODATE | Q_AMT |
+------+------+-------+-----------+------+------------+-------+
| O511 | C303 | S104 | PRODUCT C | 50 | 2009-12-31 | 15000 |
+------+------+-------+-----------+------+------------+-------+

mysql> select * from orders1 where odate=(select min(odate) from orders1);


+------+------+-------+-----------+------+------------+--------+
| OID | CID | SID | PRODUCT | QTY | ODATE | Q_AMT |
+------+------+-------+-----------+------+------------+--------+
| O501 | C302 | S102 | PRODUCT A | 10 | 2009-01-02 | 700000 |
+------+------+-------+-----------+------+------------+--------+
1 row in set (0.00 sec)
4. Display the highest value for the amount of orders.
mysql> select * from orders1 where q_amt=(select max(q_amt) from orders1);
+------+------+-------+-----------+------+------------+--------+
| OID | CID | SID | PRODUCT | QTY | ODATE | Q_AMT |
+------+------+-------+-----------+------+------------+--------+
| O506 | C303 | S101 | PRODUCT B | 12 | 2009-04-15 | 900000 |
| O508 | C306 | S101 | PRODUCT B | 35 | 2009-09-27 | 900000 |
+------+------+-------+-----------+------+------------+--------+
2 rows in set (0.00 sec)

5. Display second highest order amount.


mysql> select q_amt from orders1
-> order by q_amt desc limit 1,1;
+--------+
| q_amt |
+--------+
| 900000 |
+--------+
1 row in set (0.02 sec)

6. Display third highest order amount.


mysql> select q_amt from orders
-> order by q_amt desc limit 2,1;
+--------+
| q_amt |
+--------+
| 700000 |
+--------+
1 row in set (0.00 sec)

7. Write a query which uses the UNION.


mysql> (select * from customers)
-> union
-> (select * from orders1);
+------+--------------+------------+-------------+---------+------------+-------
------+
| CID | CNAME | CITY | STATE | PINCODE | PRODUCT | CLASS
|
+------+--------------+------------+-------------+---------+------------+-------
------+
| C301 | NIRAV PATEL | NADIAD | UP | 45657 | PRODUCT C | B
|
| C302 | KIRAN DEV | DELHI | NULL | 456657 | PRODUCT C | A
|
| C303 | SAPAN SHAH | BANGLORE | KARNATAKA | 456537 | PRODUCT A | B
|
| C304 | SAURABH | SURAT | GUJARAT | 45637 | PRODUCT C | C
|
| C305 | SMIRITI | AHMEDABAD | GUJARAT | 45537 | PRODUCT C | B
|
| C306 | HARSHAL | MUMBAI | MAHARASHTRA | 47537 | PRODUCT C | A
|
| C307 | SUNIL | BARODA | GUJARAT | 475322 | PRODUCT B | B
|
| C308 | BIMAL | SURAT | GUJARAT | 422322 | PRODUCT B | C
|
| O501 | C302 | S102 | PRODUCT A | 10 | 2009-01-02 | 700000
|
| O502 | C301 | S105 | PRODUCT C | 5 | 2009-01-21 | 10000
|
| O503 | C308 | S103 | PRODUCT B | 23 | 2009-01-10 | 250000
|
| O504 | C306 | S104 | PRODUCT A | 31 | 2009-01-14 | 400000
|
| O505 | C306 | S102 | PRODUCT A | 88 | 2009-01-29 | 100000
|
| O506 | C303 | S101 | PRODUCT B | 12 | 2009-01-15 | 900000
|
| O507 | C304 | S105 | PRODUCT C | 60 | 2009-06-24 | 7501
|
| O508 | C305 | S101 | PRODUCT C | 35 | 2009-09-27 | 900000
|
| O509 | C302 | S102 | PRODUCT B | 76 | 2009-12-21 | 205000
|
| O510 | C307 | S102 | PRODUCT C | 24 | 2009-12-30 | 27800
|
| O511 | C303 | S104 | PRODUCT C | 50 | 2009-12-31 | 15000
|
+------+--------------+------------+-------------+---------+------------+-------
------+
19 rows in set (0.00 sec)
ASSIGNMENT 4

5. To find the list of customers name, cid, city, sid they existing a highest
o_amt in existing table.
mysql> SELECT C.CNAME , C.CID ,C.CITY , S.SID
-> FROM CUSTOMERS C, SALESMAN S, ORDERS1 O
-> WHERE C.CID=O.CID
-> AND S.SID= O.SID
-> AND Q_AMT =(SELECT MAX(Q_AMT) FROM ORDERS1);
+-------------+------+------------+------+
| CNAME | CID | CITY | SID |
+-------------+------+------------+------+
| SAPAN SHAH | C303 | BANGLORE | S101 |
| SMIRITI | C305 | AHMEDABAD | S101 |
+-------------+------+------------+------+
2 rows in set (0.00 sec)

7. Display the name of salesman who took the class “A” , maximum O_AMT in
orders then placed in city SURAT.
mysql> SELECT C.CNAME , C.CID ,C.CITY , S.SID
-> FROM CUSTOMERS C, SALESMAN S, ORDERS1 O
-> WHERE C.CID=O.CID
-> AND S.SID= O.SID
-> AND C.CLASS='A'
-> AND S.CITY='SURAT'
-> AND O.Q_AMT= (SELECT MAX(Q_AMT) FROM ORDERS1);
Empty set (0.03 sec)
ASSIGNMENT-8
EXERCISE ON PROCEDURE

Q.1 Create a table with customers mane , city, o_amt, sid in relation.
mysql> create table cust
-> (CNAME VARCHAR(10),
-> CITY VARCHAR(20),
-> O_AMT INT(5),
-> SID VARCHAR(3));
Query OK, 0 rows affected (0.14 sec)

mysql> DESC CUST;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| CNAME | varchar(10) | YES | | NULL | |
| CITY | varchar(20) | YES | | NULL | |
| O_AMT | int(5) | YES | | NULL | |
| SID | varchar(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.05 sec)

mysql> INSERT INTO CUST


-> VALUES('POOJA' , 'DELHI' , 20000, 'S1');
Query OK, 1 row affected (0.08 sec)

mysql> INSERT INTO CUST


-> VALUES('NEHA' , 'KANPUR' , 30000, 'S2');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO CUST


-> VALUES('RESHU' , 'NOIDA' , 40000, 'S3');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM CUST;


+-------+--------+-------+------+
| CNAME | CITY | O_AMT | SID |
+-------+--------+-------+------+
| POOJA | DELHI | 20000 | S1 |
| NEHA | KANPUR | 30000 | S2 |
| RESHU | NOIDA | 40000 | S3 |
+-------+--------+-------+------+

Q2. To create a procedure and call the earliest table in procedure condition.
mysql> DELIMITER /
mysql> CREATE PROCEDURE P_CUST()
-> BEGIN
-> SELECT * FROM CUST ;
-> END;
-> /
Query OK, 0 rows affected (0.20 sec)
mysql> CALL P_CUST;/
+-------+--------+-------+------+
| CNAME | CITY | O_AMT | SID |
+-------+--------+-------+------+
| POOJA | DELHI | 20000 | S1 |
| NEHA | KANPUR | 30000 | S2 |
| RESHU | NOIDA | 40000 | S3 |
+-------+--------+-------+------+
3 rows in set (0.04 sec)

Q.3 In procedure usings the functions as average , length, sum ,count etc.
mysql> create procedure pro()
-> begin
-> select avg(o_amt) from cust;
-> select sum(o_amt) from cust;
-> select length(cname) from cust;
-> select count(*) from cust;
-> end;
-> /
Query OK, 0 rows affected (0.00 sec)

mysql> call pro;/


+------------+
| avg(o_amt) |
+------------+
| 30000.0000 |
+------------+
1 row in set (0.00 sec)

+------------+
| sum(o_amt) |
+------------+
| 90000 |
+------------+
1 row in set (0.01 sec)

+---------------+
| length(cname) |
+---------------+
| 5|
| 4|
| 5|
+---------------+
3 rows in set (0.02 sec)

+----------+
| count(*) |
+----------+
| 3|
+----------+
ASSIGNMENT – 5

EXERCISE ON GROUP BY AND ORDER BY CLAUSE


Q.1 Show all customers with group by city clause.
mysql> select count(cid) from customers group by city;
+------------+
| count(cid) |
+------------+
| 1|
| 2|
| 1|
| 1|
| 1|
| 1|
| 1|
+------------+
7 rows in set (0.00 sec)

Q.2 Display salesman details order by sid and group by product.


mysql> select * from salesman group by product order by sid;
+------+--------------+-----------+--------+-----------+----------+------+---------+
| SID | SNAME | CITY | SALARY | PRODUCT | TGTTOGET | COMM | COUNTRY |
+------+--------------+-----------+--------+-----------+----------+------+---------+
| S101 | AJAY PATEL | AHMEDABAD | 10000 | PRODUCT A | 3000 | 1200 | NULL |
| S102 | CHINTAN SHAN | BARODA | 20000 | PRODUCT B | 4000 | 1500 | NULL |
| S103 | VINAY MEHRA | PUNE | 6000 | PRODUCT C | 2000 | 1200 | NULL |
+------+--------------+-----------+--------+-----------+----------+------+---------+
3 rows in set (0.00 sec)

Q.3 In orders not display the duplicity in table.


mysql> select count(distinct oid) , count(distinct oid) from orders1;
+---------------------+---------------------+
| count(distinct oid) | count(distinct oid) |
+---------------------+---------------------+
| 11 | 11 |
+---------------------+---------------------+
1 row in set (0.09 sec)

Q.4 Display the system date of our system.


mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| 2019-04-07 00:13:01 |
+---------------------+
1 row in set (0.07 sec)
Q.5 Find the number of salesman dealing with more than two
customers.
mysql> SELECT COUNT(SID)
-> FROM ORDERS1
-> GROUP BY SID
-> HAVING COUNT(CID)>2;
+------------+
| COUNT(SID) |
+------------+
| 4|
+------------+
1 row in set (0.00 sec)

Q.6 Count the number of products sold by more than once


salesman.
mysql> SELECT COUNT(PRODUCT)
-> FROM SALESMAN
-> GROUP BY SID
-> HAVING COUNT(PRODUCT)>2;
Empty set (0.00 sec)

Q.7 Count the number of customers who purchased more than one
product.
mysql> SELECT COUNT(CID)
-> FROM CUSTOMERS
-> GROUP BY CID
-> HAVING COUNT(PRODUCT)>1;
Empty set (0.00 sec)

You might also like