Vous êtes sur la page 1sur 39

SELECT DISTINCT or nume, nota, nota+2 as “NOTA”, nota || ‘ ’ || nume

FROM teste;

Select nota

From teste

Where nota>2;

Insert into teste (nume, nota)

Values (‘Ion’, 8);

CONCATENARE “||”

Select nota

From teste

Where nota between 7 and 8 // where nota >=7 and <=8;

Select nota

From teste

Where nota in (5,7,9) // where nota =5 or nota=5 or nota=9;

Select *

From teste

Where nume like '_a%'; // “_” any single char; “%” any string

Select *

From teste

Where nume like '%\_N%' escape '\' // use “\” for “_”
Select *

From teste

Where nota is null; // is not null

select (319/29)+12

from math

delete

from TESTE

where NUME='George'

select initcap(lower(upper(nume)))

from teste

where lower(nume) like '%v%'

select concat(nume,concat(' ',nota))

from teste

select substr(nume, 1,1)

from teste; //substrage de la 1 pina la 1 caracter

select length(nume)

from teste //lungime nume

select nume,instr(lower(nume),'a') //gaseste primul ’a’

from teste

where instr(lower(nume),'a') !=0

select rpad(lpad(nume,10,'*'),15,'#')

from teste
select trim(leading 'v' from lower(nume)), trim(trailing 'a' from lower(nume)), trim (both 'a' from
lower(nume))

from teste; //eliminia primul sau/si ultimiul caracter ’a’ daca este prezent

select substr(nume,length(nume),1)

from teste //ULTIMUL CARACTER

select substr(nume,1,1)

from teste //PRIMUL CARACTER

select substr(lower(nume),instr(lower(nume),'a'),10)

from teste

where instr(lower(nume),'a')!=0 //substrage de la primul ’a’ gasit pana la 10 caractere

select substr(lower(nume),1,instr(lower(nume),'a')-1)

from teste

where instr(lower(nume),'a')!=0 //substrage de la caracterul 1 pana la primul ’a’ gasit

select substr(lower(nume),1,instr(lower(nume),'a')-1) ||
substr(lower(nume),instr(lower(nume),'a')+1,10)

from teste

where instr(lower(nume),'a')!=0 //elimina primul ’a’ gasit

select replace(nume,'a', '*')

from teste //inlocuieste ’a’ cu ’*’

select replace(lower(nume),'a')

from teste //elimina caracterul ’ a’

select *

from teste

where nota=:nr //atribui valoarea pt variabila nr din consola

CUM DE FOLOSIT :COLUMN PENTRU SELECT :COLUMN????


select sysdate

from math //data curenta

select mod(5,3)

from math //modulo

select trunc(15.952,0), trunc(15.952,2), trunc(15.952,-1),trunc(55.952,-2)

from teste //trunchiere

select round(15.952,0), round(15.952,2), round(15.952,-1),round(55.952,-2)

from teste //rotunjire

select nume, (sysdate-data)/30

from teste

where lower(nume) like '%v%'; //saptamani de la evinement pina acum

select nume, months_between(sysdate,data)

from teste //luni intre 2 date

select nume, add_months(data,12)

from teste //adauga 12 luni

select next_day(sysdate,'Sunday')

from math //cauta urmatoarea duminica din prezent

select last_day(sysdate)

from math //ultima zi a lunii

select nume,round(data,'Year') //trunc(data,’Month’)

from teste //rotunjeste la an


select to_char(data, 'YYYY/DD/Mon')

from teste //convert to char

//fm delete leading zero in the date


select to_number('4.221','99999.999')

from math

select to_date('November 13, 1990', 'mm,dd,yyyy')

from math

//fx(format exact) fxMonDD

//RR no current century

select nvl(nume,'None'),nota

from teste //inlocuieste NULL cu ’NONE’

select nume,nvl2(nota,nota+1,-1) as test

from teste //daca nota!=NULL atunci nota+1 else daca este NULL – -1

alter table teste

add Nota1 varchar2(20); //add new column

update teste set

nota1=nota //copiaza o coloana in alta

select nume,prenume, nullif(length(nume),length(prenume))

from teste //null daca lungimile sunt egale, sau length(nume) daca nu sunt egale

select COALESCE(nume,prenume,'De completat')

from teste //daca nume, prenume – null atunci ‘De completat’ altfel nume sau prenume not null

alter table TESTE

drop column nota1; //sterge coloana nota1


select nume,prenume,

Case

when nota<=5 then 'Respins'

else 'Admis'

end as ADMITERE

from TESTE; //daca nota este mai mica de 5 – RESPINS else ADMIS

select nume,prenume,

decode(nota,4,'Respins','ADMIS') as Admitere

from teste; //daca e 4 atunci RESPINS else ADMIS

//CUM DE FOLOSIT EXPRESII NU DOAR...

create table unknown

(asq varchar2(20), nr number); //creeaza un tabel nou

drop table unknown; //sterge tabelul

alter table TESTE

rename to TESTE1; //rename teste to TESTE1

select nume,prenume, clasa, profil,diriginte

from teste natural join clase; //selecteaza din 2 tabele unde clasa PK

select nume,prenume, clasa, profil,diriginte

from teste cross join clase; //combina randurile intre cele 2 table

//*sa nu se repete datele

select nume,prenume, clasa, profil,diriginte

from teste join clase using (clasa); //la fel natural join doar ca clasa specificat PK

//nu este obligatoriu sa fie de acelasi tip


alter table clase

modify clasanr varchar2(20); //modifica tipul de data

select nume,prenume,t.clasa,profil,diriginte

from teste t join clase c on (t.clasa = c.clasa); //la fel join ... using

//nu e oblig sa fie de acelasi nume

//sa nu se repete coloanele din ambele tabele

SELECT nume,prenume,premiul

from teste join premianti on(nota between nota_min and nota_max)

order by nota desc; //atribuie premiul corespunzator notei

SELECT nume,prenume,nota,clasa,premiul,diriginte,profil

from teste

join clase using (clasa)

join premianti on(nota between nota_min and nota_max)

order by nota desc; //afisieaza elevii, dirig lor si premiul corespunzator

select t.nume,t.prenume, c.clasa, c.diriginte,c.profil

from teste t

left outer join clase c on (t.clasa=c.clasa); //afisieaza null daca nu a facut leg cu tabelul clase

select t.nume,t.prenume, c.clasa, c.diriginte,c.profil

from teste t

right outer join clase c on (t.clasa=c.clasa); //afisizea null pt profesorii fara elevi

select t.nume,t.prenume, c.clasa, c.diriginte,c.profil

from teste t

full outer join clase c on (t.clasa=c.clasa); //afisieaza toate din ambele chiar daca e null
select e.nume,e.prenume,e.clasa,f.prenume

from teste e

left outer join teste f

on(e.id_frate=f.id_elev); //afiseaza fratele daca este din acelasi tabel

select e.nume,e.prenume,e.clasa,f.prenume as "Frate/sora"

from teste e left outer join teste f

on(e.nume=f.nume and e.prenume<>f.prenume)

order by e.id_elev; //afisieaza toti fratii/surorile posibile repetand linia

select level,nume||' '||prenume || ' condus de ' || prior nume as "Hierarchy"

from profesori

start with prof_id=1

connect by prior PROF_ID=SEF_ID; //restabileste ierarchia in tabelul profesori si afiseaza nivelul

select lpad(nume,length(nume)+(level*2)-2,'_') as ORGANIZARE

from profesori

start with prof_id=1

connect by prior prof_id=sef_id; //afiseaza ierarhia prin completare cu „_”

select lpad(nume,length(nume)+(level*2)-2,'_') as ORGANIZARE

from profesori

start with prof_id=1

connect by prior prof_id=sef_id

AND PROF_ID!=7; //exclude al 7 prof din ierarhie si toti subalternii acestuaia

select e.nume,e.prenume,c.profil,c.diriginte

from elev e,clasa c

where c.clasa=e.clasa; //identic join on (...=...)


select e.nume,e.prenume,c.profil,c.diriginte

from elev e,clasa c //cross join

select e.nume,e.prenume, c.nume

from elev e,clasa c

where e.clasa=c.clasa(+); //left outer join

select e.nume,e.prenume,e.nota, a.premiul, e.clasa, c.nume || ' ' || c.prenume as Diriginte, p.materie
as "Materie diriginte"

from elev e, clasa c,profesor p,premiu a

where e.clasa=c.clasa and c.nume=p.nume and e.nota >=a.nota_min and e.nota<=a.nota_max

order by e.nota desc; //leaga 4 tabele diferite

select sum(nota)

from elev; //avg(...),min(...),max(...),count(...), variance(...),stddev(...)

select avg(nota),clasa

from elev

group by clasa; //afiseaza media pentru fiecarea clasa grupat

select max(avg(nota))

from elev

group by clasa; //media clasei cea mai mare

select avg(nota)

from elev

group by clasa

having count(clasa)>1; //media clase unde este macar 1 elev


select avg(nota)

from elev

group by clasa

having count(clasa)>1

order by clasa;

select avg(nota),clasa,grupa

from elev

group by rollup(clasa,grupa); //media pe grupa –>clasa–>total

select avg(nota),clasa,grupa

from elev

group by cube(clasa,grupa); //media total/grupa/grupa->clasa

select avg(nota),clasa,grupa,limba

from elev

group by grouping sets ((clasa,grupa),(clasa,limba)); //grupeaza dupa mai multe seturi

select nume, to_char(null)

from teste

union

select to_char(null),nume //union all, intersect,minus

from admitere40; //reuneste 2 query

//to char(null) pentru a adauga o coloana

select nume

from elev

where nota>

(select nota

from elev

where id_elev=1); //subquery care returneaza o singura valoare


select nume,prenume,nota

from elev

where nota>

(select avg(nota)

from elev); //elevi cu nota mai mare decat media

select nume,prenume,nota,clasa

from elev

where nota in

(select nota

from elev

where clasa='12B'); //multiple row subquery

//afiseaza elevi care au aceeasi nota cu macar unul din elevii din 12B

select nume,prenume,nota,clasa

from elev

where nota > any

(select nota

from elev

where clasa='12B'); //afiseaza elevii care au nota mai mare decat macar un elev din 12B

select nume,prenume,nota,clasa

from elev

where nota > all

(select nota

from elev

where clasa='12B'); //elevii care nota mai mare decat orice elev din 12B
select nume,prenume,nota,clasa

from elev

where (grupa,limba) in

(select grupa,limba

from elev

where clasa='12B'); //multi-column query

select nume,prenume,nota,clasa

from elev a

where a.nota >

(select avg(b.nota)

from elev b

where b.clasa=a.clasa); //elevii care media mai mare decat media clase sale

select nume,prenume,clasa

from elev a

where not exists

(select *

from elev b

where b.id_frate=a.id_elev); //afiseaza pe tot cei care nu au frate/sora

select nume,prenume,clasa

from elev a

where a.id_elev not in

(select b.id_frate

from elev b

where b.id_frate is not null); //similar afiseaza doar pe cei care au frate
with rude as

(select distinct id_frate

from elev

where id_frate is not null)

select nume,prenume

from elev

where id_elev in

(select *

from rude); // similar afiseaza doar pe cei care au frate

//tabelul rude poate fi apelat ulterior

select table_name

FROM user_tables; //afiseaza numele tuturor tabelelor

create table elev_copy

as (select * from elev); //copaiaza un tabel

describe elev; //descrie tabelul

insert into elev (nume,prenume,clasa,nota)

values ('Andreescu','Han','12C',9); //adauga randuri (se poate si fara numele coloanelor)

insert into math

values (user); //numele utilizatorului

insert into test1 (nume,prenume,nota)

select nume,prenume,nota

from elev

where lower(nume) like '%a%'; //insereaza mai multe randuri din alt tabel
update employees_copy

set phone_number=12345; //update la un rand

update employees_copy

set phone_number=

(select phone_number

from employees

where employee_id=101); //update cu query

update employees_copy e

set e.department_name=(select d.department_name

from departments d

where e.department_id=d.department_id); //correlated subquery

alter table employees_copy

add (departament_name varchar2(20)); //add new column

delete from employees_copy

where employee_id =101; //sterge un rand

select employee_id

from employees_copy

where employee_id=206

for update; //blocheaza randurile selectate

create table test

utilizator varchar2(20) default user,

data date default sysdate

) //valori de default
insert into test (nota,utilizator)

values(1,default); //introduce valoarea de default (null sau definita)

merge into emp_copy c using employees e on (c.employee_id=e.employee_id)


when matched then update
set c.last_name=e.last_name
when not matched then insert
values(e.EMPLOYEE_ID,e.FIRST_NAME,e.LAST_NAME,e.EMAIL,e.PHONE_NUMBER,e.HIRE_DATE,e.JO
B_ID,e.SALARY,e.COMMISSION_PCT,e.MANAGER_ID,e.DEPARTMENT_ID,e.BONUS);
//verifica corespondenta datelor dintre cele 2 table (completeaza sau adauga randuri)

insert all
when lower(last_name) like '%a%' then
into emp1 values
(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COM
MISSION_PCT,MANAGER_ID,DEPARTMENT_ID,BONUS)
when lower(last_name) like '%b%' then
into emp2
values(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,
COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID,BONUS)
select
EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMI
SSION_PCT,MANAGER_ID,DEPARTMENT_ID,BONUS
from employees ;
//introduce in tabele daca conditia este indeplinnita (first doar primul gasit)

//table extern
select table_name,status

from user_tables; //afiseaza numele si statutul tabelelor utilizatorilo (all_tables)

systimestamp //timpul pana la milisecunde

create table time

(time_with_offset TIMESTAMP WITH TIME ZONE); //stocheaza cu fusul orar

Insert into time

Values (’10-Jun-2017 10:53:23.1245 AM +2:00 ’);

create table time

(time_with_offset TIMESTAMP WITH LOCAl TIME ZONE); //converteste deodata in fusul orar local

create table time_example

(durata_1 INTERVAL YEAR(3) to MONTH,

durata_2 INTERVAL YEAR(2) to MONTH); //salveaza intervale de timp

insert into time_example (durata_1,durata_2)

values (interval '120' Month(3),interval '3-6' year to Month); //insereaza 120 de luni si 3 ani si 6 luni

create table time_example2

(durata_1 INTERVAL Day(3) to Second,

durata_2 INTERVAL Day(3) to Second); //salveaza intervale de zile->secunde

insert into time_example2 (durata_1,durata_2)

values (interval '25' day(2),interval '4 10:30:10' day to second ); //insereaza intervale de timp

select sysdate+durata_1,sysdate+durata_2

from time_example2; //afiseaza ora actuala + intervalul de timp

CLOB //tip de data pana la 128TB BLOB,RAW//JPG,WAV,MP3


alter table time_example2

add (nota number default '2',nume varchar(20) default 'Necunoscut'); //adauga coloane si default lor

alter table time_example2

modify (nume varchar(10) default 'Ion'); //modifica tipul de date si valoarea de default

alter table time_example2

drop column nota; //sterge o coloana

alter table time_example2

set unused (nume); //ascunde o colona

alter table time_example2

drop unused columns; //sterge coloanele nefolosite

drop table test; //sterge tabelul

flashback table test to before drop; //recupereaza un tabel sters

select *

from user_recyclebin; //a vedea tabelele din urna

drop table test Purge; //sterge complet tabelul

rename emp1 to time_ex; // redenumeste un tabel

truncate table test; //sterge liniile dintr-un tabel, ireversibil

comment on table emp2

is 'Copy of employees.'; //comenteaza un table


select table_name,comments

from user_tab_comments; //afiseaza comentariile

select employee_id,first_name|| ' ' || last_name as "Name",

versions_operation as "Operation",

versions_starttime as "Start_Date",

versions_endtime as "End_Date", salary

from emp1

versions between scn minvalue and maxvalue

where employee_id=101; //afiseaza modificarile din tabel

create table clients1

(client_number number(4) constraint clients_client_number_pk Primary Key, //cheia primara

last_name varchar2(20) constraint clients_last_name_nn not null, //nu este null

email varchar2(20) constraint clients_email_uk unique); //este unic

create table clients

(client_number number(4),last_name varchar2(20) ,email varchar2(20), id number,

constraint clients_id_email_uk unique (id,email)); //la nivel de tabel

create table waiters

(lasta_name varchar2(20),

client_number number constraint waiters_client_nr_fk references clients1(client_number)); //foreign k

create table waiters

(lasta_name varchar2(20),

client_number number,

constraint waiters_client_nr_fk foreign key (client_number )

references clients1(client_number) on delete cascade); //foreign key la nivel de tabel


//stergerea din pk va sterge si liniile din fk

sau

references clients1(client_number) on delete set null); //stergerea pk seteaza null in fk


create table waiters

(firs_name varchar2(20),

client_number number,

constraint waiters_client_nr_fk check (client_number >0)) //doar valori mai mari de 0

alter table waiters

add constraint client_number_uk unique (client_number); //adauga o regula

alter table waiters

modify (first_name constraint first_name_nn not null); //modifica not null

select *

from user_constraints

order by last_change desc; //afiseaza toate constrangerile

sau

select *

from user_cons_columns;

alter table waiters

drop constraint FIRST_NAME_NN CASCADE; //sterge o constrangere

alter table waiters

disable constraint CLIENT_NUMBER_UK; //opreste o regula

alter table waiters

enable constraint CLIENT_NUMBER_UK; //porneste regula

select constraint_name,table_name,constraint_type,status

from user_constraints

where lower(table_name)=lower('waiters'); //afiseaza constrangerile unui table


create view emp_view123

as select e.employee_id,e.first_name, e.last_name, e.email

from emp e

where e.employee_id in (select c.employee_id from emp c where c.employee_id between 101 and 124);

//creeaza un view

Create or replace view test

As select * from employees; //creaza sau inlocuieste un view

create or replace view emp_view123

as select employee_id as "ID",last_name||' '||first_name as "Nume,Prenume", email

from emp

where employee_id between 101 and 124

with check option constraint emp_view123; //interzice modificarile inafara tabelului

or

with read only; //interzice insert,update...


drop view emp_view; //sterge un view

select rownum,first_name

from (select * from employees order by hire_date) //afisieaza numarul liniei

//dintai order by apoi rownum

select first_name

from (select * from employees order by hire_date)

where rownum<=5; //afiseaza doar primele 5 rezultate

create force view emp1245

as select * from emploeee; //creaza view chiar daca este imposibil

create sequence employee_id

increment by 1

start with 1

maxvalue 5000

nocache

nocycle; //creeaza un sequence

select sequence_name, min_value,max_value,increment_by,last_number

from user_sequences; //afiseaza toate sequences

insert into emp (first_name,last_name,employee_id)

values ('Andrei','Aleasda',employee_id.nextval); //introduce valoarea din secventa

insert into emp (first_name,last_name,employee_id,department_id)

values ('Andrei','Aleasda',employee_id.nextval,departments_seq.currval); //valoarea curenta din seq

select employee_id.nextval,first_name

from emp;
alter sequence employee_id

increment by 1

maxvalue 99999

nocache nocycle; //modifica o secventa

drop sequence employee_id; //sterge o secventa

create index emp_first_name_idx

on emp(first_name); //creeaza un index la coloana firs... din emp

or

on emp(firs_name,last_name); //index pe mai multe coloane

select distinct ic.index_name,ic.column_name, ic.column_position,id.uniqueness

from user_indexes id,user_ind_columns ic

where id.table_name=ic.table_name and

ic.table_name ='EMPLOYEES'; //afiseaza indexurile la tabelul EMPLOYEES

create index upper_last_name_idx

on emp (upper(last_name)); //index indiferent de CASE

drop index upper_last_name_idx; //sterge un index

create synonym emp123

for employees; //creeaza un sinonim


create user user

identified by password 'password'; //creeaza un nou utilizator

alter user scott

identified by tiger; //schimba parola

grant create session,create table,create sequence,create view

to scott; //ofera privilegii

grant update (salary)

on emlpoyees to scott; //update privilege doar pentru salary

grant select

on alice.departments

to public; //toti potselecta din departments alice


create role manager; //creeaza un rol cu numele manager

grant create table,create view to manager; //ofera privilegii unui rol

grant manager to scott; //ofera rolul unui utilizator

grant update (salary)

on emlpoyees to scott

with grant option; //ii permite lui scott sa dea privilegiul mai departe

revoke select,inserit

on clients from scott; //retrage un privilegiu

create public synonym hq_emp

for emp@hq.acme.com; //creeaza un link catre o basa de data dinafara


select first_name,last_name

from employees

where regexp_like(firstname,'^Ste(v|ph)en$'); //similar cu like

select last_name, regexp_replace(last_name, 'H(a|e|i|o|u','**')

from employees; //inlocuieste vocala cu “**”


select country_name, regexp_count(country_name,'(ab)')

from wf_countries

where regexp_count(country_name, '(ab)')>0; //numarul de apartii ‘ab’ in nume

create table my_contacts

(first_name varchar2(20), last_name varchar2(20),

email varchar2(20) check(REGEXP_LIKE(email, '.+@.+\..+'))); //verifica daca email valid

SAVEPOINT one; //creeaza un savepoint

ROLLBACK TO SAVEPOINT; //intoarce pana la savepoint sau commit

COMITT; //commit toate datele

ROLLBACK; //pana la commit


REVIEW
Probleme

select nume, prenume


from elev
where lower(prenume) like '%ad%';

select SUBSTR(prenume,1,1) ||' '||nume as Elevi


from elev;

select table_name
FROM user_tables;

select min(nume),min(prenume)
from elev;

select nume,prenume,to_char(nota,'$999.99')
from elev
where nota between 7 and 10;

select e.nume,e.prenume, profil


from elev e,clasa c
where e.clasa=c.clasa;

select nume,prenume,nota_min||' - '||nota_max, nota


from elev,premiu
where nota between nota_min and nota_max;

select nume,prenume,decode(id_frate,null,'+','-') as "Frate/Sora"


from elev;

select nume,prenume,coalesce(clasa,grupa,'-1')
from elev;

select e.nume,e.prenume,c.nume,c.prenume
from elev e
full outer join clasa c on (e.clasa=c.clasa);

select level,nume,prenume,prior nume,prior prenume


from profesor
start with prof_id=1
connect by prior prof_id = sef_id;
select min(nota),max(nota),count(nota)
from elev;

select clasa,avg(nota)
from elev
group by clasa
having avg(nota)>6
order by avg(nota);

with media as
(select clasa,avg(nota) as med
from elev
group by clasa)

select c.clasa,c.nume||' '||c.prenume as Diriginte,m.med


from clasa c,media m
where c.clasa=m.clasa

with media as
(select clasa,avg(nota) as med
from elev
group by clasa)

select max(med)
from media;
Or

select clasa,avg(nota)
from elev
group by clasa
having avg(nota) >= all
(select avg(nota)
from elev
group by clasa);
Or

select max(avg(nota))
from elev
group by clasa;

select clasa,limba,avg(nota)
from elev
group by (clasa,limba);

select clasa,limba,avg(nota)
from elev
group by CUBE(clasa,limba);
select clasa,limba,avg(nota),
case grouping(clasa)
when 1 then 'Yes'
else 'No'
end as "Dupa clasa",
case grouping(limba)
when 1 then 'Yes'
else 'No'
end as "Dupa limba"
from elev
group by CUBE(clasa,limba);

select clasa,limba,grupa,avg(nota)
from elev
group by grouping sets ((clasa,limba),grupa);

select nume||' '||prenume as "Elevi",to_char(null) as "Profesori", to_char(null) as "Premii"


from elev
union
select to_char(null) ,nume||' '||prenume,to_char(null)
from clasa
union
select to_char(null),to_char(null),premiul
from premiu;

select e.nume,e.prenume, e.clasa,e.nota


from elev e
where e.nota>
(select avg(nota)
from elev c
where c.clasa=e.clasa);

select e.constraint_name,e.column_name,e.position,c.constraint_type
from user_cons_columns e,user_constraints c
where e.table_name='JOB_HISTORY' and e.table_name=c.table_name;

alter table emp


modify (employee_id number constraint employee_id_pk Primary Key);

alter table emp


modify (constraint department_id_fk foreign key (department_id)
references dept(department_id) on delete cascade);
select e.last_name,e.salary,d.department_id,q.salavg
from departments d,employees e, (select avg(salary) as salavg,department_id
from employees
group by department_id) q
where d.manager_id=e.employee_id and d.department_id=q.department_id and e.salary>q.salavg
order by d.department_id;

create or replace view V2


as select d.department_name as "Deparment Name", q.minsal as "Lowest Salary",q.maxsal as
"Highest Salary",q.avgsal as "Average Salary"
from dept d,(select min(salary) as minsal,max(salary) as maxsal,avg(salary) as avgsal,department_id
from employees
group by department_id) q
where d.department_id=q.department_id
order by d.department_name;

create or replace view Dept_Managers_view


as select department_name as "DEPT_NAME",substr(first_name,1,1) ||'.'||last_name as
"MGR_NAME"
from departments d, employees e
where d.manager_id=e.employee_id
with read only;

select LAST_NUMBER
from user_sequences
where sequence_name ='CT_SEQ';

insert into emp


(employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,commission_pct,
manager_id,department_id)
values
(ct_seq.nextval,'Kaare','Hansen','KHANSEN','4496582312','10/10/2016','Manager',6500,null,100,20)
;

create index emp_indx


on emp(upper(substr(first_name,1,1)||' '||last_name));

select table_name,comments
from dictionary
where regexp_count(lower(table_name),'(priv)')>0;

select employee_id,department_name
from employees,departments;

Vous aimerez peut-être aussi