Sunteți pe pagina 1din 4

1.

Using the insert statement fill the tables with an element


a.Depatment(dept_id,emp_id,dept_name,mgr)
b.Employess(emp_id,empname,dept_id)

Ans: INSERT INTO Department(dept_id,emp_id,dept_name,mgr)


VALUES (1,1,'Electronics','manish')
INSERT INTO Department(dept_id,emp_id,dept_name,mgr)
VALUES (2,2,'Computer','anish')

INSERT INTO Employees(empid,empname,dept_id)


VALUES(3,'Sirish',1)
INSERT INTO Employees(empid,empname,dept_id)
VALUES(4,'Rikesh',2)
.....................................................................................
2.Remove the departments without the manager
Ans:
DELETE from department WHERE mgr=NULL
..................................................................
3.Relational scheme is as following:
a.Supplier-(Supplier code,Companyname,address,town,delivery_date)
b.Order-(Ordernunber,DATE,Suppliercode)
c.OrderLine-(OrderNumber,ComponentCode,quantity)
d.Component-(Componentcode,Description,Cost)

4.Create the tables with appropriate data type


Ans:
CREATE TABLE suppliers
(
suppliercode NUMBER(2) NOT NULL,
companyname VARCHAR2(10) NOT NULL,
address VARCHAR2(50) NOT NULL,
town VARCHAR2(20) NOT NULL,
delivery_date DATE,
CONSTRAINT suppliers_pk PRIMARY KEY (suppliercode)
);
INSERT INTO suppliers(suppliercode,companyname,address,town,delivery_date)
VALUES(1,'dhaba','jawalakhel','patan',TO_DATE('08032010','mmddyyyy'))

CREATE TABLE ordered


(
ordernumber NUMBER(2) NOT NULL,
orderdate DATE,
suppliercode NUMBER(2) NOT NULL,
CONSTRAINT ordered_pk PRIMARY KEY (ordernumber),
CONSTRAINT ordered_fk FOREIGN KEY (suppliercode) references suppliers
(suppliercode) ON DELETE CASCADE
);
INSERT INTO ordered(ordernumber,orderdate,suppliercode)
VALUES(1,TO_DATE('08012010','mmddyyyy'),1)
CREATE TABLE component
(
componentcode NUMBER(2) NOT NULL,
description VARCHAR2(20) NOT NULL,
cost NUMBER(5,2) NOT NULL,
CONSTRAINT component_pk PRIMARY KEY(componentcode)
);
INSERT INTO component(componentcode,description,cost)
VALUES (1,'momo',30.00)

CREATE TABLE orderline


(
ordernumber NUMBER(2) NOT NULL,
componentcode NUMBER(2) NOT NULL,
quantity NUMBER(3) NOT NULL,
CONSTRAINT orderline_fk_ordernumber FOREIGN KEY(ordernumber) references
ordered (ordernumber) ON DELETE CASCADE,
CONSTRAINT orderline_fk_componentcode FOREIGN KEY(componentcode) references
component (componentcode) ON DELETE CASCADE
);
INSERT INTO orderline(ordernumber,componentcode,quantity)
VALUES(1,1,10)

…...........................................................................
5.Select all orders made to "Momo"
Ans:
SELECT * FROM suppliers NATURAL JOIN ordered NATURAL JOIN orderline NATURAL
JOIN component
WHERE component.description='momo'
............................................................

6.Select all order to "Momo"placed between March and April


Ans:
SELECT * FROM suppliers NATURAL JOIN ordered NATURAL JOIN orderline NATURAL
JOIN component
WHERE component.description='momo' AND TO_CHAR(ordered.orderdate,'MM') IN ('03','04')
........................................................................................................
7.Select all orders of quantities greater than 50, but lower than 100
Ans:
SELECT * FROM suppliers NATURAL JOIN ordered NATURAL JOIN orderline NATURAL
JOIN component
WHERE orderline.quantity>50 AND orderline.quantity<100
...................................................................

8.For each order show company names and address


Ans:
SELECT companyname,address FROM suppliers NATURAL JOIN ORDERED
.......................................................................
9.Find the number of days between the order and the delivery(using the correct function.Look within
("date and time functions")
Ans:
SELECT ROUND(delivery_date-orderdate) FROM suppliers NATURAL JOIN ordered
.......................................................................................
10.Practice aggregate function on the quantity field and by creating a calculated field.
Ans:
SELECT componentcode,MAX(quantity) AS Maximium Quantity FROM orderline
GROUP BY componentcode

SELECT componentcode,MIN(quantity) AS Maximium Quantity FROM orderline


GROUP BY componentcode

SELECT componentcode,AVG(quantity) AS Maximium Quantity FROM orderline


GROUP BY componentcode

SELECT componentcode,SUM(quantity) AS Maximium Quantity FROM orderline


GROUP BY componentcode
...............................................................................................
11.Quering the different tables calculate:
a.the total value of each order
b.the number,the average and total value of the orders placed to each supplier
c.the total value and average delivery time of all the orders placed between March and April
Ans:
a.SELECT ordernumber,SUM("Total cost") FROM(SELECT ordernumber, (quantity*cost) AS
"Total cost" FROM orderline NATURAL JOIN component)
GROUP BY ordernumber
b.SELECT suppliercode,COUNT(ordernumber) AS"No of orders",AVG(quantity*cost) AS
"Average Cost",SUM(quantity*cost)AS "Total Cost" FROM suppliers NATURAL JOIN ordered
NATURAL JOIN orderline NATURAL JOIN component
GROUP BY suppliercode
C.SELECT SUM(quantity*cost),AVG(ROUND(delivery_date-orderdate)) FROM suppliers
NATURAL JOIN ordered NATURAL JOIN orderline NATURAL JOIN component
WHERE TO_CHAR (ordered.orderdate,'MM') IN ('03','04')
12.Suppose the database has folowing tables:
a. Vendor(vendorno,street,city,region,zip,contact)
b. Poheader(orderno,orddate,vendorno,invoiceno_status)
c. Priceinfo(partno,vendorno,catno,price)
d. Podetail(orderno,partno,qty,unit_price)
e. Part(partno)
f. Pohistory(orderno,orddate,vendorno,invoiceno,pototal)

13.Retrieve Order,Part,and Quantity information from the above table as mentioned in 12.
Ans:
SELECT orderno,partno,qty FROM podetail
14. Display the order numbers,dates and status for all orders completed and pending.Remember that all
orders in the pohistory table are completed.
Ans:
SELECT ordernumber,orderdate,invoiceno_status FROM poheader
WHERE invoiceno_staus='completed' OR invoiceno_status='pending'

15. All vendors located in city 'Leeds' are increasing their proces by 200.00.Apply this increase to the
database .
Ans: UPDATE podetail SET unit_price=(unit_price+200) WHERE orderno IN (SELECT orderno
FROM vendor NATURAL JOIN WHERE city='LEEDS')

16. Find the vendorno with second highest price from priceinfo.
Ans:
SELECT vendorno,price,Ranked_price FROM (SELECT vendorno,price,DENSE_RANK () over
(ORDER BY price) AS Ranked_price FROM priceinfo)

17.Create table dual as select* from dual;What happens when this statement is executed and why?
Ans: The above query was executed because it overwrites the existing virtual table.

18.Select'1'+'2' from dual;Does this statement show error when executed?


Ans: No,it doesn't show the error message instead,it displays the result as 3,3,3 i.e three rows inserted
with values as 3.

19.What will be the result of following queries ?


a. INSERT INTO dual VALUES('1');
b. SELECT 3 FROM dual;
Ans:
a. The value '1' was inserted in the table named dual.
b.3
3
Two rows inserted