BASIC
BASIC
BASIC
Alter table
-----------
The Oracle ALTER TABLE statement is used to add, modify, or drop/delete columns in
a table.
The Oracle ALTER TABLE statement is also used to rename a table.
# modifying the column in a table (Since I have specified zip code to varchar2(10)
I want to change it to number(7)
UPDATE customers
SET zipcode = 12345
WHERE customer_id = 1;
UPDATE customers
SET manager_id = 111
WHERE dept_id = 100;
Renaming a column/s
------------------------------------------
ALTER TABLE customers
RENAME COLUMN zipcode TO postal_zipcode;
4) DO it in the class
Select the first name, email and the salary details from the employee table
5) Selecting the first name and salary of the employees if salary is greater than
6000
6)Now you are willing to sell the data to a marketing company about the details of
the
employees in your organization, about those employees whose salary is greater than
7000.
What fields would like like to display safely ?
Note : What is displayed as the output is what would be sold to 3rd party
companies.
7)The salary field is monthly compensation, how would you obtain the firstname and
annual
compensation of each employee
Note : we can rename the new column to a more meaningful column name
8)Now lets give a hike to every employee. We display both the original annual ctc
and incremented
annual ctc (max 30% hike)
10) Using the concat operator to append 2 column values in the result
11) What if we want a space between the first and last name after concatenating ?
The HR of the company has decided to hire a third party agency to do a back ground
check on
all those employees who are not getting any commision. The 3rd party company needs
the following
details. First name, last name, email id , phone numeber and the employee id. Write
a query which
would display the above details only.
14)How to find out how many distinct departments are there from the employee table
Check if the above output contains any NULL values in the output. Modify the query
to igore
the NULL
17)How to count how many distinct departments are there in the employee table
18) Modify the above query in such a way that NULL must be ignored in the
department column
count
What can u infer from the output of query of question 17 and 18 (how does count
work) ?
20) IN CLASS EXERCISE
Find out the count of all the employees who salary is greater than 6000
Find out the count of all the employees whose salary is beteween 6000 and 7000
Hint : salary > 6000 and salary < 7000 is the syntax to be used in the where clause
26)Lets try to find out how many people are there in each kind of jobs (if u notice
the
job_id column, there are different kind of designations)
28) Someone from an auditing firm needs to know the total number of distict job
types in
this organization. How would u write a query for this ?
29) How to fetch only the rows where job_id has the word clerk ?
Modify the above query to find out how many clerks are there in the office
Find out how many people are there in the non clerk positions
32) An auditing firm wants to know the average salary of all the employees
Modify the above query to find out the average salary including the commission
Find out the average salary of only the clerical grade employees (do not consider
the commisions)
You manager wants to know many kinds of clerical jobs are there in the company ?
select job_id , count(job_id) from hr.employees where job_id like '%CLERK%' group
by job_id;
In the above query, I want to find out even the average salary of each clerk type.
37) Creating a view ( a pseudo table ) which can be queried just like a normal
table
create view non_clerk as select * from hr.employees where job_id not like '%CLERK%'
;
Find out how many rows are there in the non_clerk table
Your manager wants to know how many kinds of non clerical jobs are there in the
company
and what is the employee count of each job category along with thier average salary
Modify the above query so that the result is sorted by the average salary column
41)Select only those rows where the job id is either IT person or a fianance person
select * from hr.employees where job_id like '%IT%' or job_id like '%FI%' ;
42) Select only those IT employees whose saalry is greater than 5000
select * from hr.employees where job_id like '%IT%' and salary > 5000;
43) Selecting only rows satifying both conditions using the and operator
select * from hr.employees where salary > 6000 and job_id = 'FI_ACCOUNT';
For how many years did employee 101 work as Account Manager
Write a query to display the salary of all the employees with income tax deduction
of flat 10% tax. The following data needs to be present in the output
emplyoyee id, first name, email id, original annual ctc with bonus, salary after
10% tax deduction
Write a query to display the count of employees who belong to management cadre
(including president and VP) and those who do not belong to this category
**************************************SESSION2*************************************
1) An interesting query to increment the salary based on the kind of job the
employees are
into
2)
Modify the above code in such a way that salary hike will be provided based on the
current
pay bracket
3) IN CLASS EXERCISE
Modify the above code to now deduct the tax based on the pay scale as per the below
scale
----------------------------------------------------------------------------
Understanding joins using 2 simple tables (customer table and sales table)
q)Lets try the most commonly used kind of join (the inner join)
Note : Only the matching customer_id from left table & right table will be selected
in the
final output. Those customer_id's not found in right table for a corresponding
entry in
left table will not be displayed in the output !
q) Lets try the left join (also known as the left outer join in oracle)
Business query : I want to know all the customers who have registered, made a
purchase and
also those who have not made any purchase !
q) Lets try the right join (is the same as the right outer join)
----------------------------------------------------------------------------
Now I want to know which employee works in which department by a default join
operation
(inner join) The below is the brief overview of different kinds of joins
INNER JOIN:
Returns rows that match on common key values, compared from both tables
LEFT JOIN:
Returns matched rows from both tables
And Unmatched rows from the left table#
RIGHT JOIN:
Returns matched rows from both tables
And Unmatched rows from the Right table
Now lets quickly see what different kinds of joins can look like
Note : There is no difference between left join and left outer join , right join
and right outer join,
full join and full outer join.
5) Now looking at the schema (jpeg image shared) I want to join 3 tables to find
out
which city is an employee working in
6) IN CLASS EXERCISE
Write a query to display the following columns and sort the result by descending
order of salary.
7) IN CLASS EXERCISE
Your manager wants a report based on which country employees have been most loyal
employees
You just need to popoulate all the details in a table in an appropriate manner.
8) IN CLASS EXERCISE
Write a query to find out the firtname, last name, department name, job_id , salary
of all
the employees who either work in london
9) IN CLASS EXERCISE
Write a query to fetch the department id, department name, manager id and manager's
first name
Using the result of the above query, write a query to populate the following data
Employee id, first name, last name, department name, his manager's name.
Write a query to display job title, employee name, and the difference between
salary of
the employee and minimum salary for the job
Using the result of the previous query. If the difference between the current
salary and max
salary is greater than 30% of the current salary , then make a market correction by
giving a
raise of 30 % from the current salary and populate the following columns
employee_id, first name, last name, department_name, years of experince, current
salary,
max salary, difference in salary, incremented salary if applicable or same salary