SQL Assignment
SQL Assignment
Sql-Assignment
Staff table:
Column name Datatype Description
Staffid Number Primary key
Sname Varchar
Design Varchar
Mgrid Number References
Doj Date
Sal Number
Comm. Number
Libno Number References library
Library table:
Columnname Datatype Description
Libno Number Primary key
Libname Varchar
City varchar
Prepared by Tulsi
10g sql
Member table
Column name Datatype Description
Mem_no Varchar2(20) Primary key
Mem_name Varchar2(20) Not null
Mem_type Varchar2(20) (M,Y,L)
No_of_books Number(2)
Total_fine Number(4)
Expiry date date
Prepared by Tulsi
10g sql
Transaction table
Prepared by Tulsi
10g sql
salgrade
GRADE NUMBER
LOSAL NUMBER
HISAL NUMBER
Data
Prepared by Tulsi
10g sql
Queries
Pattern Matching
1. List the staff whose name start with an ‘S’
2. list the staff details whose name start with an ‘S’ or end with ‘S’
3. list the names of staff whose names have exactly 5 characters
4. List the names having ‘I’ as the second character
Expressions
1. List the name,sal and Pf amount to all the staff (pf is 10% of sal)
2. list the names of staff who are more than 2yrs old in the organization
Order By
1. list the staffid, name and sal in ascending order of sal
2. list the staff details in ascending order of doj
3. list the staff name,sal,design and libno in descending order of libno and sal
4. list the staffname,sal,PF,HRA,DA and gross, order by the result in
ascending order of gross(HRA 50% and DA 30% of sal).
Functions
Prepared by Tulsi
10g sql
1. for each staff, display the staffid,sname,sal and sal increased by 15% and
expressed in whole number.
2. write the query that displays the staff name with the first letter capitalized and
all others in lowercase and the length of the name for those staff who start
with J,A or M sort by sname
3. for each employee, display the sname and calculate the number of months
between today and the date the employee was hired.
4. create a query to display the sname and sal for all staff,format sal to be 15
char long,left padded with $
5. display the sname,doj and day of the week on which they started for all the
employees
Aggregate functions
1. list the number of staff working with the company
2. list the number of designation available
3. list the max sal of staff working as librarian
4. list the avg sal and number of staff working in the lib 20
Date functions
1. List staff having experience greater than 200 months.
2. Declare certain percentage of bonus based designation.
3. Display each staffname, doj, and salary review date, which is the first Monday
after six months of service. Label the column REVIEW. Format the dates to
appear in the format similar to “Monday, the Thirty-First of July, 2000.”
4. Print list of staff displaying “Good salary” if sal > 3000, “Average salary” if
sal=3000 and “Poor salary” if sal < 3000.
5. Write a query to calculate the length of any staff has been with the company.
Display in number of years and months. Staff hired on or before 15th are paid on
last Friday of same month. Those hired after 15th are paid on last Friday of next
month. Display first pay date of all the employees.
6. List staff hired on Monday.
7. Create a query that displays the staff names and commission amounts. If an staff
does not earn commission, put “No Commission.” Label the column COMM.
Group By
1. list the libno and number of staff in each lib
2. list the libno and the total sal payable in each library
3. list the design and the number of staff in each design. Display in the
ascending order of number of staff
4. list the total sal,max and min sal and the average sal of staff job wise
5. list the avg sal for each job excluding managers
Prepared by Tulsi
10g sql
6. list the total sal,max,min and avg sal of staff jobwise for libno 20 only
Having Clause
1. list the avg sal for all the lib employing more than five people
2. list the jobs of all staff where max sal is greater than or equal to 5000
3. list the tot sal,max and min sal and avg sal of staff job wise for libno 20 only
and display only those rows having sal greater than 2000 and order the result
by the sum of sal
Prepared by Tulsi
10g sql
Joins
1. list the member details and book details for those members who have paid fine
amount more then 200
2. List libno, lname along with total salary in each library
3. List employees located in delhi
4. List staff and lib details for libno 30 and 40.
5. List all staff who earn less than their managers. List their managers details also.
6. Find all staff who joined the company before their manager
7. List details from staff, library and salgrade table excluding clerks and employees with
grade 1. Order on their names.
8. List staff with grade 3 or 4
9. List sname, design, sal, grade, lname with annual renumeration greater than 36000 or
any clerks.
10. List all staff who earn less than their managers. List their managers details also.
11. Find the job that was filled in first half of 2002 and the same job that was filled
during first half of 2003
Nested Queries
1. list the staff belonging to the lib of MILLER
2. list the names of the staff drawing the highest sal
3. list the staff details whose sal is greater than avg sal of staff whose doj is
before “01-04-01”
4. list the job with the highest avg sal
5. list the lib details whose managers id is 17698
6. list the names of the staff who earn lowest sal in each lib
7. List staff working under KING
8. List library with maximum average salary.
Correlated Subqueries
1.list the staff details who earn sal more than the avg sal earned by his library
2. list the details of staff who earn highest sal for their design.
3 Find staff who earn minimum salary for each job type excluding clerks.
sort on their salary.
4 Find most recently hired staffin each lib Order by doj
5. List library for which no staff exists.
Prepared by Tulsi
10g sql
Views
1. Create a view which returns details of member and books which has a fine more than 100
2. Create view which display all the staff information along with manager information
3. Create view which display all staff information who get sal more than avg sal of their
job type.
Exercise
1. Calculate the fine amount of every member
2. List all the member details who have borrowed book on Wednesday
3. List all member details who had borrowed the same book as that of the member
who had paid fine more than 50/-
4. List all the member details who had not borrowed any book
5. List all the book details which had not been borrowed by any member
6. List all the member details who had paid fine more than 100/-
7. List all the member details who had never paid fine
8. List all member and book details who had borrowed the same book on the same
day
9. List all the book deatails with the book code starting with any char, followed by 3
numbers
10. List all the books published by the same author
11. List all the book details of the same price
12. List the no of books published by the same author
13. List all clerks earning less than 1000 and all managers
14. Display the o/p ‘ALLEN BORROWED ORACLE BOOK’ USING CONCAT
operator
15. Print memberno,bookno displaying “somefine” if fine<=50 and “nofine” if fine=0
and “fine” if fine>50
16. Display the members and the number of books they have currently borrowed
17. List average sal of each job type excluding managers
18. List the number of managers
19. List all members whose expiry date is the last day of the month
20. List how many staff are under each manager
21. List the number of people for the same job
22. List staff working under head
23. Find staff who earn the highest sal in each job type
24. List the member and book information for all the books which are currently
borrowed
25. List all the details of members and books that the specified member has borrowed
the specified book
Prepared by Tulsi