Structure Query Language
Structure Query Language
Query
Language
Q.1 Given the following relation student
Relation Student :
No. Name Age Department Dateofadm 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
Write SQL commands for (a) to (f) and write output for (g).
a) To show all information about the students of History department.
b) To list the names of female students who are in Hindi department.
c) To list names of all students with their date of admission in ascending order.
d) To display students Name, Fee, Age for male students only.
e) To count the number of student with Age < 23.
f) To insert a new row in the STUDENT table with the following data :
9, Zaheer, 36, Computer, {12/03/97}, 230, M
g) Give the output of following SQL statements :
i. Select COUNT (distinct department) from STUDENT;
ii. Select MAX (Age) from STUDENT where Sex=F;
iii. Select AVG (Fee) from STUDENT where Dateofadm < {01/01/98};
iv. Select SUM (Fee) from STUDENT where Dateofadm < {01/01/98};
CREATION AND INSERTION<<<<<<
SQL> create table student
2 (no number primary key,
3 name char(20),
4 age number,
5 department char(20),
6 dateofadm date,
7 fee number,
8 sex char(2));
Table Created.
SQL> desc student;
Name Null? Type
NO NOT NULL NUMBER
NAME CHAR(20)
AGE NUMBER
DEPARTMENT CHAR(20)
DATEOFADM DATE
FEE NUMBER
SEX CHAR(20)
SQL> insert into student
2 values
3 (1,Pankaj,24,Computer,10/01/97,120,M);
1 row created.
SQL> insert into student
2 values
3 (2,Shalini,21,History,24/03/98,200,F);
1 row created.
SQL> insert into student
2 values
3 (3,Sanjay,22,Hindi,12/12/96,300,M);
1 row created.
SQL> insert into student
2 values
3 (4,Sudha,25,History,01/07/99,400,F);
1 row created.
SQL> insert into student
2 values
3 (5,Rakesh,22,Hindi,05/09/97,250,M);
1 row created.
SQL> insert into student
2 values
3 (6,Shakeel,30,History,27/06/98,300,M);
1 row created.
SQL> insert into student
2 values
3 (7,Surya,34,Computer,25/02/97,210,M);
1 row created.
SQL> insert into student
2 values
3 (8,Shikha,23,Hindi,31/07/97,200,F);
1 row created.
SQL> select * from student;
NO NAME AGE DEPARTMENT DATEOFADM 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
8 rows selected.
SOLUTIONS<<<<<<
COMMANDS<<<<<<
a) SQL> SELECT * FROM Student
WHERE Department= History;
b) SQL> SELECT Name FROM Student
WHERE sex= F and Department= Hindi;
c) SQL> SELECT name FROM Student
ORDER BY Dateofadm;
d) SQL> SELECT Name, Fee, Age FROM Student
WHERE sex= M;
e) SQL> SELECT COUNT (*) FROM Student
WHERE Age > 23;
f) SQL> INSERT INTO Student
VALUES (9, Zaheer, Computer, {12/03/97}, 230, M);
OUTPUTS<<<<<<
i.
ii.
iii.
iv.
COUNT (DISTINCT DEPARTMENT)
3
MAX (AGE)
25
AVG (FEE)
236
SUM (FEE)
1080
Q.2 Given the following tables for a database LIBRARY:
TABLE: BOOKS
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 A.W. 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
Write SQL queries for (a) to (f):
a) To show Book name, Author name and Price of books of First Publ.
publishers.
b) To list the names from books of Text type.
c) To display the names and price from books in ascending order of their price.
d) To increase the price of all books of EPB Publishers by 50.
e) To display the Book_Id, Book_Name and Quantity_issued for all books which have been
issued.
f) To insert a new row in the table Issued having the following data: F0003,1.
g) Give the output of the following queries based on the above tables:
i. SELECT COUNT(*) FROM Books;
ii. SELECT MAX(Price) FROM Books WHERE Quantity >=15;
iii. SELECT Book_Name, Author_Name FROM Books WHERE Publisher =EPB;
iv. SELECT COUNT(DISTINCT Publishers) FROM Books WHERE Price>=400;
CREATIONS AND INSERTIONS<<<<<<
SQL> create table books
2 (book_id char(20) primary key,
3 book_name char(20),
4 author_name char(20),
5 publishers char(10),
6 price number,
7 type char(20),
8 qty number);
Table created.
SQL> desc books;
Name Null? Type
BOOK_ID NOT NULL CHAR(20)
BOOK_NAME CHAR(20)
AUTHOR_NAME CHAR(20)
PUBLISHERS CHAR(10)
PRICE NUMBER
TYPE CHAR(20)
QTY NUMBER
SQL> create table issued
2 (book_id char(20) primary key,
3 quantity_issued number);
Table created.
SQL> desc issued;
Name Null? Type
BOOK_ID NOT NULL CHAR(20)
QUANTITY_ISSUED NUMBER
SQL>insert into books
2 values
3 (C0001, Fast Cook, Lata Kapoor, EPB, 355, Cookery, 5);
1 row created.
SQL>insert into books
2 values
3 (F0001, The Tears, William Hopkins, First Publ., 650, Fiction, 20);
1 row created.
SQL>insert into books
2 values
3 (T0001, My First C++, Brian & Brooke, EPB, 350, Text, 10);
1 row created.
SQL>insert into books
2 values
3 (T0001, C++ Brainworks, A.W. Rossaine, TDH, 350, Text, 15);
1 row created.
SQL>insert into books
2 values
3 (F0001, Thunderbolts, Anna Roberts, First Publ., 750, Fiction, 50);
1 row created.
SQL>insert into issued
2 values
3 (T0001, 4);
1 row created.
SQL>insert into issued
2 values
3 (C0001, 5);
1 row created.
SQL>insert into issued
2 values
3 (F0001,2);
1 row created.
SQL> select * from books;
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 A.W. Rossaine TDH 350 Text 15
F0002 Thunderbolts Anna Roberts First Publ. 750 Fiction 50
5 rows selected.
SQL> select * from issued;
3 rows selected.
SOLUTIONS<<<<<<
COMMANDS<<<<<<
a) SQL> SELECT Book_Name, Author_Name, Price
FROM Books
ORDER BY Price;
b) SQL> SELECT Book_Name FROM Books
WHERE Type=Text;
c) SQL> SELECT Book_Name, Price FROM Books
ORDER BY Price;
d) SQL> UPDATE Books
SET Price=Price+50
WHERE Publishers=EPB;
e) SQL> SELECT Books.Book_Id, Book_Name,
Quantity_Issued FROM Books, Issued
WHERE Books.Book_Id=Issued.Book_Id;
f) SQL> INSERT INTO Issued
VALUES (F0003, 1);
OUTPUTS<<<<<<
i.
Book_ID Quantity_Issued
T0001 4
C0001 5
F0001 2
COUNT(*)
5
ii.
iii.
iv.
MAX(Price)
750
BOOK_NAME AUTHOR_NAME
Fast Cook Lata Kapoor
My First C++ Brian & Brooke
COUNT(DISTINCT Publishers)
1
Q.3 Consider the following table STORE and SUPPLIERS and answer (a) and (b) parts of this question.
RELATION: STORE
Itemno Item Scode Qty Rate LastBuy
2005 Sharpener Classic 23 60 8 31-Jun-09
2003 Ball Pen 0.25 22 50 25 01-Feb-10
2002 Gel Pen Premium 21 150 12 24-Feb-10
2006 Gen Pen Classic 21 250 20 11-Mar-09
2001 Eraser Small 22 220 6 19-Jan-09
2004 Eraser Big 22 110 8 01-Dec-09
2009 Ball Pen 0.5 21 180 18 03-Nov-09
RELATION: SUPPLIERS
Scode Sname
21 Premium Stationers
23 Soft Plastics
22 Tetra Supply
a) Write SQL commands for the following statements :
i. To display details of all items in the Store table in ascending order of LastBuy.
ii. To display ItemNo and Item name of those items from Store table whose Rate is more
than 18 Rupees.
iii. To display the details of those items whose Suppliers code (Scode) is 22 or Quantity in
Store (Qty) is more than 110 from the table Store.
iv. To display Minimum Rate of items for each Supplier individually as per Scode from the
table Store.
b) Give the output of the following SQL queries :
i. SELECT COUNT (DISTINCT Scode) FROM Store;
ii. SELECT Rate*Qty FROM Store WHERE ItemNo = 2004;
iii. SELECT Item, Sname FROM Store S, Suppliers P WHERE S.Scode = P.Scode AND
ItemNO=2006;
CREATION AND INSERTION<<<<<<
SQL> create table store
2 (itemno number primary key,
3 item char(20),
4 scode number,
5 qty number,
6 rate number,
7 lastbuy date);
Table Created.
SQL> desc Store;
Name Null? Type
ITEMNO NOT NULL NUMBER
ITEM CHAR(20)
SCODE NUMBER
QTY NUMBER
RATE NUMBER
LASTBUY DATE
SQL> create table suppliers
2 (scode number primary key,
3 sname char(20));
Table Created.
SQL> desc Suppliers;
Name Null? Type
SCODE NOT NULL NUMBER
SNAME CHAR(20)
SQL> insert into store
2 values
3 (2005, Sharpener Classic, 23, 60, 8, 31-Jun-09);
1 row created.
SQL> insert into store
2 values
3 (2003, Ball Pen 0.25, 22, 50, 25, 01-Feb-10);
1 row created.
SQL> insert into store
2 values
3 (2002, Gel Pen Premium,21, 150, 12, 24-Feb-10);
1 row created.
SQL> insert into store
2 values
3 (2006, Gel Pen Classic, 21, 250, 20, 11-Mar-09);
1 row created.
SQL> insert into store
2 values
3 (2001, Eraser Small, 22, 220, 6, 19-Jan-09);
1 row created.
SQL> insert into store
2 values
3 (2004, Eraser Big, 22, 110, 8, 02-Dec-09);
1 row created.
SQL> insert into store
2 values
3 (2009, Ball Pen 0.5, 21, 180, 18, 03-Nov-09);
1 row created.
SQL> insert into suppliers
2 values
3 (21, Premium Stationers);
1 row created.
SQL> insert into suppliers
2 values
3 (23, Soft Plastics);
1 row created.
SQL> insert into suppliers
2 values
3 (22, Tetra Supply);
1 row created.
SQL> select * from store;
ITEMNO ITEM SCODE QTY RATE LASTBUY
2005 Sharpener Classic 23 60 8 31-Jun-09
2003 Ball Pen 0.25 22 50 25 01-Feb-10
2002 Gel Pen Premium 21 150 12 24-Feb-10
2006 Gel Pen Classic 21 250 20 11-Mar-09
2001 Eraser Small 22 220 6 19-Jan-09
2004 Eraser Big 22 110 8 02-Dec-09
2009 Ball Pen 0.5 21 180 18 03-Nov-09
7 rows selected.
SQL> select * from suppliers;
Scode Sname
21 Premium Stationers
23 Soft Plastics
22 Tetra Supply
3 rows selected.
SOLUTIONS<<<<<<<
COMMANDS
i. SQL> SELECT * FROM Store
ORDER BY LastBuy;
ii. SQL> SELECT ItemNo, Item FROM
STORE WHERE Rate>15;
iii. SQL> SELECT * FROM Store
WHERE Scode=22 OR Qty>110;
iv. SQL> SELECT MIN (DISTINCT Rate)
FROM Store
GROUP BY Scode;
OUTPUTS<<<<<<
i.
COUNT (DISTINCT Scode)
3
ii.
Rate*Qty
880
iii.
Item Sname
Gel Pen Classic Premium Stationers
iv.
MAX (LastBuy)
24-Feb-10