100% found this document useful (1 vote)
2K views9 pages

All Mock

The document contains sample SQL queries and questions about retrieving data from various database tables. The questions ask to: 1. Display employee details for employees from specific companies. 2. Display locations with more than 2 cabins and the number of cabins. 3. Display unallocated cabins. 4. Display employees allocated cabins for over 10 days without duplicates. The document contains many similar questions about retrieving and summarizing data from tables using SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views9 pages

All Mock

The document contains sample SQL queries and questions about retrieving data from various database tables. The questions ask to: 1. Display employee details for employees from specific companies. 2. Display locations with more than 2 cabins and the number of cabins. 3. Display unallocated cabins. 4. Display employees allocated cabins for over 10 days without duplicates. The document contains many similar questions about retrieving and summarizing data from tables using SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

SELECT * from allocation;

SELECT * from employee;


SELECT * from allocator;
SELECT * from cabin;

1.
'Display EmployeeId and EmployeeName of those employees where their Company is
�Wipron� or �Datasys�.
The EmployeeId should display only the numeric value of the EmployeeId. for
example e101 should be displayed as 101.'

SELECT SUBSTR(EMPLOYEEID,2) EMPLOYEEID,EMPLOYEENAME


FROM EMPLOYEE WHERE COMPANY='Wipron' OR COMPANY='Datasys'
OR
SELECT SUBSTR(EMPLOYEEID,2) EMPLOYEEID,EMPLOYEENAME
FROM EMPLOYEE WHERE COMPANY IN ('Wipron','Datasys')

2.
'Display the Location and number of cabins in each location provided the number of
cabins in that location is more than 2.
For the above example, location �GEC� has 3 cabins and must be included in the
result.'

SELECT LOCATION,COUNT(CABINID) "NUMBER OF CABINS" FROM CABIN


GROUP BY LOCATION HAVING COUNT(CABINID)>2

3.
'Display the CabinId and Location of those cabins which are never allocated to any
employee.
Note: allocated cabin is one that is present in the allocation table.'

SELECT CABINID,LOCATION FROM CABIN WHERE CABINID NOT IN


(SELECT CABINID FROM ALLOCATION)
OR
SELECT C.CABINID,C.LOCATION FROM CABIN C
LEFT JOIN ALLOCATION A ON C.CABINID<>A.CABINID WHERE A.CABINID IS NULL

4.
'Display EmployeeId and EmployeeName of those employees who have been allocated
with the cabins for more than 10 days.
Do not display duplicate records.
Note: There can be more than 1 row in the allocation table for a given employee
with more than 10 days.
The employee should appear only once in the result.'

SELECT DISTINCT EMPLOYEEID,EMPLOYEENAME FROM EMPLOYEE


WHERE EMPLOYEEID IN(SELECT EMPLOYEEID FROM ALLOCATION WHERE NOOFDAYS>10)
OR
SELECT DISTINCT E.EMPLOYEEID,E.EMPLOYEENAME FROM EMPLOYEE E
INNER JOIN ALLOCATION A ON E.EMPLOYEEID=A.EMPLOYEEID WHERE A.NOOFDAYS >10

5.
'Display AllocationId of those allocations where charge collected is more than the
average charge collected for all the allocations.
For example for the above data, the average charge collected is 3000. So
AllocationId 107 will be one of the roes in the output.
'
SELECT ALLOCATIONID FROM ALLOCATION WHERE CHARGE >
(SELECT AVG(CHARGE) FROM ALLOCATION)

6.
'For each company display the Company name and total number of employees of that
company who are allocated with a cabin.'

SELECT COMPANY,COUNT(EMPLOYEEID) "NUMBER OF EMPLOYEES"


FROM EMPLOYEE WHERE EMPLOYEEID IN
(SELECT EMPLOYEEID FROM ALLOCATION) GROUP BY COMPANY

7.
'Display the CabinId and Location of the cabin which has been allocated for maximum
number of times.'

SELECT C.CABINID,C.LOCATION FROM CABIN C


INNER JOIN ALLOCATION A ON C.CABINID=A.CABINID
GROUP BY C.CABINID,C.LOCATION HAVING COUNT(A.CABINID)=
(SELECT MAX(COUNT(CABINID)) FROM ALLOCATION GROUP BY CABINID)
OR
SELECT CABINID,LOCATION FROM CABIN WHERE CABINID IN
(SELECT CABINID FROM ALLOCATION GROUP BY CABINID HAVING COUNT(CABINID)=
(SELECT MAX(COUNT(CABINID)) FROM ALLOCATION GROUP BY CABINID))

8.
'Display the CabinId, EmployeeId, EmployeeName and NoOfDays for which the same
AllocatorId appears more than twice in the allocation table.
For example, AllocatorId A101 appears 3 times in the allocation table. So the
following row will be part of the output:'

SELECT A.CABINID,E.EMPLOYEEID,E.EMPLOYEENAME,A.NOOFDAYS FROM EMPLOYEE E


INNER JOIN ALLOCATION A ON E.EMPLOYEEID=A.EMPLOYEEID WHERE A.ALLOCATORID IN
(SELECT A.ALLOCATORID FROM ALLOCATION A GROUP BY A.ALLOCATORID HAVING
COUNT(A.ALLOCATORID)>=2)

9.
'Display the AllocatorId and AllocatorName of the allocators who have allocated
different cabins to the same employee.
Write the query using JOIN concept.'

SELECT AL.ALLOCATORID,AL.ALLOCATORNAME FROM ALLOCATOR AL


INNER JOIN ALLOCATION A ON AL.ALLOCATORID=A.ALLOCATORID INNER JOIN
ALLOCATION A1 ON A1.EMPLOYEEID=A.EMPLOYEEID AND A1.CABINID<>A.CABINID

10.
'Identify the Allocator who allocates the same cabin to different employees.
Display the CabinId, AllocationId, EmployeeId and AllocatorId for the identified
cabins.'

SELECT a.CabinId, a.AllocationId, a.EmployeeId, a.AllocatorId


FROM Allocation a
INNER JOIN Allocation aa
ON a.CabinId=aa.CabinId
AND a.AllocatorId=aa.AllocatorId
AND a.EmployeeId<>aa.EmployeeId

'Write a query for the same requirement using Correlated Subquery.'


SELECT a.CabinId,a.AllocationId,a.EmployeeId,a.AllocatorId
FROM Allocation a
WHERE a.CABINID IN (SELECT aa.CABINID FROM ALLOCATION aa where a.cabinid=aa.cabinid
AND a.AllocatorId=aa.AllocatorId
and a.EmployeeId<>aa.EmployeeId)

'**********************************************************************************
************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
###################################################################################
################################################
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'

SELECT * FROM gift


SELECT * FROM customer1
SELECT * FROM giftorder

A.
'Display the numeric part of giftid as �GIFTNUM� (column alias),
giftname and reduction in price per quantity rounded to the nearest whole number
as
�PRICEREDUCTION� (column alias) of the �Utilities� category gifts.'

SELECT SUBSTR(GIFTID,2) GIFTNUM,GIFTNAME,ROUND(PRICE*DISCOUNT/100) PRICEREDUCTION


FROM GIFT WHERE CATEGORY='Utilities'

B.
'Display customerid and giftid of the giftorders which are been ordered for the
gifts of the category other than the �Home Decor�.
For the given sample data, following records will feature as part of the output
along with other record(s).'

SELECT G.CUSTOMERID,G.GIFTID FROM GIFTORDER G INNER JOIN GIFT G1 ON


G.GIFTID=G1.GIFTID WHERE CATEGORY!='Home Decor'

C.
'Display giftid and orderid as �ORDERS� (column alias) of all the gifts that are
been shipped to �Mysore� city.
Display �NA� in ORDERS for the gifts that are not been shipped to �Mysore� and
also for the unordered gifts.'

SELECT NVL(TO_CHAR(G.GIFTID),'NA') GIFTID,NVL(TO_CHAR(G1.ORDERID),'NA') ORDERS FROM


GIFT G
LEFT OUTER JOIN GIFTORDER G1 ON G.GIFTID=G1.GIFTID AND SHIPPINGCITY='Mysore'

D.
'Display giftid, giftname, availability and number of times the order placed as
�NOOFORDERS� (column alias)
for those gifts that are available more than 40 units and has been ordered twice
or more times.'

SELECT G.GIFTID,G.GIFTNAME,G.AVAILABILITY,COUNT(*) NOOFORDERS FROM GIFT G


INNER JOIN GIFTORDER G1 ON G.GIFTID=G1.GIFTID WHERE AVAILABILITY>40 GROUP BY
G.GIFTID,G.GIFTNAME,G.AVAILABILITY

E.
'Display orderid as �ORDERNUM� (column alias) and customerid as �CUSTOMER� (column
alias)
for all the orders as well as for all the customers. Display �N� in CUSTOMER for
customers
who do not belong to �Bangalore� and �NA� in ORDERNUM if the order is not placed
or if the
order is placed by the customers who do not belong to �Bangalore�.'

SELECT NVL(TO_CHAR(GO.orderid),'NA') ORDERNUM ,NVL(C.CUSTOMERID,'N') CUSTOMER FROM


CUSTOMER1 C
FULL OUTER JOIN GIFTORDER GO ON C.CUSTOMERID=GO.CUStOMERID and
C.LOCATION='Bangalore'

select nvl(to_char(g1.orderid),'NA') orderid,


nvl(c1.customerid,'N') customerid FROM customer1 c LEFT OUTER JOIN giftorder g1
ON c.customerid=g1.customerid LEFT OUTER JOIN customer1 c1 ON
c1.customerid=c.customerid
and c.location='Bangalore'

F.
'Display orderid, customerid and giftid for those giftorders that are been placed
by the same customer
for the �Home Decor� category gifts. For the given sample data, following record
will feature as part
of the output along with other record(s).'

SELECT DISTINCT G1.ORDERID,G1.CUSTOMERID,G1.GIFTID FROM GIFTORDER G1


INNER JOIN GIFTORDER G2 ON G1.GIFTID=G2.GIFTID INNER JOIN GIFT G
ON G2.GIFTID=G.GIFTID WHERE CATEGORY='Home Decor' AND G1.CUSTOMERID=G2.CUSTOMERID
AND G1.ORDERID<>G2.ORDERID

G.
'Identify the customer who has paid the highest total billamount. Display the
customerid, customername
and highest total billamount as �TOTALBILL� (column alias). TOTALBILL has to be
rounded to the
nearest whole number. Billamount can be calculated using price, discount and
quantity.'

SELECT C.CUSTOMERID,C.CUSTOMERNAME,ROUND(SUM(G1.QUANTITY*G.PRICE*(1-
G.DISCOUNT/100))) "TOTALBILL"
FROM CUSTOMER1 C INNER JOIN GIFTORDER G1 ON C.CUSTOMERID=G1.CUSTOMERID INNER JOIN
GIFT G ON
G1.GIFTID=G.GIFTID GROUP BY C.CUSTOMERID,C.CUSTOMERNAME HAVING
SUM(G1.QUANTITY*G.PRICE*(1-G.DISCOUNT/100))=
(SELECT MAX(SUM(G1.QUANTITY*G.PRICE*(1-G.DISCOUNT/100))) "TOTALBILL"
FROM CUSTOMER1 C INNER JOIN GIFTORDER G1 ON C.CUSTOMERID=G1.CUSTOMERID INNER JOIN
GIFT G ON
G1.GIFTID=G.GIFTID GROUP BY C.CUSTOMERID,C.CUSTOMERNAME)

H.
'Identify the giftorders that are been placed by the customers from �Delhi� for the
gifts other than �Showpiece�
category and are been shipped to �Chennai� city. Display orderid, giftid and
customerid for the identified giftorder.'

SELECT ORDERID,GIFTID,CUSTOMERID FROM GIFTORDER G1 WHERE SHIPPINGCITY='Chennai'


AND CUSTOMERID IN
(SELECT CUSTOMERID FROM CUSTOMER1 C WHERE G1.CUSTOMERID=C.CUSTOMERID AND
LOCATION='Delhi')
AND GIFTID IN
(SELECT GIFTID FROM GIFT G WHERE G1.GIFTID=G.GIFTID AND CATEGORY !='Showpiece')

'**********************************************************************************
************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
###################################################################################
################################################
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'

SELECT * FROM CUST;


SELECT * from TRAIN;
SELECT * FROM BOOKING;

A.
'Display the customerid, name and email of customers whose mobile number has the
number �2� as the third digit.
The customerid should only display the numeric part. It is known that the
customerid follows a standard pattern of
�C� followed any number of digits.(Example, for customerid C003, display 003). '

SELECT SUBSTR(CID,2) CID,NAME,EMAIL FROM CUST WHERE SUBSTR(TO_CHAR(MOBILE),3,1)='2'


OR
SELECT SUBSTR(CID,2) CID,NAME,EMAIL FROM CUST WHERE MOBILE LIKE '__2%'

B.
'Display customerid of all the customers who have done booking multiple times.
For Example customer �C004� has done multiple bookings.'

SELECT CID FROM BOOKING GROUP BY CID HAVING COUNT(CID)>1

C.
'Display the customerid of the customers who have booked a train from Surat or to
Surat.
For Example, for the given sample data, customerid �C004� would be one of the rows
in the
output along with other rows.'

SELECT CID FROM BOOKING WHERE TRAINID IN


(SELECT TRAINID FROM TRAIN WHERE SOURCE='Surat' OR DESTINATION='Surat')
D.
'Display the trainid, source, destination and stops of the trains that arrive at
the same destination.
If the number of stops is 0, display �Non-stop�. Display the records only if more
than one train arrive at the same destination. '

SELECT T.TRAINID,T.SOURCE,T.DESTINATION,
CASE TO_CHAR(T.STOPS)
WHEN '0' THEN 'NON-STOP'
ELSE TO_CHAR(T.STOPS)
END STOPS
FROM TRAIN T INNER JOIN TRAIN T1 ON T.TRAINID=T1.TRAINID WHERE
T.DESTINATION=T1.DESTINATION

E.
'For each trainid, display total number of bookings done and total number of quota
seats(ac+nonac)
whether or not a booking was made for the train.'

SELECT T.TRAINID,COUNT(BID) "BOOKINGS DONE",ACQ+NACQ "QUOTA"


FROM TRAIN T LEFT OUTER JOIN BOOKING B ON T.TRAINID=B.TRAINID GROUP BY
T.TRAINID,ACQ,NACQ

F.
'Display the customerid, bookingid and trainid for the customers who have booked a
round trip. '

SELECT B1.CID,B1.BID,B1.TRAINID FROM BOOKING B1 INNER JOIN TRAIN T1 ON


B1.TRAINID=T1.TRAINID
INNER JOIN BOOKING B2 ON B1.CID=B2.CID JOIN TRAIN T2 ON B2.TRAINID=T2.TRAINID
WHERE T1.DESTINATION=T2.SOURCE

G.
'Display the trainid and total amount of the train that has the second highest
total amount.
For example, for the given sample data, the trainid that has the second highest
total amount
is AA303 and the total amount is 8500.'

SELECT TRAINID,SUM(AMOUNT) FROM BOOKING GROUP BY TRAINID HAVING SUM(AMOUNT)=


(SELECT MAX(SUM(AMOUNT)) FROM BOOKING GROUP BY TRAINID HAVING SUM(AMOUNT) NOT IN
(SELECT MAX(SUM(AMOUNT)) FROM BOOKING GROUP BY TRAINID))

'**********************************************************************************
************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
###################################################################################
################################################
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'

SELECT * FROM Customers;


SELECT * FROM Category;
SELECT * FROM toys;
SELECT * FROM Transactions;

1.
'Display ToyId, ToyName, Stock of those toys whose stock lies within 10 and 15(both
inclusive)
and toyname contains more than 6 characters.'

SELECT TOYID,TOYNAME,STOCK FROM TOYS WHERE STOCK>=10 AND STOCK<=15 AND


LENGTH(TOYNAME)>=6
SELECT TOYID,TOYNAME,STOCK FROM TOYS WHERE STOCK BETWEEN 10 AND 15 AND
LENGTH(TOYNAME)>=6

2.
'Identify and display CustId, CustName, CustType of those customers whose name ends
with �y�.
For the selected records, if the customer type is NULL display CustType as �N�.
Do case insensitive comparison.'

SELECT CUSTID,CUSTNAME,NVL(CUSTTYPE,'N') CUSTTYPE FROM CUSTOMERS WHERE


UPPER(CUSTNAME) LIKE '%Y%'

3.
'Display CustName and total transaction cost as TotalPurchase for those customers
whose
total transaction cost is greater than 1000.'

SELECT C.CUSTNAME,SUM(TXNCOST) TotalPurchase FROM CUSTOMERS C


INNER JOIN TRANSACTIONS T ON C.CUSTID=T.CUSTID
GROUP BY C.CUSTNAME HAVING SUM(TXNCOST)>1000

4.
'For each customer display transactionid, customerid and cost of the transaction
that has the maximum cost among all transactions made by that customer.'

SELECT T1.TXNID,T1.CUSTID,T1.TXNCOST FROM TRANSACTIONS T1


WHERE TXNCOST=(SELECT MAX(TXNCOST) FROM TRANSACTIONS T2 WHERE T1.CUSTID=T2.CUSTID)

5.
'List all the toyid, total quantity purchased as 'total quantity' irrespective of
the customer.
Toys that have not been sold should also appear in the result with total units as
0'

SELECT T.TOYID,NVL(SUM(QUANTITY),0) TOTAL_QUANTITY FROM TOYS T


LEFT OUTER JOIN TRANSACTIONS T1 ON T.TOYID=T1.TOYID GROUP BY T.TOYID

6.
'List all the ToyId, ToyName, Price of those toys which have the same price'

SELECT T1.TOYID,T1.TOYNAME,T1.PRICE FROM TOYS T1


INNER JOIN TOYS T2 ON T1.TOYID=T1.TOYID WHERE T1.PRICE=T2.PRICE AND
T1.TOYID<>T2.TOYID
7.
'The CEO of Toys corner wants to know which toy has the highest total Quantity
sold.
Display CName, ToyName, total Quantity sold of this toy.'

SELECT C.CNAME,T.TOYNAME,SUM(QUANTITY) FROM CATEGORY C


INNER JOIN TOYS T ON C.CID=T.CID INNER JOIN TRANSACTIONS T1 ON
T.TOYID=T1.TOYID GROUP BY C.CNAME,T.TOYNAME HAVING SUM(QUANTITY)=
(SELECT MAX(SUM(QUANTITY)) FROM TRANSACTIONS GROUP BY TOYID)

'**********************************************************************************
************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
###################################################################################
################################################
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'

SELECT * FROM transaction;


SELECT * FROM creditcard;
SELECT * FROM customer;

A.
SELECT CUSTOMERID,CUSTNAME,AMOUNTSPENT FROM CUSTOMER
WHERE LOWER(CUSTNAME) LIKE '%i%'
AND DOB BETWEEN ('01-Nov-1982') AND ('31-DEC-1988')

B.
SELECT CUSTNAME||'-'||CUSTOMERID CUSTDETAILS,CONTACTNO,GENDER
FROM CUSTOMER WHERE CUSTOMERID NOT IN (SELECT CUSTOMERID FROM CREDITCARD) AND
GENDER='M'

C.
SELECT CUSTOMERID,SUM(CREDITLIMIT) "TOTAL CREDIT LIMIT" FROM CREDITCARD GROUP BY
CUSTOMERID
HAVING SUM(CREDITLIMIT)>(SELECT AVG(CREDITLIMIT) FROM CREDITCARD)

D.
SELECT C.CUSTOMERID,T.TRANSACTIONID FROM CREDITCARD C
INNER JOIN TRANSACTION T ON C.CARDNO=T.CARDNO
INNER JOIN TRANSACTION T1 ON T.TAMOUNT=T1.TAMOUNT
WHERE T.TRANSACTIONID<>T1.TRANSACTIONID

E.
SELECT C.CUSTOMERID,NVL(TO_CHAR(CR.CARDNO),'NA')
CARDOWNED,NVL(TO_CHAR(T.TAMOUNT),'NT') AMOUNT
FROM CUSTOMER C LEFT JOIN CREDITCARD CR ON C.CUSTOMERID=CR.CUSTOMERID
LEFT JOIN TRANSACTION T ON CR.CARDNO=T.CARDNO

F.
SELECT CUSTOMERID,CUSTNAME FROM CUSTOMER WHERE CUSTOMERID IN
(SELECT CUSTOMERID FROM CREDITCARD GROUP BY CUSTOMERID HAVING COUNT(CARDTYPE)=3)

G.
SELECT C.CUSTOMERID,C.CUSTNAME FROM CUSTOMER C
INNER JOIN CREDITCARD CR ON C.CUSTOMERID=CR.CUSTOMERID
WHERE CR.CARDTYPE IN
(SELECT CARDTYPE FROM CREDITCARD WHERE CARDNO IN
(SELECT CARDNO FROM TRANSACTION GROUP BY CARDNO HAVING COUNT(CARDNO) IN
(SELECT MAX(COUNT(CARDNO)) FROM TRANSACTION GROUP BY CARDNO)))

H.
SELECT CUSTOMERID FROM CUSTOMER
WHERE CUSTOMERID IN
(SELECT CUSTOMERID FROM CREDITCARD WHERE CARDNO IN
(SELECT CARDNO FROM TRANSACTION) AND CARDTYPE='Platinum') AND GENDER='M'

SELECT C.CUSTOMERID FROM CUSTOMER C


WHERE C.CUSTOMERID IN
(SELECT CR.CUSTOMERID FROM CREDITCARD CR WHERE C.CUSTOMERID=CR.CUSTOMERID AND
CARDNO IN
(SELECT CARDNO FROM TRANSACTION T WHERE CR.CARDNO=T.CARDNO) AND
CARDTYPE='Platinum') AND GENDER='M'

You might also like