Practical No: 1: TASK: Given The Following Tables For A Database LIBRARY
Practical No: 1: TASK: Given The Following Tables For A Database LIBRARY
+-----------+---------------------+-----------------------+---------------+-------+-----------+------+
| book_id | book_name
| author_name
| publishers | price | type
| qty |
+-----------+---------------------+-----------------------+---------------+-------+-----------+------+
| C0001 | Fast cook
| Lata Kapoor
| EPB
| 355 | Cookery | 5 |
| F0001 | The Tears
| William Hopkins | First Publ | 650 | Fiction |20 |
| T0001 | My first c++
| Brian & Brooke | EPB
| 350 | Text
|10 |
| T0002 | C++ Brainworks | AW Rossaine
| TDH
| 350 | Text
|15 |
| F0002 | Thunderbolts
| Anna Roberts
| First publ | 750 | Fiction |50 |
+-----------+----------------------+----------------------+---------------+-------+------------+------+
TABLE: ISSUED
+---------+--------------------+
| book_id | Quantity_issued |
+---------+--------------------+
| T0001 |
4
|
| C0001 |
5
|
| F0001 |
2
|
+---------+--------------------+
QUERY QUESTIONS:
(a) To show Book name, Author name and Price of books of first Publ.
publishers.
SOLUTION:
SELECT book_name, author_name, price
FROM books
WHERE publishers=first publ;
OUTPUT:
+----------------+---------------------+-------+
| book_name | author_name
| price |
+--------------- +---------------------+-------+
| The Tears
| William Hopkins | 650 |
| Thunderbolts | Anna Roberts
| 750 |
+----------------+---------------------+-------+
(b) To list the names from books of Text type.
SOLUTION:
SELECT book_name
FROM books
WHERE type=text;
OUTPUT:
+--------------------+
| book_name
|
+--------------------+
| My first c++
|
| C++ Brainworks |
+--------------------+
(c) To display the names and price from books in ascending order of their price.
SOLUTION:
SELECT book_name, price
FROM books
ORDER BY price;
OUTPUT:
+--------------------+-------+
| book_name
| price |
+--------------------+-------+
| My first c++
| 350 |
| C++ Brainworks | 350 |
| Fast cook
| 355 |
| The Tears
| 650 |
| Thunderbolts
| 750 |
+--------------------+-------+
(d) To increase the price of all books of EPB Publishers by 50.
SOLUTION:
UPDATE books
SET price=price+50
WHERE publishers=EPB;
OUTPUT:
+-------+------------+
| price | publishers |
+-------+------------+
| 405 | EPB
|
| 400 | EPB
|
+-------+------------+
(e) Display book id, book name, quantity issued, for all books which have been
issued.
SOLUTION:
SELECT books.book_id, book_name,Quantity_issued
FROM books,issued
WHERE books.book_id=issued.book_id;
OUTPUT:
+---------+-----------------+---------------------+
| book_id | book_name | Quantity_issued |
+---------+-----------------+---------------------+
| C0001 | Fast cook
|
5
|
| F0001 | The Tears
|
2
|
| T0001 | My first c++ |
4
|
+---------+----------------+----------------------+
(f) To insert a new row in the table Issued having the following data: F0003,1.
SOLUTION:
+---------------+--------------------+
iv) SELECT COUNT(DISTINCT publishers) FROM books WHERE price>=400;
OUTPUT:
+--------------------------------------+
| COUNT(DISTINCT publishers) |
+--------------------------------------+
|
2
|
+--------------------------------------+
PRACTICAL NO: 2
TASK : Given the following table STUDENT
TABLE: student
+----+-----------+------+-------------+------------+------+------+
|No.|Name
| Age |Dept
| DOA
| Fee | Sex|
+----+-----------+------+-------------+------------+------+------+
|1 | Pankaj |24 |computer |10/01/97| 120 |M |
|2 | Shalini |21 |history
|24/03/98| 200 |F
|
|3 | Sanjay |22 |hindi
|12/12/96| 300 |M |
|4 | Sudha |25 |history
|01/07/99| 400 |F
|
|5 | Rakesh |22 |hindi
|05/09/97| 250 |M |
|6 | Shakeel |30 |history
|27/06/98| 300 |M |
|7 | Surya |34 |computer |25/02/97| 210 |M |
|8 | Shikha |23 |hindi
|31/07/97| 200 |F
|
+----+-----------+------+--------------+------------+------+-----+
->(No. integer,
-> Name char(20),
-> Age integer,
-> Dept char(20),
-> DOA date,
-> fee integer,
-> sex char);
INSERT INTO student
-> VALUES(1,Pankaj',24,'computer',10/01/97,120,M);
QUERY QUESTIONS:
(a)Show all information about students of history.
SOLUTION:
SELECT* FROM student
WHERE Dept=history;
OUTPUT:
+----+----------+-------+---------+------------+-------+------+
|No.|Name | Age |Dept | DOA
| Fee | Sex |
+----+----------+-------+---------+------------+-------+------+
|2 | Shalini |21 |history |24/03/98| 200 |F
|
|4 | Sudha |25 |history |01/07/99| 400 |F
|
|6 | Shakeel|30 |history |27/06/98| 300 |M
|
+----+----------+-------+---------+------------+-------+------+
OUTPUT:
+----------+
|Name |
+----------+
| Shikha |
+----------+
(c) List names of all students with their DOA in ascending order.
SOLUTION:
SELECT name FROM student
ORDER BY DOA;
OUTPUT:
+---------+
|Name |
+---------+
|Sanjay |
|Pankaj |
|Surya |
|Shikha |
|Rakesh |
|Shalini |
|Shakeel|
|Sudha |
+----------+
(d) Display students Name,Fee,age for male students.
SOLUTION:
SELECT Name,Fee,Age
FROM student
WHERE sex=M;
OUTPUT:
+----------+------+-------+
|Name | Age | Fee |
+-----------+-----+-------+
| Pankaj |24 | 120 |
| Sanjay |22 | 300 |
| Rakesh |22 | 250 |
| Shakeel|30 |300 |
| Surya |34 | 210 |
+-----------+----1-+------+
(e) Count no of students with age<23.
SOLUTION:
SELECT COUNT(*) FROM student
WHERE Age>23;
OUTPUT:
+------------+
|COUNT(*)|
+------------+
|3
|
+------------+
(f)Insert new row in student with the following data:
9,Zaheer,36,computer,{12/03/97},230,M
SOLUTION:
INSERT INTO student
VALUES(9,Zaheer,36,computer,{12/03/97},230,M);
OUTPUT:
+----+----------+-------+-------------+------------+-------+------+
|No.|Name | Age |Dept
| DOA
| Fee | Sex |
+----+-----------+------+-------------+------------+-------+------+
|1 | Pankaj |24 |computer |10/01/97| 120 |M
|
|2 | Shalini |21 |history
|24/03/98| 200 |F
|
|3 | Sanjay |22 |hindi
|12/12/96| 300 |M
|
|4 | Sudha |25 |history
|01/07/99| 400 |F
|
|5 | Rakesh |22 |hindi
|05/09/97| 250 |M
|
|6 | Shakeel|30 |history
|27/06/98| 300 |M
|
|7 | Surya |34 |computer |25/02/97| 210 |M
|
|8 | Shikha |23 |hindi
|31/07/97| 200 |F
|
|9 | Zaheer | 36 | computer|12/03/97 |230 |M
|
+----+-----------+------+-------------+-------------+------+------+
PRACTICAL NO: 3
TASK : Given following tables for a database FURNITURE
TABLE: furniture
+----+-----------------+--------------+---------------+--------+-----------+
| no | itemname
| type
| DOS
| price | discount |
+----+-----------------+--------------+---------------+--------+-----------+
| 1 | White lotus | double bed | 2002-02-23 | 30000 |
25 |
| 2 | pink feather | baby cot
| 2002-01-20 | 7000 |
20 |
| 2 | dolphin
| baby cot
| 2002-02-19 | 9500 |
20 |
| 4 | decent
| office table | 2002-01-01 | 25000 |
30 |
| 5 | comfort zone | double bed | 2002-01-12 | 25000 |
25 |
| 6 | donald
| baby cot
| 2002-02-24 | 6500 |
15 |
| 7 | royal finish
| office table | 2002-02-20 | 18000 |
30 |
| 8 | royal tiger
| sofa
| 2002-02-22 | 31000 |
30 |
| 9 | econo sitting | sofa
| 2001-12-13 | 9500 |
25 |
| 10 | eating paradise | dining
| 2002-02-02 | 11500 |
25 |
+----+------------------+--------------+-------------- +--------+-----------+
SOLUTION:
SELECT itemname, type FROM furniture WHERE DOS<2002/01/22 ORDER BY itemname;
OUTPUT:
+---------------+----------------+
| itemname | type
|
+---------------+----------------+
| comfort zone | double bed |
| decent
| office table |
| econo sitting | sofa
|
| pink feather | baby cot
|
+---------------+----------------+
(d) display itemname and DOS of those items ,in which discount% is more than 25, from
furniture table.
SOLUTION:
SELECT itemname,DOS FROM furniture WHERE discount>25;
OUTPUT:
+--------------+--------------+
| itemname | DOS
|
+--------------+--------------+
| decent
| 2002-01-01 |
| royal finish | 2002-02-20 |
| royal tiger | 2002-02-22 |
+--------------+--------------+
(e) Count the no of items, whose type is sofa from furniture table.
SOLUTION:
SELECT Count(*) FROM furniture WHERE type=sofa;
OUTPUT:
+----------+
| Count(*) |
+----------+
|
2
|
+----------+
(f) FIND THE OUTPUTS:
i)
SELECT COUNT(distinct TYPE) FROM furniture;
ii)
SELECT MAX(DISCOUNT) FROM furniture, arrivals;
iii)
SELECT AVG(DISCOUNT) FROM furniture where type=baby cot;
iv)
SELECT SUM(PRICE) FROM furniture where DOS<2002-02-12;
SOLUTION:
i)
5
ii)
30
iii)
18.33
iv)
66500
(g) insert new values in arrival: 14,"velvet touch","double bed",'2003-03-25',25000,30
SOLUTION:
INSERT INTO arrivals VALUES(14,"velvet touch","double bed",'2003-03-25',2
5000,30);
OUTPUT:
+------+----------------+---------------+---------------+--------+---------- +
| no
| itemname | type
| DOS
| price | discount |
+------+----------------+---------------+---------------+--------+-----------+
| 11 | Wood comfort | double bed | 2003-03-03 | 25000 |
25 |
| 12 | old fox
| sofa
| 2003-02-20 | 17000 |
20 |
| 13 | micky
| baby cot
| 2003-02-21 | 7500 |
15 |
| 14 | velvet touch | double bed | 2003-03-25 | 25000 |
30 |
+----+------------------+---------------+---------------+---------+-----------+