oracle commands basics

7
Command Example Use 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

Upload: virender-kulharia

Post on 21-Jul-2016

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Oracle commands basics

Command Example UseSelect from select * from tab --displays all the tables or viewsdesc desc regions -- describes/displays region tableSelect from select * from tablename -- displays entries in the table namely tablenameSelect from select employee_id,first_name,email from employees -- displays the records of these three fields in the

employees tableSelect from where select * from employees

where department_id=90-- displays the entries where department_id is 90

Select from where select * from employeeswhere salary between 4000 and 6000

-- displays salary b/w 4000 and 6000

Select from where select * from employeeswhere first_name like 'A%’

-- displays first_name starts with A

Select from where select * from employeeswhere first_name like '_a%'

-- displays first_name has second letter a

Create table create table naaa -- creates a new table namely naaaCreate 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 naaaRename Rename dept to dept80Insert 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=60order 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)

Page 2: Oracle commands basics

Select from inner join on select first_name,email,salary,departments.department_id,department_namefrom employeesinner 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_datefrom employeesinner 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 FirstnamesSelect 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_idwhen 100 then salary*1.2when 60 then salary+2000else salaryend "NEWSALARY"from employees

Page 3: Oracle commands basics

Select case when else end from

select first_name,department_id,salary,case department_idwhen 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_idwhen 'IT_PROG' then salary*2when 'AD_PRES' then salary*10else salaryend "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 tableUpdate set update emp_new

set name = 'Vijay'where emp_no = 100

To update information inside a table

Alter table add alter table emp_newadd (father_name varchar2(100))

To add a column

Alter table modify ALTER TABLE dept80MODIFY (last_name VARCHAR2(30))

To modify the characteristics of a column

Alter table rename column alter table emp_newrename column father_name to parent_name

To rename a column

Alter table drop column alter table emp_newdrop column parent_name

To delete a column

Page 4: Oracle commands basics

select first_name, last_name, departments.department_id, department_name, job_title fromemployees inner joindepartmentson employees.department_id=departments.department_idinner joinjobson employees.job_id=jobs.job_idselect first_name, last_name, b.department_id, department_name, city fromemployees a inner joindepartments bon a.department_id=b.department_idinner joinlocations con b.location_id=c.location_idcreate view emp_cityasselect first_name, last_name, b.department_id, department_name, city fromemployees a inner joindepartments bon a.department_id=b.department_idinner joinlocations con b.location_id=c.location_idwhere a.department_id in (10,60,90)select department_id,sum(salary) "Sum of Salaries per Department" from employeesgroup by department_idorder by sum(salary)

Page 5: Oracle commands basics

select department_id,count(employee_id) "number of employees" from employeesgroup by department_idorder by count(employee_id) desc

select department_id,max(salary) "maximum salary" from employeesgroup by department_idorder by max(salary) descselect department_id,count(*) "number of employees" from employeesgroup by department_idorder by count(*) descselect department_id,round(avg(salary),2) "average salary" from employeesgroup by department_idorder by avg(salary) descSELECT department_id, MIN(salary), MAX (salary) FROM employeesGROUP BY department_id HAVING MIN(salary) < 7000ORDER BY MIN(salary);