4
1) Emp table: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ------ ------- ------- ----- --------- ------- ----- -------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 1. Display those emp who are working as manager 2. Count the no.of emp who are working as manager(using set op). 3. Select all the information from the table emp? 4. Display ename,job and hiredate from emp table? 5. Select all employee who have salary between 1000 and 2000? 6. List the details of the employees in department 10 and 20 in alphabetical in order of names? 7. Display all employees whose names start with letter 'S'? 8. Display all employees whose name ends with letter 'T'? 9. List the details of all employees who have a manager? 10. List all employees who have name exactly four char in length? 11. List all the employees who have no managers? 12. Find employees whose salary is not between a range 1000 and 2000? 13. Find those employees whose job does not start with M? 14. To find clerks who earn between 2000 and 4000? 15. Display all different job types? 16. Find the minimum salary of all employees? 17. List the minimum and maximum salary for each job type? 18. Find the minimum,maximum and Average salaries of all employees? 19. Find out the different of highest and lowest salaries?

DBMS [Database Management Systems] Example Queries

  • Upload
    micmech

  • View
    1.352

  • Download
    1

Embed Size (px)

Citation preview

Page 1: DBMS [Database Management Systems] Example Queries

1) Emp table:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

------ ------- ------- ----- --------- ------- ----- --------

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5000 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1300 10

1. Display those emp who are working as manager

2. Count the no.of emp who are working as manager(using set op).

3. Select all the information from the table emp?

4. Display ename,job and hiredate from emp table?

5. Select all employee who have salary between 1000 and 2000?

6. List the details of the employees in department 10 and 20 in

alphabetical in order of names?

7. Display all employees whose names start with letter 'S'?

8. Display all employees whose name ends with letter 'T'?

9. List the details of all employees who have a manager?

10. List all employees who have name exactly four char in length?

11. List all the employees who have no managers?

12. Find employees whose salary is not between a range 1000 and 2000?

13. Find those employees whose job does not start with M?

14. To find clerks who earn between 2000 and 4000?

15. Display all different job types?

16. Find the minimum salary of all employees?

17. List the minimum and maximum salary for each job type?

18. Find the minimum,maximum and Average salaries of all employees?

19. Find out the different of highest and lowest salaries?

20. Write the query who are getting 1000,2000,3000.

21. Write a query to list all the employees whose names are having ‘O’ as

second character

22. Write a query to list all the employees whose names are having ‘R’ as

last character.

23. Write a query to list all the employees whose names are having just 5

character as length.

Page 2: DBMS [Database Management Systems] Example Queries

24. Write a query to list all the employees whose names are starting with

‘R’ and ending with ‘O’.

25. Find all departments which have more than three employees.

26. Display those employees whose name contains ‘A’.

2) Write the SQL expressions for the following data base.

Sailor_schema(sailor_id, sailor_name,rating,age)

Reserves(sailor_id,Boat_id,day)

Boat_schema(Boat_id,Boat_name,color)

i)Find the age of the youngest sailor for each rating level.

ii)Find the age of the youngest sailor who is eligible to vote for each

rating level with atleast two such sailors.

iii)Find the number of reservations for each red boat.

iV) Find the average age of sailor for each rating level that atleast two

sailors.

3)Consider the following schema.

The primary keys are underlined.

Sailors(Sailor_id,sailor_name,Sailor_rating,Sailor_age)

Boats(Boat_id,Boat_name,Boat_color)

Reserves(Sailor_id,Boat_id,day)

Write the queries in SQL for the following.

1) Find the names of sailors who have reserved at least one boat.

2) Find the ages of sailors whose names begin and end with C and has atleast

three characters.

3) Find the names of sailors who have reserved a blue or a yellow boat.

4) Find the names of sailors who have reserved a blue and a yellow boat.

5) Find the names of all sailors who have reserved blue boats but not yellow

boats.

6) Find the names of sailors who have reserved boat number 120

7) Find the name of sailors who have reserved a green boat

8) Find the names of sailors who have not reserved a green boat

9) Find the names of sailors with highest rating.

4) Consider the following schema for the company data base. The

primary keys are underlined.

Employee(SSN, fname, lname, birthdate, address, salary, dnumber)

Department(Dnumber, Dname, Dlocation)

Perform the following operations using SQl. Assume the data.

i) Insert a record into employee table.

ii) Delete an employee with SSN equal to 10.

iii) Update the Dnumber of the employee tuple having salary greater than

Rs. 10,000.

iv) Retrieve the name and address of all employees who work for the “XYZ”

department.

Page 3: DBMS [Database Management Systems] Example Queries

5)Assume there are two relations,

STUDENT(stnum,stname,totalmarks,semester)

Hostel(stnum,roomnum)

Represent the following queries in SQL.

i) Obtain the room number allotted to Sai

ii) Obtain name of the student staying in room number 19

iii) Get the name and marks of student with student number 1939

6) For the following relational database write the expressions in SQL.

Branch Schema (branch name, Branchcity, Assets)

Customer schema(customername, customerstreet, customercity)

Loan schema(Branchname, loan number, Amount)

Borrower schema(customername, loan number)

Account schema (Branchname, Account number, balance)

Depositor schema(customername, Account number)

i) Find the names of all branches in Loan Schema?

ii) Find all customers having loan, account or both at bank?

iii) Display customernames in alphabetical order who have a loan at the Perry

ridge branch?

iv) Find set of all customers who have an account at the bank?

V) find the names of all customers whos street address include substring

’Main’ [2]

Vi) Find average balance for each customer who lives in Harrison and at least

three accounts? [3]

Vii) Find all customer who have a loan at bank whose names are neither

?smith? non ?jones’?