You are on page 1of 7

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 Employee;
2. Get First_Name,Last_Name from employee table
select First_name,Last_name from Employee;
3. Get First_Name from employee table using alias name “Employee Name”
select First_name as Employee_name from Employee;
4. Get First_Name from employee table in upper case
select upper(First_name) from Employee;
5. Get First_Name from employee table in lower case
select lower(First_name) from Employee;
6. Get unique DEPARTMENT from employee table
select distinct(Department) from Employee;
7. Select first 3 characters of FIRST_NAME from EMPLOYEE
select substr(First_name,1,3) from Employee;
8. Get position of 'o' in name 'John' from employee table
select instr(First_name,'o') from Employee where First_name='John';
9. Get FIRST_NAME from employee table after removing white spaces from right side
select rtrim(First_name) from Employee;
10. Get FIRST_NAME from employee table after removing white spaces from left side
select ltrim(First_name) from Employee;
11. Get length of FIRST_NAME from employee table
1
select length(First_name) from Employee;
12. Get First_Name from employee table after replacing 'o' with '$'
select replace(First_name,'o','$') from Employee;
13. Get First_Name and Last_Name as single column from employee table separated by a '_'
select First_name||'_'||Last_name from Employee;
14. Get FIRST_NAME ,Joining year,Joining Month and Joining Date from employee table
select First_name,Joing_date from Employee;
15. Get all employee details from the employee table order by First_Name Ascending
select First_name from Employee order by First_name asc;
16. Get all employee details from the employee table order by First_Name descending
select First_name from Employee order by First_name desc;
17. Get all employee details from the employee table order by First_Name Ascending and Salary
descending
select First_name from Employee order by First_name asc;
18. Get employee details from employee table whose employee name is “John”
select * from Employee where First_name='John';
19. Get employee details from employee table whose employee name are “John” and “Roy”
select * from Employee where First_name='John' or First_name='Roy' ;
20. Get employee details from employee table whose employee name are not “John” and “Roy”
select * from Employee where First_name not in ('John','Roy') ;
21. Get employee details from employee table whose first name starts with 'J'
select First_name from Employee where First_name like 'J%';
22. Get employee details from employee table whose first name contains 'o'
select First_name from Employee where First_name like '%o%';
23. Get employee details from employee table whose first name ends with 'n'
select First_name from Employee 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 Employee 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 Employee where First_name like 'J___';
26. Get employee details from employee table whose Salary greater than 600000
select * from Employee where Salary>=60000;
27. Get employee details from employee table whose Salary less than 800000
select * from Employee where Salary<=800000;
28. Get employee details from employee table whose Salary between 500000 and 800000
select * from Employee where Salary>=500000 and salary<=800000;
29. Get employee details from employee table whose name is 'John' and 'Michael'
select * from Employee where First_name in ('John','Michael');
30. Get employee details from employee table whose joining year is “2013”
31. Get employee details from employee table whose joining month is “January”
32. Get employee details from employee table who joined before January 1st 2013
33. Get employee details from employee table who joined after January 31st
35. Get Joining Date and Time from employee table
36. Get Joining Date,Time including milliseconds from employee table
37. Get difference between JOINING_DATE and INCENTIVE_DATE from employee and
38. Get database date
39. Get names of employees from employee table who has '%' in Last_Name. Tip : Escape character for
special characters in a query.
40. Get Last Name from employee table after replacing special character with white space
41. Get department,total salary with respect to a department from employee table.
42. Get department,total salary with respect to a department from employee table order by total salary
descending
43. Get department,no of employees in a department,total salary with respect to a department from
employee table order by total salary descending
44. Get department wise average salary from employee table order by salary ascending
45. Get department wise maximum salary from employee table order by salary ascending
2
46. Get department wise minimum salary from employee table order by salary ascending
47. Select no of employees joined with respect to year and month from employee table
48. Select department,total salary with respect to a department from employee table where total salary
greater than 800000 order by Total_Salary descending
49. Select employee details from employee table if data exists in incentive table?
50. How to fetch data that are common in two query results?
51. Get Employee ID's of those employees who didn't receive incentives without using sub query ?
52. Select 20 % of salary from John , 10% of Salary for Roy and for other 15 % of salary from
employee table
53. Select Banking as 'Bank Dept', Insurance as 'Insurance Dept' and Services as 'Services Dept' from
employee table
54. Delete employee data from employee table who got incentives in incentive table
55. Insert into employee table Last Name with " ' " (Single Quote - Special Character)
56. Select Last Name from employee table which contain only numbers
57. Write a query to rank employees based on their incentives for a month
58. Update incentive table where employee name is 'John'
59. Select first_name, incentive amount from employee and incentives table for those employees who
have incentives
60. Select first_name, incentive amount from employee and incentives table for those employees who
have incentives and incentive amount greater than 3000
61. Select first_name, incentive amount from employee and incentives table for all employes even if
they didn't get incentives
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.
63. Select first_name, incentive amount from employee and incentives table for all employees who got
incentives using left join
64. Select max incentive with respect to employee from employee and incentives table using sub query
65. Select TOP 2 salary from employee table
66. Select TOP N salary from employee table
67. Select 2nd Highest salary from employee table
68. Select Nth Highest salary from employee table
69. Select First_Name,LAST_NAME from employee table as separate rows
70. What is the difference between UNION and UNION ALL ?
71. Write create table syntax for employee table
72. Write syntax to delete table employee
73. Write syntax to set EMPLOYEE_ID as primary key in employee table
74. Write syntax to set 2 fields(EMPLOYEE_ID,FIRST_NAME) as primary key in employee table
75. Write syntax to drop primary key on employee table
------------------------------------------------------------------------------------------------------------------
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.
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.
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)

3
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:
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
====

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.
4
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 |
+----+----------+-----+-----------+----------+
| 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.
5
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
+-----+---------------------+-------------+--------+
|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.

6
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.

You might also like