ODBA Lab Questions
ODBA Lab Questions
Assignment-1:
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
3. Get First_Name from employee table using alias name “Employee Name”
Select first_name as Employee Name from employees;
10. Get FIRST_NAME from employee table after removing white spaces from left side
Select ltrim(frist_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');
37. Get difference between JOINING_DATE and INCENTIVE_DATE from employee and
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.
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;
5
69. Select First_Name,LAST_NAME from employee table as separate rows
select FIRST_NAME from EMPLOYEES union select LAST_NAME from EMPLOYEES;
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,
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:
Table created.
Table created.
Table created.
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.
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;
--------------------------------------------------------------------------------------
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:
Following example deletes records from CUSTOMERS table for all the customers whose AGE
is greater than or equal to 27:
+----+-------------+------------+------------+
| id | name | date | origin |
+----+-------------+------------+------------+
16. Display the distinct record of the tables.
ASSS-7:
10