SQL Queries
SQL Queries
(itemno int, item char (20), scode int, qty int, rate int, lastbuy date);
SQL> insert into STORE values (2005,'Sharpener Classic', 23, 60, 8,'30-jun-09');
SQL> insert into STORE values (2003,'Ball Pen 0.25', 22, 50, 25,'1-Feb-10');
SQL> insert into STORE values (2002,'Gel Pen Premium', 21, 150, 12,'24-Feb-10');
Table: SUPPLIERS
scode sname
21 Premium Staioners
23 Soft Plastics
22 Tera Supply
1
Que) Write SQL commands for the following statements:
(i) Display details of all the items in the STORE table in ascending order of lastbuy.
Select * from STORE order by lastbuy;
(ii) Display Itemno and itemname of those items from STORE table whose rate is more
than 15 rrupees.
Select itemno,item from STORE where rate > 15;
(iii) Display details of those items whose supplier code (scode) is 22 or Quantity in Store
(Qty) is more than 110 from table STORE.
Select * from STORE where (scode=22 or qty >110);
(iv) To display minimum rate of items whose suppliers individually as per scode from
table STORE.
Select scode, min (rate) from STORE group by scode;
(v) Display item name, qty of items of supplier Tera Supply.
Select item,qty from STORE S, Suppliers P
where (S.scode=P.scode and P.sname='Tera Supply';
2
Table: WORKER
Table:PAYLEVEL
SQL> create table PAYLEVEL (plevel char (5), pay decimal, allowance decimal);
SQL> insert into paylevel values ('P001',26000,12000);
SQL> insert into paylevel values('P002',22000,10000);
SQL> insert into paylevel values('P003',12000,6000);
3
(ii) To display NAME and DESIG of those WORKERs, whose PLEVEL
is either P001 or P002.
Ans: Select NAME, DESIGN from WORKER where PLEVEL in (‘P001’,’P002’);
(iii) To display the content of all the WORKERs table, whose DOB is in
between '19-JAN-1984' and '18-JAN-1987'.
Ans: Select * from WORKER where ( DOB between '19-JAN-1984' and '18-JAN-1987' );
(iv) To add a new row with the following:
19, 'Daya Kishore', 'Operator', 'P003', '19-Jun-2008', '11-Jun-1984'
Ans: insert into WORKER values(19, 'Daya Kishore', 'Operator', 'P003', '19-Jun-2008',
'11-Jun-1984');
(v) Display pay and allowances of ‘Fizza’.
Ans: Select NAME, PAY,ALLOWANCES from WORKER W, PAYLEVEL P
where (W.PLEVEL = P.PLEVEL and W.NAME=’Fizza’);
(c) Give the output of the following SQL queries:
(i) SELECT COUNT (PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;
Ans:COUNT(PLEVEL) PLEVEL
------------------- ------------
1 P001
2 P002
2 P003
4
Table: PROUDCT
PID Productname Manufacturer Price
TP01 Talcum Powder LAK 40
FW05 Face Wash ABC 45
BS01 Bath Soap ABC 55
SH06 Shampoo XYZ 120
FW12 Face Wash XYZ 95
5
3) Display ClientName, City from table Client, ProductName and Price from table Product with
their corresponding matching PID.
Select ClientName, City, ProductName, Price
from Client C, Product P
where C.PID=P.PID;
4) Increase the price of all products by 10.
Update Product
Set Price=Price+10;
5) Display details of products in ascending order of price.
Select * from Product order by price asc;
(Que) Give the output of the following SQL queries:
1) SELECT DISTINCT City from Client;
City
-------
Delhi
Mumbai
Banglore
2) SELECT Manufacturer, MAX (Price), Min (Price), Count(*)
From Product Group By Manufacturer;
Manufacturer MAX(Price) MIN(Price) Count(*)
---------------------------------------------------------------------------
LAK 40 40 1
ABC 55 45 2
XYZ 120 95 2
3) SELECT ClientName, ManufacturerName
from Product, Client where Client.PID=Product.PID;
ClientName ManufacturerName
-------------------------------------------------------------------------
Cosemetic Shop Face Wash
Total Health Bath Soap
Live Life Shampoo
Pretty Woman Face Wash
Dreams Talcom Powder
4) SELECT ProductName, Price * 4 From Product where Price <50;
ProductName Price*4
-----------------------------------------------
Talcom Powder 160
Face Wash 180