ODBA Lab Questions

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Answer all the questions

Assignment-1:

Table Name: Employee


Employee_id First_name Last_name Salary Joining_date Department

1 John Abraham 1000000 01-JAN-13 12.00.00 AM Banking

2 Michael Clarke 800000 01-JAN-13 12.00.00 AM Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00 AM Banking

4 Tom Jose 600000 01-FEB-13 12.00.00 AM Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00 AM Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00 AM Services

7 TestName1 123 650000 01-JAN-13 12.00.00 AM Services

8 TestName2 Lname% 600000 01-FEB-13 12.00.00 AM Insurance

Table Name : Incentives


Employee_ref_id Incentive_date Incentive_amount

1 01-FEB-13 5000

2 01-FEB-13 3000

3 01-FEB-13 4000

1 01-JAN-13 4500

2 01-JAN-13 3500

1.Get all employee details from the employee table


Select * from employees;

2. Get First_Name,Last_Name from employee table


Select first_name,last_name from emplyees;

3. Get First_Name from employee table using alias name “Employee Name”
Select first_name as Employee Name from employees;

4. Get First_Name from employee table in upper case


Select upper(first_name) from employees;

5. Get First_Name from employee table in lower case


Select lower(first_name) from employees;

6. Get unique DEPARTMENT from employee table


Select substr(first_name,1,3) from employees;

8. Get position of 'o' in name 'John' from employee table


SELECT INSTR(FIRST_NAME,'o') FROM EMPLOYEES WHERE FIRST_NAME = 'John';
1
9. Get FIRST_NAME from employee table after removing white spaces from right side
Select rtrim(first_name) from employees;

10. Get FIRST_NAME from employee table after removing white spaces from left side
Select ltrim(frist_name) from employees;

11. Get length of FIRST_NAME from employee table


select length(first_name) from employees;

12. Get First_Name from employee table after replacing 'o' with '$'
select REPLACE(FIRST_NAME,'o','$') from employees;

13. Get First_Name and Last_Name as single column from employee table separated by a '_'
Select FIRST_NAME|| '_' ||LAST_NAME from EMPLOYEES;

14. Get FIRST_NAME ,Joining year,Joining Month and Joining Date from employee table
Select first_name , joining date from employees;

15. Get all employee details from the employee table order by First_Name Ascending
Select * from employees order by first_name;

16. Get all employee details from the employee table order by First_Name descending
Select * from employees order by first_name desc;

17. Get all employee details from the employee table order by First_Name Ascending and Salary
descending
select * from employees order by first_name asc,salary desc;

18. Get employee details from employee table whose employee name is “John”
Select * from employees where name = ‘john’;

19. Get employee details from employee table whose employee name are “John” and “Roy”
Select * from employees where first_name in(‘John’ . ‘Roy’);

20. Get employee details from employee table whose employee name are not “John” and “Roy”
Select * from employees where first_name not in(‘john’,’roy’);

21. Get employee details from employee table whose first name starts with 'J'
Select * fom employees where first_name like ‘J%’;

22. Get employee details from employee table whose first name contains 'o'
Select 8 from employees where first_name like’%o%’;

23. Get employee details from employee table whose first name ends with 'n'
Select * from employees where first_name like ‘%n’;

24. Get employee details from employee table whose first name ends with 'n' and name contains 4
letters
Select * from employees where first_name like ___n’;

25. Get employee details from employee table whose first name starts with 'J' and name contains 4
letters
Select * from employees where first_name like ‘J___’;

2
26. Get employee details from employee table whose Salary greater than 600000
Select * from employees where salary>6000000;

27. Get employee details from employee table whose Salary less than 800000
Select * from employees where salary<8000000;

28. Get employee details from employee table whose Salary between 500000 and 800000
Select * from employees where salary between 5000000 and 8000000;

29. Get employee details from employee table whose name is 'John' and 'Michael'
Select * from employees where first_name in(‘john’,’Michael’);

30. Get employee details from employee table whose joining year is “2013”
Select * from employees where joining_date between '13-JAN-01 12.00.00.000000 AM' and '31-
DEC-01 12.00.00.000000 AM';
Or
Select * from employees where to_char(joining_date,’dd’)=’13’;

31. Get employee details from employee table whose joining month is “January”
Select * from employees where to_char(joining_date,’mon’)=’jan’;

32. Get employee details from employee table who joined before January 1st 2013
Select * from EMPLOYEES where JOINING_DATE <to_date('01/01/2013','dd/mm/yyyy');

33. Get employee details from employee table who joined after January 31 st
Select * from EMPLOYEES where JOINING_DATE >to_date('31/01/2013','dd/mm/yyyy');

35. Get Joining Date and Time from employee table


Select joining_date from employees;

36. Get Joining Date,Time including milliseconds from employee table


Select joining_date from employees;

37. Get difference between JOINING_DATE and INCENTIVE_DATE from employee and

38. Get database date


Select sysdate from dual;

39. Get names of employees from employee table who has '%' in Last_Name. Tip : Escape character for
special characters in a query.
Select FIRST_NAME from employees where Last_Name like '%?%%'

40. Get Last Name from employee table after replacing special character with white space
Select translate(LAST_NAME,'%',' ') from employees;

41. Get department,total salary with respect to a department from employee table.
select dept,sum(salary) as total_salary from employees group by dept;

42. Get department,total salary with respect to a department from employee table order by total salary
descending
select dept,sum(salary) from employees group by dept order by total_salary desc;

3
43. Get department,no of employees in a department,total salary with respect to a department from
employee table order by total salary descending
Select dept,count(first_name),sum(salary) Total_Salary from employees group by dept order by
Total_Salary desc;

44. Get department wise average salary from employee table order by salary ascending
select dept,avg(salary) avg_salary from employees group by dept order by avg_salary desc;

45. Get department wise maximum salary from employee table order by salary ascending
Select dept,max(salary) max_salary from employees group by dept order by max_salary;

46. Get department wise minimum salary from employee table order by salary ascending
Select dept,min(salary) min_salary from employees group by dept order by min_salary;

47. Select no of employees joined with respect to year and month from employee table
select count(emp_id) from employees group by joining_date;

48. Select department,total salary with respect to a department from employee table where total salary
greater than 800000 order by Total_Salary descending
select dept,sum(salary) total_salary from employees group by dept having sum(salary)>800000
order by total_salary desc;

49. Select employee details from employee table if data exists in incentive table?
select * from EMPLOYEES where exists (select * from INCENTIVES);

50. How to fetch data that are common in two query results?
select * from EMPLOYEES where EMP_ID=3 INTERSECT select * from EMPLOYEES where EMP_ID <
4;

51. Get Employee ID's of those employees who didn't receive incentives without using sub query ?
select EMP_ID from EMPLOYEES MINUS select EMP_REF_ID from INCENTIVES;

52. Select 20 % of salary from John , 10% of Salary for Roy and for other 15 % of salary from
employee table
SELECT FIRST_NAME, CASE FIRST_NAME WHEN 'John' THEN SALARY * .2 WHEN 'Roy' THEN
SALARY * .10 ELSE SALARY * .15 END "Deduced_Amount" FROM EMPLOYEES;

53. Select Banking as 'Bank Dept', Insurance as 'Insurance Dept' and Services as 'Services Dept' from
employee table
SELECT distinct DECODE (DEPT, 'Banking', 'Bank Dept', 'Insurance', 'Insurance Dept', 'Services',
'Services Dept') FROM EMPLOYEES;

54. Delete employee data from employee table who got incentives in incentive table
delete from EMPLOYEE where EMPLOYEE_ID in (select EMPLOYEE_REF_ID from INCENTIVES)

55. Insert into employee table Last Name with " ' " (Single Quote - Special Character)
Insert into employees (LAST_NAME) values ('Test''');

56. Select Last Name from employee table which contain only numbers
Select * from EMPLOYEES where lower(LAST_NAME)=upper(LAST_NAME);

In order to achieve the desired result, we use "ASCII" property of the database. If we get results for a column
using Lower and Upper commands, ASCII of both results will be same for numbers. If there is any alphabets
in the column, results will differ.
4
57. Write a query to rank employees based on their incentives for a month
select FIRST_NAME,INCENTIVE_AMOUNT,DENSE_RANK() OVER (PARTITION BY INCENTIVE_DATE
ORDER BY INCENTIVE_AMOUNT DESC) AS Rank from EMPLOYEES a, INCENTIVES b where
a.EMP_ID=b.EMP_REF_ID;

In order to rank employees based on their rank for a month, "DENSE_RANK" keyword is used. Here
partition by keyword helps us to sort the column with which filtering is done. Rank is provided to the column
specified in the order by statement. The above query ranks employees with respect to their incentives for a
given month.

58. Update incentive table where employee name is 'John'


update INCENTIVES set INCENTIVE_AMOUNT='9000' where EMP_REF_ID=(select EMP_ID from
EMPLOYEES where FIRST_NAME='John' );

59. Select first_name, incentive amount from employee and incentives table for those employees who
have incentives
Select FIRST_NAME,INCENTIVE_AMOUNT from employees a inner join incentives B on
A.EMP_ID=B.EMP_REF_ID;

60. Select first_name, incentive amount from employee and incentives table for those employees who
have incentives and incentive amount greater than 3000
Select FIRST_NAME,INCENTIVE_AMOUNT from employees a inner join incentives B on
A.EMP_ID=B.EMP_REF_ID;

61. Select first_name, incentive amount from employee and incentives table for all employes even if
they didn't get incentives
Select FIRST_NAME,INCENTIVE_AMOUNT from employees a left join incentives B on
A.EMP_ID=B.EMP_REF_ID;

62. Select first_name, incentive amount from employee and incentives table for all employees even if
they didn't get incentives and set incentive amount as 0 for those employees who didn't get incentives.
Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0) from employees a left join incentives B on
A.EMP_ID=B.EMP_REF_ID;

63. Select first_name, incentive amount from employee and incentives table for all employees who got
incentives using left join
Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0) from employees a right join incentives B on
A.EMP_ID=B.EMP_REF_ID;

64. Select max incentive with respect to employee from employee and incentives table using sub query
select DEPT,(select nvl(max(INCENTIVE_AMOUNT),0) from INCENTIVES where
EMP_REF_ID=EMP_ID) Max_incentive from EMPLOYEES;

65. Select TOP 2 salary from employee table


select * from (select * from employees order by SALARY desc) where rownum <3;

66. Select TOP N salary from employee table

67. Select 2nd Highest salary from employee table


select min(salary) from (select * from (select * from employees order by SALARY desc) where
rownum <3);

68. Select Nth Highest salary from employee table

5
69. Select First_Name,LAST_NAME from employee table as separate rows
select FIRST_NAME from EMPLOYEES union select LAST_NAME from EMPLOYEES;

70. What is the difference between UNION and UNION ALL ?


Both UNION and UNION ALL is used to select information from structurally similar tables. That
means corresponding columns specified in the union should have same data type. For example, in the
above query, if FIRST_NAME is DOUBLE and LAST_NAME is STRING above query wont work. Since the
data type of both the columns are VARCHAR, union is made possible. Difference between UNION and
UNION ALL is that , UNION query return only distinct values.

71. Write create table syntax for employee table


Create table employee(
Emp_id int, name varchar);
72. Write syntax to delete table employee
DROP table employees;

73. Write syntax to set EMPLOYEE_ID as primary key in employee table


ALTER TABLE EMPLOYEES add CONSTRAINT EMPLOYEE_PK PRIMARY KEY(EMP_ID);

74. Write syntax to set 2 fields(EMPLOYEE_ID,FIRST_NAME) as primary key in employee table


ALTER TABLE EMPLOYEES add CONSTRAINT EMPLOYEE_PK PRIMARY KEY(EMP_ID,FIRST_NAME);

75. Write syntax to drop primary key on employee table


Alter table employees drop constraint employee_pk;
------------------------------------------------------------------------------------------------------------------
Assignment-2:
1. Create a depth table with deptno as the primary key and
dept_name,emp_name,emp_mobile,emp_address,join_date are not null.

create table dept(

2 dept_no number,

3 dept_name varchar(45),

4 emp_name varchar(45),

5 emp_mobile number,

6 emp_address varchar(45),

7 join_date timestamp,

8 constraint pk_id primary key(dept_no));

2. Insert 10 records in that table.

3. Add emp_email after emp_name in that dept table.


4. Change emp_mobile to phno
5. Add salary field before the join_date field.
6. Change emp_address field by increasing its size 500.
7. Select all emp_name according alphabetical order.

6
8. Update emp_name field data according alphabetical order. 
9. Delete 2nd row from dept table.
10. Select employee details whose salary more than 5000 /-

-----------------------------------------------------------------------------------------------------------------
Assignment-3:

1. Create 4 tables
employee(employee-name, street, city)
works(employee-name, company-name, salary)
company(company-name, city)
manages(employee-name, manager-name)
Give an expression in SQL for each of the following queries:

create table employee(employee_name varchar(89),street varchar(78),city varchar(78));

Table created.

SQL> create table works(employee_name varchar(78),company_name varchar(90),salary int);

Table created.

SQL> create table company(company_name varchar(67),city varchar(67));

Table created.

SQL> create table manages(employee_name varchar(78),manager_name varchar(78));

Table created.

a) Find the names, street address, and cities of residence for all employees who work for 'First Bank
Corporation' and earn more than 10,000.

b) Find the names of all employees in the database who live in the same cities as the companies for
which they work.
c) Find the names of all employees in the database who live in the same cities and on the same streets
as do their managers.
d) Find the names of all employees in the database who do not work for 'First Bank Corporation'.
Assume that all people work for exactly one company.
e) Find the names of all employees in the database who earn more than every employee of 'Small Bank
Corporation'. Assume that all people work for at most one company.
f) Assume that the companies may be located in several cities. Find all companies located in every city
in which 'Small Bank Corporation' is located.
g) Find the names of all employees who earn more than the average salary of all employees of their
company. Assume that all people work for at most one company.
h) Find the name of the company that has the smallest payroll.

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

Assignment-4:

View
====

7
Table: Employee
EMPNO ENAME SAL JOB DEPTNO JOIN_DATE
130 JOHN 4000 MANAGER 10 2014-03-07
140 RAKHI 3000 TEACHER 20 2014-09-07
150 MANAGER 30
160 VC 40
170 4000 MANAGER 50
180 8000 PRINCIPAL 60

Table: Dapt
DEPTNO DNAME LOCATION
10 RESEARCH BBSR
20 CSE BLS

Having clause
1. To display the deptno for which total amount of salary is greater than 10,000.
2 To display the deptno along with their total salary for which total salary is greater then 10000
corresponding to deptno 10,20.
order by clause
3. To display the employee information in the ascending order of the empno.
4. To display the employee information in the descending order of the salary.
5.To display the employee information in the ascending order of the deptno,descending order of salary.
Union
6. To display in deptno the jobs 10 or 20.
7. To display the jobs from emp where deptno 10 or 20
Intersect.
8.To display the jobs in deptno 10 and 20.
9.To display the jobs in deptno 10 and (20or 30 )
Minus
10. To Display the jobs in deptno 10 and not in 20
11. to list the jobs which are unique to dept 10 as compare to 20 and 30
12. Create a view with name empview for Employee table with EMPNO, ENAME, SAL.
13. Create a view name emp10 for Employee table with DEPTNO 10.
14. Insert a new record into empview.
15. To update record of empview.
16. To display the contents of emp10.

-------------------------------------------------------------------------------------------------------------------
Assignment-5:

1. Create table of CUSTOMERS( ID integer NOT NULL,NAME of size 20 NOT NULL,AGE integer
NOT NULL,ADDRESS of size 25 ,SALARY with decimalpoints(18, 2), PRIMARY KEY is ID.

CREATE TABLE ORDERS ( ID is NOT NULL,DATE is DATETIME,CUSTOMER_ID INT


Is FOREIGN KEY,AMOUNT is with decimal points,PRIMARY KEY (ID).

2. If ORDERS table has already been created, and the foreign key has not yet been set
(Customer_ID)as foreign key by altering a table.
3. Drop a FOREIGN KEY constraint.
4. Insert table records CUSTOMERS
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
8
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Insert table records USTOMERS ORDERS
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
5. Join these above 2 tables.
6. Calculate the avarage salary of customers.
7. Count the no of records present in the customers table and orders table.
8. Perform the EQUIJOIN of the 2 tables.
9. Perform the LEFT JOIN of the 2 tables.
10. Perform the LEFT JOIN, RIGHT JOIN, FULL JOIN/ UNION ALL, SELF JOIN, CARTESIAN
JOIN or CROSS JOIN of the 2 tables
11. Create a UNION of 2 tables.
12. (Create a table alias )Display ID,NAME,AGE from customer and AMOUNT from orders table
with customer id present in both the tables.
13. column alias: SELECT CUSTOMER_ID, CUSTOMER_NAME FroM CUSTOMERS WHERE SALARY
IS NOT NULL;

14. Create a index on column NAME,AGE.


15. Remove the index.
16. TRUNCATE TABLEs.
17. Create a view from CUSTOMERS table. This view would be used to have customer name and
age from CUSTOMERS table.
18. The WITH CHECK OPTION Execute the following.
CREATE VIEW CUSTOMERS_VIEW AS SELECT name, age FROM CUSTOMERS
WHERE age IS NOT NULL WITH CHECK OPTION;
19. Insert, update , delete records from the view.
20. Remove the view.

--------------------------------------------------------------------------------------
Assignment-6:

CUSTOMERS
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
CUSTOMERS ORDERS
9
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
1. Display record for which similar age count would be more than or equal to 2. (note: use
having clause).
2. Delete records from the table having age = 25 and then COMMIT the changes in the
database.
3. Delete records from the table having age = 25 and then ROLLBACK the changes in the
database.
4. Create a savepoint as sp1, sp2, sp3 after performing 3 transactions and then rollback and
see the changes.
5. Release the savepoint.
6. Display all the records from CUSTOMERS table where SALARY starts with 200.
7. Display the current date time.
8. Display the current date, time, month, year, day, week.
9. Display the lenth of name whose id is 5 from customer table..
10. Update the name to upper case whose id is 4 from customer table 4.
11. Create and drop a temporay table.
12. Create copies of 2 table (Clone) with different names.
13. Consider a table CUSTOMERS_BKP with similar structure as CUSTOMERS table. Now to copy
complete CUSTOMERS table into CUSTOMERS_BKP.
14. Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table.

Following example updates SALARY by 0.25 times in CUSTOMERS table for all the customers
whose AGE is greater than or equal to 27:

Assuming, we have CUSTOMERS_BKP table available which is backup of CUSTOMERS table.

Following example deletes records from CUSTOMERS table for all the customers whose AGE
is greater than or equal to 27:

15. Create table ss with id auto generated and is of integert ype.

+----+-------------+------------+------------+
| id | name | date | origin |
+----+-------------+------------+------------+
16. Display the distinct record of the tables.

ASSS-7:

Create type-4 driver for mysql.(library jar)?

10

You might also like