You are on page 1of 8

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.

List all the information about employees in the EMP table.


List all the information about departments in the DEPT table.
List the employee number, name, job title and hire date of employees in department 10.
Select name and salary of employees who are clerks.
List the department number and name for all departments with department numbers
greater than or equal to 20.
List the name of the employees having salary less than 2500.
Select name, salary and commission of employees whose commission is greater than their
salary.
List the employee number and name of the President.
List the employees who do not get commission.
List all the employees in deptno 10 other than KING.
Display names of the employees whose job is either analyst or clerk.
Display different kind of jobs available.
List names of all employees whose names are 4 letters long.
List names of all employees whose names end with letter 'R'.
List names of all employees whose names start with 'B' or 'M'.
If a new person HENRY joins the Organisation in place of TURNER on 8th December 1985
with empno 7333, make necessary changes in the EMP table.
Retrieve the names and job of the employees working in the department number 20
Display the result with 'Emloyee - Job' as column heading and arranging the
columns with '-' in between like ' Smith - Clerk'.
Show what length names appear in the EMP table. Eliminate the duplicate lengths
from
the rows returned.
Determine the average earning of an employee working in department 30.
Determine the new comer to the organisation.
Find out the total salary in each department. Display the department number and
the total salary.

Examples:
1
2.
3.
4.
order.
5.

List the information of all employees.


List the information of all departments.
List the employee names along with their salaries.
List the department numbers, employee numbers and the managers numbers in that
List department name and their location from department table.

Examples:
1.
2.
3.
4.
5.
6.
7.
8.

SELECT Statement

WHERE clause

List the employees belonging to the department 20.


List the name and salary of the employees, whose salaries more than 1000.
List the employee number and name of the managers.
List the names of clerks working in the department 20.
List the employee name and salary for employees whose salary is between 1000 and.
2000.
List the names of analysts and salesmen.
List the employees who have joined before the end of September 81.
List the names of employees who are not managers.

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.

Examples : IS NULL Operator


1.
2.
3.
4.
NULL.

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

Examples : LIKE Operator


1.
2.
3.
4.

List the employees whose name start with an 'S'.


List the employee names ending with an ' S.
List the employee names having third character as 'R'.
List the names of employees whose names have exactly five characters.

Examples: ORDER BY clause


1
List the employee details in ascending order of salary.
2.
List the names of employees in department 30 from emp table in the descending order of
their salary.
3.
Order the results Of above query using relative positions of columns.
4.
List the employee names and hiredate in the descending order of hiredate.
Examples : Using expressions
1.
List name, salary and PF amount of all the employees. PF is calculated as 10 % of
salary. (use of the column alias)
2.
List the names of employees, who are more than 2 years old In the organization. (use"
Of SYSDATE)
Examples : Aggregate Functions
1)
2)
3)
4)
5)
6)
7)

List the number of employees wrking in the company.


List the number of jobs available in the company.
List the total salaries payable to employees.
List maximum, minimum, average, sum of salary
List the maximum Salary of employee working as a salesman.
List the average salary and number of employees working in the department 20.
List the earliest date and the latest date on which someone was hired.

Examples : GROUP BY clause


1.
2
3

List the department numbers and number of employees in each department


List the department number and total salary payable to each department
List the jobs and the number of employees in each job. The Result should be in the
descending order of the number of jobs.

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.

scale : one * for 500.


What is the length of the longest employee name and by how many characters is it
longer than its nearest one.
Find out the locations of the employees.
Who was the last employee hired in each department ?
Display name and salary of the employee who is working in CHICAGO.
How many employees work in NEWYORK
List the employees names and the cities in which they work. Sort the list by city.
Display the names of the employees who are working in Sales or Research
department.
Find out the difference between the maximum salary earned by a person in deptno
10 and minimum salary earned by a person in deptno 30.
Find out the difference between average earnings of deptno 30 and 40.
Find the people whose salary is less than the avg. salary for dept no. 20
List number, name , and job of each employee and the persons manager and
managers job

5
36.
37.
38.
39.
40.

Display information about people who have no people reporting to them


Display information about people who have maximum people reporting to them.
List the names of the people who reporting to BLAKE.
Find the employees who earn more than the avg salary in their own department.
List the locations of the departments and the employees working in them including the
departments without employees.

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.

Find the largest orders for Serres and Rifkin.


Extract the salespeople table in the following order @ snum, sname, commission. city.
Select all customers whose names fall in between 'a' and 'g' alphabetical range.
Select all the possible combinations of customers you can assign.
Select all orders that are greater than the avg of october 4.
Write a select command using correlated subquery that selects the names and numbers
at all customers with ratings equal to the maximum for their city.
Write a query that totals the orders for each day and places the results in descending
order.
Write a select command that produces the ratings followed by the name of each
customer in SanJose.
Find all orders with amounts smaller than any amount for a customer in SanJose.
Find all orders with above average amounts for their customers.
Write a query that selects the highest rating in each city.
Write a query that calculates the amount of the salesperson's commission on each order
by a customer with a rating above 100.00.
Count the customers with ratings above SanJose's average.
Write a query that produces all pairs of salespeople who are living in the same city.
Exclude combinations of salespeople with themselves as well as duplicate rows with
the order reversed.
Find all salespeople that are located in either Barcelona or London.
Find all salespeople with only one customer.
Write a query that joins the Customer table to itself to find all pairs or customers served
by a single salesperson.
Write a query that will give you all orders for more than $1000.00
Write a query that lists each order number followed by the name of the customer who
made that order.
Write 2 queries that selects all salespeople ( by name and number ) who and one a
correlated
sub query. Which solution is more elegant ?
( Hint : One way to do this is to find all customers not serviced by a given salesperson
and see if any of them are in his / her city )
Write a query that selects all the customers whose ratings are equal to or greater than
ANY ( in the SQL sense ) of Series.
Write 2 queries that will produce all orders taken on October 3 or October 4.
Write a query that produces all pairs of orders by a given customer, name that customer,
and eliminate duplicates.
Find only those customers whose ratings are higher than every customer in Rome.
Write a query on the Customers table whose output will exclude all customers with a
rating <= 100.00, unless they are located in Rome.
Find all rows from the customers table for which the salesperson number is l001
Find the total amount in orders for each salesperson ,for whom this total is greater than
the amount of the largest amount in the table
Write a query that selects all orders save those with zeroes or NULLs in the amount field
Produce all combinations of salespeople and customer names such that the former
precedes the latter alphabetically, and the latter has a rating of less than 200.
Find all sales people name and commission.
63.Write a query that produces the names and cities of all customers with the same
ratings as Hoffman..
write the query using Hoffman's cnum rather than his rating, so that it would Still be
usable if his rating is changed.
Find all sales people for whom there are customers that follow them in alphabetical
order.
Write a query that produces the names and ratings of all customers who have
average orders.
Find the SUM of all purchases from the orders table.

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?