Oracle Commands Basics
Oracle Commands Basics
Select from select * from tab --displays all the tables or views
desc desc regions -- describes/displays region table
Select from select * from tablename -- displays entries in the table namely tablename
Select from select employee_id,first_name,email from employees -- displays the records of these three fields in the
employees table
Select from where select * from employees
where department_id=90
-- displays the entries where department_id is 90
Select from where select * from employees
where salary between 4000 and 6000
-- displays salary b/w 4000 and 6000
Select from where select * from employees
where first_name like 'A%
-- displays first_name starts with A
Select from where select * from employees
where first_name like '_a%'
-- displays first_name has second letter a
Create table create table naaa -- creates a new table namely naaa
Create table create table naaa
(ot_name varchar2(50),
id number(10),
sal number(7,2))
-- creates a new table namely naaa
--creates a field called ot_table, texttype with 50
characters
--creates a field called id, number type with 10
characters
--creates a field called sal, number type with 7
characters and 2 decimals
Drop table drop table naaa Deletes the table namely naaa
Rename Rename dept to dept80
Insert into insert into naaa
values ('raja',13,23890.75)
-- inserts values to the fields
Select from where select first_name,email,salary from employees
where department_id=60
Select from where order by select first_name,email,salary from employees
where department_id=60
order by first_name desc
Select from where or select first_name,email,salary,department_id from employees
where department_id=60 or department_id=80
Select from where in select first_name,email,salary,department_id from employees
where department_id in (60,80)
Select from inner join on select
first_name,email,salary,departments.department_id,department_name
from employees
inner join departments on
employees.department_id=departments.department_id
Make relationship between two tables
Select from where select first_name,last_name from employees where job_id='AD_VP' or
job_id='ST_CLERK'
Select from where in select first_name,last_name from employees where job_id in
('AD_VP','ST_CLERK')
Select from inner join on select first_name,email,employees.department_id,start_date,end_date
from employees
inner join job_history on
employees.employee_id=job_history.employee_id
Select distinct select distinct manager_id,first_name from employees -- to show all unique manager_ids and Firstnames
Select from select employee_id,first_name,salary,salary*12 "Annual Salary" from
employees
-- to display salary * 12 as with Column name
"Annual Salary"
Select from select employee_ID,first_name,salary,salary+(salary*commission_pct) "Total
Salary" from employees
Using mathematical equations
Select from select employee_id "Identity Number",first_name||' Phone Number is
'||phone_number "Name and Phone" from employees
--displays the first name and phone number of
employees
Select from order by select first_name||' '||last_name||' is working in department
'||department_id||', his salary is '||salary|| ' and his manager is
'||manager_id "Details of Employees"
from employees
order by First_name
Concatenate column names along with some
joining text
Select case when else end
from
select first_name,department_id,salary,
case department_id
when 100 then salary*1.2
when 60 then salary+2000
else salary
end "NEWSALARY"
from employees
Select case when else end
from
select first_name,department_id,salary,
case department_id
when 100 then 'IT Officials'
when 60 then 'Administration'
else 'Not Applicable'
end "NEWDEPT" from employees
Select case when else end
from
select first_name,email,job_id,salary "Old Salary",
case job_id
when 'IT_PROG' then salary*2
when 'AD_PRES' then salary*10
else salary
end
"New Salary" from employees
Create table as create table emp_new
(emp_no,name,email,mobileno)
as
select employee_id,first_name,email,phone_number
from employees
Create a new table using column names from an
old table
Delete from where delete Client_Master where state='Maharashra' Delete rows in a table
Update set update emp_new
set name = 'Vijay'
where emp_no = 100
To update information inside a table
Alter table add alter table emp_new
add (father_name varchar2(100))
To add a column
Alter table modify ALTER TABLE dept80
MODIFY (last_name VARCHAR2(30))
To modify the characteristics of a column
Alter table rename column alter table emp_new
rename column father_name to parent_name
To rename a column
Alter table drop column alter table emp_new
drop column parent_name
To delete a column
select first_name, last_name, departments.department_id,
department_name, job_title from
employees inner join
departments
on employees.department_id=departments.department_id
inner join
jobs
on employees.job_id=jobs.job_id
select first_name, last_name, b.department_id, department_name, city
from
employees a inner join
departments b
on a.department_id=b.department_id
inner join
locations c
on b.location_id=c.location_id
create view emp_city
as
select first_name, last_name, b.department_id, department_name, city
from
employees a inner join
departments b
on a.department_id=b.department_id
inner join
locations c
on b.location_id=c.location_id
where a.department_id in (10,60,90)
select department_id,sum(salary) "Sum of Salaries per Department"
from employees
group by department_id
order by sum(salary)
select department_id,count(employee_id) "number of employees"
from employees
group by department_id
order by count(employee_id) desc
select department_id,max(salary) "maximum salary"
from employees
group by department_id
order by max(salary) desc
select department_id,count(*) "number of employees"
from employees
group by department_id
order by count(*) desc
select department_id,round(avg(salary),2) "average salary"
from employees
group by department_id
order by avg(salary) desc
SELECT department_id, MIN(salary), MAX (salary) FROM employees
GROUP BY department_id
HAVING MIN(salary) < 7000
ORDER BY MIN(salary);