You are on page 1of 7

We will use the below database definition to generate the queries:

CLIENT (clientno#,name, client_referred_by#)


ORDER (orderno#, clientno#, order_date, empid#)
ORDER_LINE (orderno#, order line number#, item_number#,
no_of_items, item_ cost, shipping_date)
ITEM (item_number#, item_type, cost)
EMPLOYEE (empid#, emp_type#, deptno, salary, firstname,
lastname)

1. Display all the rows and columns in the CLIENT table. Sort by
client name in reverse alphabetical order.
SELECT * FROM CLIENT ORDER BY NAME DESC;
2. Display the item number and total cost for each order line (total
cost = no of items X item cost). Name the calculated column
TOTAL COST.
SELECT A.ITEM _NUMBER ITEM NUMBER, B.
NO_OF_ITEMS *B. ITEM_COST TOTAL COST, C. NAME
FROM ITEM A, ORDER LINE B, CLIENT C, ORDER D
WHERE A.ITEM_NUMBER =B.ITEM_NUMBER
AND B.ORDERNO = D.ORDERNO
AND C.CLIENTNO=D.CLIENTNO;
3. Display all the client numbers in the ORDER table. Remove
duplicates.
SELECT CLIENTNO FROM ORDER GROUP BY CLIENTNO ;
4. Display the order number and client number from the ORDER
table. Output the result in the format. Client <clientno> ordered
<orderno>
SELECT CLIENT ++ CLIENTNO + ORDERED+ +
ORDERNO FROM ORDER ;
5. Display full details from the ORDER_LINE table where the item
number is (first condition) between 1 and 200 (no > or <
operators) OR the item number is greater than 1000 AND (second
condition) the item cost is not in the list 1000, 2000, 3000 OR the
order number is not equal to 1000.
SELECT * FROM ORDER_LINE
WHERE (ITEM_NUMBER BETWEEN 1 AND 200 OR
ITEM_NUMBER >1000 )
AND (ITEM_COST <> 1000,2000,3000 OR ORDERNO <>
1000);
6. Display the client name and order date for all orders using the
traditional method.
SELECT A.NAME CLIENT NAME, B.ORDER_DATE ORDER
DATE FROM CLIENT A, ORDER B
WHERE A.CLIENTNO=B.CLIENTNO ;
7. Repeat query (6) but also display all clients who have never
ordered anything.
SELECT A.NAME CLIENT NAME, B.ORDER_DATE ORDER
DATE FROM CLIENT A, ORDER B
WHERE A.CLIENTNO(+) = B.CLIENTNO ;
8. Display the client name and order date for all orders using the
natural join keywords.
SELECT A.NAME, B.ORDER_DATE FROM CLIENT A, ORDER B
JOIN CLIENT A ON A.CLIENTNO=B.CLIENTNO;

9. Display the client name and order date for all orders using the
JOIN . . . USING method.
SELECT CLIENT.NAME, ORDER1.DATE
FROM TEST.CLIENT A, TEST.ORDER1 B
WHERE A.CLIENTNO = B.CLIENTNO;
10. Display the client number, order date and shipping date for all
orders where the shipping date is between three and six months
after the order date.
SELECT CLIENTNO, ORDER_DATE, SHIPPING_DATE FROM
ORDER A, ORDER_LINE B
WHERE SHIPPING_DATE > ORDER_DATE+90
AND SHIPPING_DATE <ORDER_DATE+180;
11. Display the client number and name and the client number and
name of the person who referred that client.
SELECT CLIENTNO, NAME, B.CLIENT_NAME
REFERREDBYCLIENT_NAME, B.CLIENTNO AS
REFERREDBY_CLIENTNO
FROM CLIENT A, CLIENT B
WHERE A.CLIENTNO=B. CLIENT_REFERRED_BY;
12. Display the client name in upper case only and in lower case
only.
SELECT UPPER(NAME), LOWER(NAME) FROM CLIENT
13. Display the second to fifth characters in each client name.
SELECT SUBSTR(NAME,2,5) FROM CLIENT ;
14. Display the item_cost and then round it to the nearest
hundred, ten, unit, tenth and hundredth.
SELECT COST, COST,
ROUND(COST, -2) HUNDRED_ROUND,
ROUND(COST, -1) TEN_ROUND,
ROUND(COST, 0) UNIT_ROUND,
ROUND(COST, 1) TENTH_ROUND,
ROUND(COST, 2) HUNDREDTH_ROUND
FROM ITEM;
15. Display the item_cost and then truncate it to the nearest
hundred, ten, unit, tenth and hundredth.
SELECT COST, COST,
TRUNC(COST, -2) HUNDRED_ROUND,
TRUNC (COST, -1) TEN_ROUND,
TRUNC (COST, 0) UNIT_ROUND,
TRUNC (COST, 1) TENTH_ROUND,
TRUNC (COST, 2) HUNDREDTH_ROUND
FROM ITEM;
16. Display the order number, order line number and the shipping
date. If the shipping date is null, display the string <not shipped
yet>.
SELECT ORDERNO,B. ORDER_LINE_NUMBER,
DECODE(B.SHIPPING_DATE,NULL,NOT YET SHIPPED,
SHIPPING_DATE) FROM ORDER A, ORDER_LINE B
WHERE A.ORDERNO=B.ORDERNO;
17. Display the order number and average item cost for each
order.
SELECT ORDER_NO,AVG(ITEM_COST) FROM ORDER_LINE
GROUP BY ORDER_NO;
18. Display the clientno and total value for all orders placed by that
client. Output the result in the following format: Client <clientno>
has placed orders to the value of <total value>
SELECT CLIENT ||NVL(TOCHAR(CLIENT_NO,UNKNOWN)
|| HAS
PLACED ORDERS TO THE VALUE OF ||
NVL(SUM(ORDERS),0) AS
ORDER_SUMMARY
FROM ORDER_LINE
GROUP BY CLIENT_NO;
19. Display all clients whose name begins with the letter J or
contains the letter M anywhere or contains E as the third letter.
SELECT CLIENT_NAME FROM CLIENT WHERE
CLIENT_NAME LIKE J%
OR CLIENT_NAME LIKE %M% OR CLIENT_NAME LIKE
___E%;
20. Using a set operator, display the client number of all clients
who have never placed an order.
SELECT CLIENTNO FROM CLIENT
MINUS
SELECT CLIENTNO FROM ORDER;

21. Using a set operator, display the client number of all clients
who have ever placed an order and whose name does not contain
the string Sm.
SELECT CLIENTNO FROM CLIENT WHERE NAME NOT LIKE
%SM%
UNION
SELECT CLIENTNO FROM ORDER;
22. Display the order number, number of lines in the order, total
number of items and total value for all orders that have a total
value greater than $100
SELECT ORDERNO, ORDER_LINE_NUMBER, NO_OF_ITEMS,
SUM(ITEM_COST)
FROM ORDER_LINE
GROUP BY ORDERNO, ORDER_LINE_NUMBER,
NO_OF_ITEMS;

23. Display the client name for all clients who have placed an
order where any order line has more than 3 items. Do not use a
table join anywhere in your query.
SELECT NAME,COUNT(ITEM_NUMBER) FROM CLIENT
WHERE ITEM_NUMBER IN
(SELECT ITEM_NUMBER FROM ITEM )
HAVING COUNT(ITEM_NUMBER)>3 ;
24. Display the order number for all orders whose average item
cost is greater than the overall average item cost across all orders.
SELECT ORDER_NO,AVG(ITEM_COST) FROM ORDER_LINE
GROUP BY ORDER_NO
HAVING AVG(ITEM_COST) > (SELECT
AVG(AVG(ITEM_COST)) FROM
ORDER_LINE GROUP BY ORDER_NO);
25. Display the client number and the value of the highest value
order placed by that client. (High challenge questionwell above
exam standard).
SELECT A.CLIENTNO,(B.NO_OF_ITEMS * ITEM_COST )
HIGH_VALUE_ORDER FROM ORDER A, ORDER_LINE B
WHERE
A.ORDERNO =B.ORDERNO AND
HIGH_VALUE_ORDER=(SELECT MAX(NO_OF_ITEMS *
ITEM_COST) FROM ORDER_LINE);
26. Display the earliest shipping date in the format:
DD/MON/YYYY
SELECT
CONVERT(CHAR(12),GETDATE(SHIPPING_DATE),106) FROM
ORDER_LINE ORDER BY SHIPPING_DATE ;
27. Display the order number and the number of months since the
order was shipped for all orders that have been shipped in the last
year (365 days). (Hint: Unshipped orders will have a null value).
SELECT ORDERNO, MONTHS_BETWEEN(SYSDATE-
ORDER_DATE) FROM ORDER A, ORDER_LINE B
WHERE A.ORDERNO=B.ORDERNO AND
B.ORDER_SHIPPING_DATE IS NOT NULL
AND TRUNC(SYSDATE)
TO_DATE(ORDER_SHIPPING_DATE, DD-MM-YYYY)>=365;

28. Display the item_number, the total number of times that item
has been sold and the average cost for that item. Restrict that
output to items that have been included in more than 500 orders.
(Hint: Assume an item will appear in only one order line of an
order).

SELECT B.ITEMNO, SUM(B.NO_OF_ITEMS), AVG(C.COST)


FROM ORDER_LINE B, ITEM C
WHERE B.ITEM_NUMBER=C.ITEM_NUMBER
AND SUM(B.NO_OF_ITEMS) >500;

29. Display the surname for all employees who earn less the
average salary of all those employees in the department with the
lowest average salary.

SELECT EMPLOYEE_ID,LAST_NAME FROM EMPLOYEES


WHERE SALARY<
(SELECT AVG(SALARY) FROM EMPLOYEES)

30. Display the client number and the total number of orders the
client has placed for all clients who have placed at least one order
in 2011. (Hint: Use exists).

SELECT CLIENTNO, COUNT(ORDERNO)


FROM ORDER
WHERE TO_DATE(ORDER_DATE,YYYY) IN 2011;

You might also like