All about DataSince, DataEngineering and ComputerScience
View the Project on GitHub datainsightat/DataScience_Examples
desc[ribe] table_name;
select *|{column_name1,column_name2,...} from table;
select first_name as name, last_name "Last Name", salary*12 "Annual Salary" from employees;
select q'[My Name is Steven]' my_text from dual;
select distinct job_id from employees;
The distinct operator ist row-based only. One distinct operator is used in one select statement.
select first_name || ' ' || last_name as "Names" from employees;
select employees_id, salary, (salary + 200) * 12 as "Annual Salary" from employees;
select sysdate + 3 from dual;
select *|{[distinct] column [alias],...}
from table
[where logical expressions(s)];
select first_name, last_name, salary
from employees
where salary between 12000 and 15000;
select employee_id, first_name, last_name, salary, manager_id
from employees
where employee_id in (100,102,105,200);
select first_name, last_name
from employees
where first_name like 'A%';
select first_name
from employees
where manager_id is null;
select first_name, lats_name, salary, job_id
from employees
where job_id = 'IT_PROG' and salary >= 5000;
select first_name, lats_name, salary, job_id
from employees
where job_id = 'IT_PROG' or salary >= 5000;
select first_name, lats_name, salary, job_id
from employees
where salary > 10000 and job_id not in ('SA_MAN','ST_CLERK','SH_CLERK');
select first_name, last_name
from employees
order by last_name;
select first_name, last_name
from employees
order by first_name desc, last_name asc;
select first_name, last_name
from employees
order by 1 desc nulls first, 2 asc nulls last;
select function_name [(arg1, arg2,...)] from dual;
select lower('Test') from dual; -> 'test'
select upper('Test') from dual; -> 'TEST'
select initcap('TEST') from dual; -> 'Test'
select substr('Sql Course',1,3) from dual; -> 'Sql'
select length('Sql Course') from dual; -> 10
select concat('Sql','Course') from dual; -> 'SqlCourse'
select instr('Sql Course','o') from dual; -> '6'
select trim(' Sql Course ') from dual; -> 'Sql Course'
select ltrim(' Sql Course ') from dual; -> 'Sql Course '
select rtrim(' Sql Course ') from dual; -> ' Sql Course'
select replace('Sql Course','s','*') from dual; -> 'Sql Cour*e'
select lpad('sql',10,'-'); -> '-------sql'
select rpad('sql',10,'-'); -> 'sql-------'
select round(12.136,2) from dual; -> 12.14
select trunc(12.136,2) from dual; -> 12.13
select ceil(2.67) from dual; -> 3
select floor(2.67) from dual; -> 2
select mod(1800,400) from dual; -> 200
select first_name, last_name lpad(upper(concat(first_name, last_name)),20,'*') nesting
from employees;
Current Year Specified Date RR YY
1990 13-Jun-93 1993 1993
1990 13-Jun-15 2015 2015
2015 13-Jun-15 2015 2015
2015 13-Jun-93 1993 2093
Subtracting two dates returns the number of days between these dates.
select tfirst_name, lats_name(sysdate - hire_date)/365 as "Working Perriod"
from employees;
create [unique|bitmap] index index_name
on table_name (column_name1[,column_name2,...])
We cannot create more than 1 unique index on the same column.
If the index contains more than 1 column, the where-clause needs to address all columns of the index.
create table employee_temp
(employee_id number(6) primary key
using index (create index temp_idx on employee_temp(employee_id)),
first_name varchar2(29),
last_name varchar2(25));
create table new_emp2
(employee_id number(6),
first_name varchar2(20),
last_name varchar2(25));
create index emp_id_idx2 on
new_emp2(employee_id);
alter table new_emp2
add primary key (employee_id)
using index emp_id_idx2;
create index first_name_idx on employees
(upper(first_name));
Index is active, if the propper function is used in the where clause.
select * from employees
where upper(first_name) = 'ALEX';
create index temp_idx on employees
(first_name, last_name);
alter index temp_idx invisible;
create bitmap index temp_idx on employees
(first_name, last_name);
drop index index_name;
To allow DML operations while the index is dropped use:
drop index index_name online;
select * from user_ind_columns
where table_name = 'EMPLOYEES'
order by index_name;
create [or replace] [force|noforce] view view_name
[(alias[,alias]...)] as subquery
[with check option [constraint contraint_name]]
[with read only [constraint constraint_name]];
Aliases
create view empvw20 as
select employee_id eid, first*name name, last*name surname
from employees
where department_id = 20;
create view empvw20 (eid, name, surname) as
select employee_id, first*name, last*name
from employees
where department_id = 20;
select * from user_views;
drop view view_name;
create materialized view view_name
build [immediate|deferred]
refresh [fast|complete|force] on [commit|demand]
[[enable|disable] query rewrite]
[on rebuilt table]
as subquery;
For fast refreshed materialized views, you need to create a log on the basetable
create materialized view log on table_name;
execute dbms_mview.refresh('view*name','F'|'C');
dbms_mview.refresh_all_mviews(failures,'C','',TRUE,FALSE,FALSE);
create materialized view view_name
build immediate
refresh force
start with sysdate + 3/24
next syssate + 5
as subquery
select employee_id, first*name from employees
where department_id = (select department_id from employees where upper(first_name) = 'LUIS');
select employee_id, first_name from employees
where department_id in (select department_id from employees where upper(first_name) = 'LUIS');
select first_name, department_id from employees
where (department_id, manager_id) in
(select department_id, manager_id from employees where upper(first_name) = 'LUIS');
select employee_id, department_id from employees a
where salary = (select max(salary) from employees b
where b.department_id = a.department_id);
select employee_id, department_id from employees a
where exists (select 1, employee_id from employees
where manager_id = a.employee_id);
select department_id, department_name
from departments d
where not exists
(select department_id from employees
where department_id = d.department_id);
with max_salaries as
(select department_id, max(salary from employees group by department_id)
select * from max_salaries;
with all_managers (Employee, Manager, Department) as
(select demployee_id, manager_id, department_id from employees
union all
select all_managers.employee, employees.manager_id, all_managers.department from all_managers, employees
where all_managers.manager = employees.employee_id)
select employee, manager, department from all_managers order by employee;
insert into (select d.department_id, d.department_name
from departments_temp d
join locations l
on (d.location_id = l.location_id)
where l.city = 'Oxford')
values (300, 'Marketing', );
select * from departments_temp;
update table1 alias1
set (column, column, ...) = (select column, column, ...
from table2 alias2
where alias1.column = alias2.column);
delete from table1 alias1
where column operator
(select expression
from table2 alias2
where alias1.column = alias2.column);
insert into departments_temp (department_id, department_name, manager_id)
values (310, 'Temp Department', DEFAULT);
update departments_temp set manager_id = DEFAULT;
alter departments_temp modify manager_id number(6) default 100;
insert all
into insert_statement
into insert_statement
...
subquery;
insert all
into employees_history values (employee_id, first_name, last_name, hire_date)
into salary_history values (employee_id, extract(year from sysdate), extract(month from sysdate), salary, commission_pct)
select * from emplyees where hire_date > sysdate - 365;
insert all
when condition then
into insert_statement
...
when condition then
into insert_statement
...
subquery;
insert all
when hire_date > sysdate-365 then
into employees_history values(employee_id,first_name,last_name,hire_date)
into salary_history values(employee_id,extract(year from sysdate),extract(month from sysdate),salary,commission_pct)
when job_id = 'IT_PROG' then
into it_progammers values(employee_id,first_name,last_name,hire_date)
when department_id in
(select department_id from departments where location_id in
(select location_id from locations where country_id = 'US')) then
into living_in_us values (employee_id, first_name, last_name, hire_date)
select * from employees;
insert first
when condition then
into insert_statement
...
when condition then
into insert_statement
...
else
into insert_statement
...
subquery;
insert first
when salary < 5000 then
into low_salaries values(employee_id, department_id, salary)
when salary between 5000 and 10000 then
into average_salaries values(employee_id, department_id, salary)
else
into high_salaries values(employee_id, department_id, salary)
select * from employees;
job_salaries table: year | month | it_prog | sa_man | st_man
job_sal table: year | month | job_id | total_salary
insert all
into job_sal values (year,month,'IT_PROG',it_prog)
into job_sal values (year,month,'SA_MAN',sa_man)
into job_sal values (year,month,'ST_MAN',st_man)
select * from job_salaries;
merge into table_name table_alias
using (table|view|subquery) alias
on (join condition)
when matched then
update set
column_name1 = value1,
column_name2 = value2
when not matched then
insert (columns)
value (values);
merge into employees_copy c
using (select * from employees) e
on (c.employee_id = e.employee_id)
when matched then
update set
c.first_name = e.first_name
c.last_name = e.last_name
c.department_id = e.department_id
c.job_id = e.job_id
c.salary = e.salary
delete where department_id is null
when not matched then
insert values (e.employee_id, e.first_name, e.last_name, e.department_id, e.job_id, e.salary)
alter table employees_copy enable row movement;
select * from recyclebin;
flashback table [schema.] table
to {scn|timestamp} expr |
restore point restore_point [{enable|disable} triggers] |
before drop [rename to table];
flashback table employees_copy to timestamp sysdate - 1 / (24 * 60);
Get Systemchange number select dbms_flashback.get_system_change_number as sc from dual;
flashback table employees_copy to timestamp scn 10194783;
drop table employees_copy purge;
purge recyclebin;
select salary from employees_copy
where employe_id = 100;
update employees_copy set salary = 1000
where employee_id = 100;
select * from employees_copy
as of timestamp(sysdate - interval '2' minute)
where employee_id = 100;
select salary from employees_copy
where employe_id = 100;
update employees_copy set salary = 1100
where employee_id = 100;
select * from employees_copy
versions between scn minvalue and maxvalue
where employee_id = 100;
select * from employees_copy
versions between timestamp(sysdate - interval '13' minue) and sysdate
where employee_id = 100;
alter table table_name
add [constraint <constraint_name>]
type [<column_name>];
alter table employees_temp
add constraint temp_cons unique (employee_id);
alter table employees_temp
add unique (employee_id);
alter table employees_temp
modify job_id constraint not_null_job not null;
alter table employees_temp
modify first_name not null;
alter table table_name drop constraint constraint_name;
The cascade option when dropping a primary key is used to drop all related foreign keys. alter table employees_temp drop primary key cascade;
alter table employees_temp
drop constraint constraint name
online;
alter table salary_history
add constraint sal_emp_fk foreign key (employee_id)
references employees_temp(employee_id)
on delete cascade;
alter table high_salaries
add constraint hi_emp_fk foreign key (employee_id)
references employees_temp(employee_id)
on delete set null;
alter table emplyoees_temp
drop column employee_id
cascade constraints;
alter table table_name rename constraint constraint_name to new_constraint_name;
alter table table_name
disable constraint constraint_name;
alter table table_name
enable constraint constraint_name;
Novalidate (No conformity check on existing data)
alter table employees_temp enable novalidate constraint constraint_name;
not deferrable
alter table dep_tmp add constraint dep_id_pk primary_key(department_id) deferrable initially deferred;
set constraints dep_id_pk immediate;
set constraints dep_id_pk deferred;
set constraints all immediate;
set constraints all deferred;
alter session set constraints = immediate;
create global temorary table shopping_carts
(id number,
type varchar2(20),
shopping_date(date)
on commit delete rows;
create user temp_user
identified by 123;
select * from system_privilege_map;
grant privilege [,privilege...]
to user [user|role,public...];
grant create session, create table, create view to temp_user;
create role developer;
grant create table, create view, create session, create trigger to developer;
grant developer to user;
alter user temp_user
identified by 123;
grant object_privileges [columns]
on object_name to {user|role|public}
[with grant option];
grant select, update on employees_temp to temp_user, developer;
grant update(first_name, last_name) on employees_temp to temp_user;
revoke {privilege [,privilege...]|all}
on object_name
from {user [,user...]|role|public}
[cascade constraints];
revoke select, insert on employees_temp from temp_user;
select [level], column_name, expression from table_name
[where condition]
[start with condition]
[connected by prior condition];
select employee_id, first_name, last_name, job_id, manager_id
from employees
start with employee_id = 102
connect by prior employee_id = manager_id;
select level, employee_id, first_name, last_name, job_id, manager_id
from employees
where level = 2
start with employee_id = 101
connect by manager_id = prior employee_id;
select lpad(last_name, length(last_name)+(level*2)-2,'> ') as employees_hierarchy
from employees
start with employee_id = 101
connect by prior employee_id = manager_id;
select employee_id, last_name, manager_id
from employees
start with employee_id = 101
connect by prior employee_id = manager_id
and employee_id <> 108;
select 'create table ' || table_name || '_backup ' || 'as select * from ' || table_name || ';' as "Backup Scripts"
from user_tables;
Copy & Paste results in SQL Developer and execute queries.
regexp_like (string, pattern [,match_option]);
Match-Option:
‘m’: Source string has multiple lines
select first_name, last_name from employees where regexp_like (first_name, ‘^Ste(v|ph)en$’);
regexp_replace (string, pattern [,str_to_replace] [,postition] [,occurence] [,match_option]);
regexp_substr (string, pattern [,postition] [,occurence] [,match_option]);
regexp_instr (string, pattern [,postition] [,occurence] [,return_option] [,match_option]);
regexp_count (string, pattern [,postition] [,match_option]);
function (arg1, ...) over ([partition by ...] [order by ...] [windowing clause]);
LEAD, LAG, RANK, DENSE_RANK, ROW_NUMBER, FIRST, FIRST VALUE, LAST, LAST_VALUE, …
select employee_id, department_id, hire_date, row_number() over (partition by department_id order by hire_date nulls last) row_number
from employees
where department_id in (10,20,30)
order by department_id;
select employee_id, department_id, hire_date,
rank() over (partition by department_id order by salary desc nulls last) rnk,
dense_rank() over (partition by department_id order by salary desc nulls last) dns_rnk
from employees
where department_id in (10,20,30)
order by department_id;
lead|lag column_name, offset, default over analytic_clause;
select employee_id, department_id, salary,
lead(salary,1,0) over (partition by department_id order by salary nulls last) next_salary,
lag(salary,1,0) over (partition by department_id order by salary nulls last) previous_salary
from employees
where department_id in (10,20,30)
order by department_id;
Start_point & End_point:
value | expression preceding |
value | expression following |
select employee_id, department_id, salary, sum(salary) over (partition by department_id order by salary rows between 0 preceding and 2 following) windowing from employees where department_id in (10,20,30) order by department_id, salary;
select employee_id, department_id, salary, sum(salary) over (partition by department_id order by salary range between 100 preceding and 100 following) windowing from employees where department_id in (10,20,30) order by department_id, salary;
select employee_id, department_id, salary, hire_date,
sum(salary) over (partition by department_id order by hire_date range between interval '1' year preceding and interval '1' year following) sums
from employees
where department_id in (10,20,30)
order by department_id, hire_date;
first_value(column_name) [respect|ignore} nulls over (analytic_clause);
select employee_id, department_id, salary
first_value(salary) ignore nulls over (partition by department_id order by salary) as low_sal,
last_value(salary) ignore nulls over (partition by department_id order by salary range between unbound preceeding and unbound following) as high_sal,
from employees
where department_id in (10,20,30)
order by department_id, salary;
select * from
(select employee_id, department_id, salary,
row_number() over (partition by department_id order by salary desc) row_number
from employees where department_id in (10,20,30))
where row_num <= 4;