Q.
1 Observe the following tables Customers and Orders and write queries given below:
Table – Customer
CustomerID CustomerName City MobileNo
C111 Abhishek Ahmedabad 9999999999
C132 Bhavik Anand 7799779977
C135 Chandani Baroda 8856895485
C145 Dhara Ahmedabad 7456879652
C121 Divya Anand 9015123569
Table – Order
OrderID OrderDate OrderAmt CustomerID
O111 2022-04-15 1500 C111
O112 2022-05-20 1800 C121
O113 2022-05-31 1000 C199
O131 2022-06-12 1400 C135
i. Display CustomerID, CustomerName, and OrderAmt for customers
belonging to Ahmedabad.
ii. Display the order details in descending order of amount.
iii. Display OrderId, Orderdate , customername and mobileno of customers
whose name ends with ‘k’ and name contains letter ‘h’.
iv. Display the sum of order amount for each city.
Ans.: select customerid, customername, orderamt from customer, order where
customer.customerid = order.customerid and city=’ahmedabad’;
Ans.: select * from order order by orderamt desc;
Ans.: select orderid, orderdate, customername, mobileno from customer, order where
customer.customerid = order.customerid and (customername like ‘%k’ or customername like
‘%h%’;
Ans.: select city,sum(orderamt) from customer,order where customer.customerid = order.customerid
group c
Q.2 Consider the tables books and issue write SQL command for given statements:
i. Write command to add primary key and foreign key in tables using alter.
ii. Increase the price of all computer books by 70.
iii. Display the book id, book name and quantity issued for all books which have been
issued.
iv. Display the book id, book name, author name, quantity issued for allbooks which
price are 200 to 400.
Ans.:
alter table books add primary key (BID);
alter table issued add foreign key references books (bid);
Ans.:
update books set price=price + 70 where type='computer';
Ans.:
select books.bid,bname,qty_issued from books,issued where books.bid=issued.bid;
Ans.:
select books.bid,bname,auname,qty_issued from books,issued
where books.bid=issued.bid and price>=200 and price<=400;
Q.3 Write queries (a) to (d) based on the tables EMPLOYEE and DEPARTMENT given
below:
i. .
ii
iii
iv
SELECT AVG(SALARY) FROM EMPLOYEE GROUP BY DEPTID;
SELECT NAME, DEPTNAME FROM EMPLOYEE, DEPARTMENT WHERE
EMPLOYEE.DEPTID= DEPARTMENT.DEPTID AND SALARY>50000;
SELECT NAME FROM EMPLOYEE WHERE SALARY IS NULL ORDER BY NAME;
SELECT DISTINCT DEPTID FROM EMPLOYEE;
Q.4
Q.5
Q.6