Exercises-Sql1 1
Exercises-Sql1 1
Exercises-Sql1 1
ORACLE ASSIGNMENT 1
Solve the following queries which are based on the EMP and DEPT tables.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
Examples:
1
2.
3.
4.
order.
5.
Examples:
1.
2.
3.
4.
5.
6.
7.
8.
SELECT Statement
WHERE clause
Examples IN Operator
2
1.
7788.
2.
3.
List the name of the employee whose employee numbers are 7369,75211 78391- 7934,
Same query using IN operator.
List the employees not belonging to department 30 and 10.
Examples:
1.
2.
3.
BETWEEN Operator
List the employee name and salary, whose salary is between 1000 and 2000.
Same query using BETWEEN operator.
List employee names, who have joined before 30 June 81 and after December 81.
List the employee names, who are not eligible for commission.
List the name of the employee and job of the employee who does not report to anybody.
List the employees not assigned to any department.
List the details of employees, whose salary is grater than 2000 and commission is
3
4.
List the total salary, average salary, minimum Salary and the average of employees job
wise.
5.
List the total salary of employees job wise for department 20 only.
6.
List the total salary, average salary of employees job wise of department number 20
and display only those rows having average salary greater than 1000.
MULTIPLE TABLES QUERYING
Examples
1.
List employee number, name, department and the department name.
2.
List employee name, his department name and the department location.
3.
List employee name, department name for all the clerks in the company
4
List employees working in each department. List the department information
even if no employee belongs to that department.
5.
list employee number ,name,job,his managers name, managers job.
6.
Display different designations in dep. 20 and 30
7.
List the jobs common to department 20 and 30
8.
List the jobs unique to department 20.
9.
list the examples belonging to the department of 'MILLER'.
10.
List the employees who have the same job as SCOT'.
11.
List the details of employees in department 10 who have the same job as in
department 30.
12.
List all the departments that have employees who exist in them.
13.
List the employee details if and only if more than 10 employees are present in
department10.
14.
List the employee names whose salary is greater than the lowest salary of an
employee belonging to department 20.
15.
List the employee names whose salary is greater than the highest salary of an
employee belonging to department 20.
16.
List the names of the employees drawing the highest salary.
17
List the names of employees who got an increment
18
List the employees whose salary is second highest in the company.
19
List the details about employees who have maximum number of people
reporting to them.
20
List the employees who earn more than the average salary in their own
department.
21.
List the employee name, length of his name, his managers name whose name
length is greater than their managers name length.
22.
List employee and his managers details whose salary is greater than their
managers salary.
23.
List employees whose salary is highest in their department.
ORACLE ASSIGNMENT -2
Solve the following queries which are based on the EMP and DEPT tables.
1. List the names and hiredate of the employees in deptno 20, display the hiredate
formatted as 21/03/87.
4
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
How many months did the president work for the company ? Round to the
nearest whole number of months.
Find the day of the week on which SMITH joined.
Find out time of the day ( in hh24,mi,ssss ) on which FORD joined.
Find out the day of the month on, which JAMES joined.
Find out the quarter of the year the employees joined.
List the names, department of all employees whose hiredate anniversary does
not exist in the first quarter of the year.
Write a query to count the number of people in deptno 30 who receive a salary
and the number of people who receive a commission.
Compute the average, minimum and maximum salaries of those groups of
employees having the job of CLERK or MANAGER.
List the department number and the maximum salary earned in deptno = 20.
Give a hike of 10% to each employee
Select all employees whose names fall between 'A' and 'G' alphabetical range.
Find out maximum salaries department wise excluding those who are less than
3000
Give commissions equal to 1 % of their salaries to employees having
commission as NULL
List employees under their own department name like,
Department 10
Scott
Department 20
Department 30
John
Robert
Assume there are 3 departments only.
20. List employee name, sal and his income group as 'LOW' or 'HIGH' depending on the
21. salary amount. ( If the salary is less than 5000 then he is in 'LOW income group or
else in 'HIGH' income group.
Ename
Salary LOW HIGH
SCOTT
2000 LOW
JOHN
6000
HIGH
22. List ename, salary for all employees showing salary in bar chart form.
Ename Salary Graph
Scott 2000 ****
John 6000 ************
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
5
36.
37.
38.
39.
40.
Assignment 3
SQL and Queries
Solve the following queries which are based on the SALESPEOPLE, CUSTOMERS
and ORDERS tables.(I will send the table creation scripts and data inserting scripts in another
mail)
Gain practice with the tables and questions below (Dates are American)
SOLVE THE QUERIES:
1. List all the columns of the Salespeople table
2. List all customers with a rating of 100.
3. Find all records in the Customer table with NULL value in the city column
4. Find the largest order taken by each salesperson on each date.
5. Arrange the Order table by descending customer number
6. Find which salespeople currently have orders in the order table
7. List names of all customers matched with the salespeople serving them
8. Find the names and numbers of all salespeople who had more than one customer.
9. Count the orders of each of the salespeople and output the results in descending order.
10. List the customer table if and only if one or more of the customers in the customer table
are located in SanJose
11. Match salespeople to customers according to what city they lived in.
12. Find the largest order taken by each salesperson.
13. Find all the customers in SanJose who have a rating above 200.
14. List the names and commissions of all salespeople in London.
15. List all the orders of Salesperson Motika from the orders table.
16. Find all customers with orders on October 3.
17. Give the sums of the amounts from the Orders table grouped by date
18. eliminating all those dates where the SUM was not at least 2000.00 above the maximum.
Amount.
19. Select all orders that had amounts that were greater than at least one cf the orders from
October
20. Write a query that uses the EXISTS operator to extract all salespeople have customers
with a rating of 300.00
21. Find all pairs of customers having the same rating.
22. Find all customers whose cnum is 1000 above the snum of Serres.
23. Give the salespeople's commissions as percentages instead of decimal numbers.
24. 23.Find the largest order taken by each salesperson on each date, eliminating those
Maximum orders which are less than $3000.00
25. List all the largest orders for October 3, for each salesperson.
26. Find all customers located in cities where Serre-S ( snum 1002 ) has customers.
27. Select all customers with a rating above 200.00
28. Count the number of salespeople currently listing orders in the orders table.
29. Write a query that produces all customers serviced by salespeople with a commission
above 12% . Output the customer's name, salesperson's name and the salesperson's
rate of commission.
30. Find salespeople who have multiple customers.
31. Find salespeople with customers located in their cities.
32. Find all salespeople whose name starts with 'P'and fourth character is 'I'.
33. Write a query that uses a subquery, to obtain all orders for the custormer named
scenerios. Assume you do not know the customers name
6
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
7
71. Write a SELECT command that produces the order number, amount, and the date for all
rows in the order table.
72. Count the number of non NULL rating fields in the Customers table (including repeats)
73. Write a query that gives the names of both the salesperson and the customer for each
order after the order number.
74. List the commissions of all salespeople servicing customers in London.
75. Write a query using ANY or ALL that will find all salespeople who have no customers
located in their city.
76. Write a query using the EXISTS operator that selects all salespeople with customers
located in their cities who are not assigned to them.
77. Write a query that selects all customers serviced by Peel or Motika.
78. Hint : The snum field relates the 2 tables to one another
79. Count the number of salespeople registering orders for each day. ( If a salesperson has
more than one order on a given day, he or she should be counted only once )
80. Find all orders attributed to salespeople in London.
81. Find all orders by customers not located in the same cities as their salespeople.
82. Find all salespeople who have customers with more than one current order.
83. Write a query that extracts from the customers table every customer assigned to a
salesperson who currently has at least one another customer ( besides the customer
being selected ) with orders in the Orders Table.
84. Write a query that selects all customers whose names begin with 'CONTROL'.
85. Write a query on the customers table that will find the highest rating in each city. Put
the output in this form : for the city ( city ) , the highest rating is ( rating)
86. Write a query that will produce the snum values of all salespeople with orders currently
in the Orders table ( without any repeats )
87. Write a query that lists customers in a descending order of rating. Output the rating
field first, followed by the customer's names and numbers.
88. Find the average commission for salespeople in London.
89. Find all orders credited to the same salesperson who services Hoffman. ( cnum 2001)
90. Find all salespeople whose commission is in between 0.10 and 0.12 ( both incisive ).
91. Write a query that will give you the names and cities of all salespeople in London with a
commission above 0.10
92. What will be the output from the following query ?
93. SELECT * FROM ORDERS
94. WHERE (amt < 1000 OR NOT (odate = 10 / 03 /1990 AND cnum > 2003));
95. Write a query that selects each customer's smallest order.
96. Write a query that selects the first customer in alphabetical order whose name begin
with 'GENERATOR'.
97. Write a query that counts the number of different nonNULL city values in the customers
table.
98. Find the average amount from the Orders table.
99. What would be the output of the following query ?
SELECT * FROM ORDERS WHERE ((odate=10/03/19900 Rsnum > 1006 ) AND amt >= 1500
);
93.
Find all customers who are not located in San Jose and whose rating is above 200.
94.
Give a simpler way to write this query.
SELECT snum, sname, city, comm FROM salespeople WHERE ( comm > + 0.12 OR
comm < 0.14);
95.
Evaluate the following query.
SELECT * FROM orders
WHERE NOT ( (odate = 10/03/1990 AND snum > 1002 ) OR amt > 2000.00
96. Which salespersons attend to customers not in the city they have been assigned to ?
97. Which salespeople get commission greater than 0.11 are serving customers rated less
than 250 ?
98. Which salespeople have been assigned to the same city but get different commission
percentages?
99. Which salesperson has earned the most by way of commission ?
8
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
Does the customer who has placed the maximum number of orders have the
maximum rating?
The customer who has spent largest amount of money been given the largest
rating ?
List all customers in descending order of customer rating.
On which days has Hoffman placed orders ?
Do all the salespersons have different commissions ?
Which salesmen have no orders between-10/03/90 and 10/05/90 ?
How many salesersons have succeeded in getting orders ?
How many customers have placed orders ?
On which date has each salesman booked an order of maximum value ?
Who is the most successful salesperson ?
Who is the worst customer with respect to the company ?
Are all customers not having placed orders greater than 200 totally been serviced by
salesperson Peel or Serres?
Which customers have the same rating?
Find all orders greater than the average for October 4th?
Give me all customers with ratings above San Joses average who donot smoke?
Which customers have above average orders?
Select the total amount in orders for each salesperson for whom the total is greater
than the amount of the largest order in the table.
Give the names and numbers of all salespersons who have more than one
customer?.
Select all salespeople by name and number who have customers in their city whom
they donot service.
Which customers rating should be lowered.?
Is there a case for assigning a salesperson to Berlin?
Does the total amount in orders by customer in Rome and London exceed the
commission paid to salesperson in London and NewYork. By more than 5 times.?
Which is the Date, order number ,Amt and city for each salesperson ( by Name) for
the maximum order has obtained?
Which salesperson should be fired?
What is the total income for company?