references = foreign key
equijoin = inner join
http://docs.oracle.com/cd/E11882_01/appdev.112/e17125/adfns_regexp.htm#ADFNS232
Precedence:
1. Arithmetic (pemdas)
2. Concat
3. Comparisons
4. is null, like, between
5 not between
6. not equal
7. not
8. AND
9. OR
Read consistency is an automatic implementation. It keeps a partial copy of the database in
the undo segments. The read-consistent image is constructed from the committed data in the
table and the old data that is being changed and is not yet committed from the undo segment.
When an insert, update, or delete operation is made on the database, the Oracle server takes
a copy of the data before it is changed and writes it to an undo segment.
All readers, except the one who issued the change, see the database as it existed before the
changes started; they view the undo segment’s “snapshot” of the data.
The relationship between the EMPLOYEES and DEPARTMENTS tables is
an equijoin; that is, values in the DEPARTMENT_ID column in both tables must be equal.
Often, this type of join involves primary and foreign key complements.
Note: Equijoins are also called simple joins or inner joins.
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM
employees e, departments d
WHERE e.department_id = d.department_id;
SELECT d.department_id, d.department_name, l.city
FROM
departments d, locations l
WHERE
d.location_id = l.location_id
AND d.department_id IN (20, 50);
NATURAL JOIN
SELECT department_id, department_name,
location_id, city
FROM
departments NATURAL JOIN locations ;
USING
SELECT employee_id, last_name,
location_id, department_id
FROM
employees JOIN departments USING (department_id) ;
ON
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM
employees e JOIN departments d
ON
(e.department_id = d.department_id);
NONEQUIJOINS
SELECT e.last_name, e.salary, j.grade_level
FROM
employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
SELECT e.last_name, e.salary, j.grade_level
FROM
employees e JOIN job_grades j
ON
e.salary BETWEEN j.lowest_sal AND j.highest_sal;
The outer join operator is the plus sign (+)
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e, departments d
WHERE e.department_id(+) = d.department_id ;
SELECT e.last_name, d.department_id, d.department_name
FROM
employees e RIGHT OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
Self join
SELECT worker.last_name || ‘ works for ‘
|| manager.last_name
FROM
employees worker, employees manager
WHERE worker.manager_id = manager.employee_id ;
UNDO
(5 minut temu)
select * from testable as of timestamp (SysDate – 5/1440)
select * from testtable as of timestamp(’08-Aug-2011 5:00:00 PM’,'DD-MON-YYYY HH:MI:SS AM’);
flashback table testtable to before drop;
NATURAL JOIN
SELECT employee_id, last_name,
department_name
FROM
hr.employees JOIN hr.departments
USING (department_id) ;
SELECT l.city, d.department_name
FROM
locations l JOIN departments d USING (location_id)
WHERE location_id = 1400;
SELF JOIN
Sometimes you need to join a table to itself. To find the name of each employee’s manager,
you need to join the EMPLOYEES table to itself, or perform a self-join.
SELECT worker.last_name emp, manager.last_name mgr
FROM
employees worker JOIN employees manager
ON
(worker.manager_id = manager.employee_id);
NONEQUIJOINS
To return the unmatched rows, you can use
an OUTER join. An OUTER join returns all rows that satisfy the join condition and also returns
some or all of those rows from one table for which no rows from the other table satisfy the join
condition.
There are three types of OUTER joins:
•LEFT OUTER
•RIGHT OUTER
•FULL OUTER
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e LEFT OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
This query retrieves all the rows in the EMPLOYEES table, which is the left table, even if there
is no match in the DEPARTMENTS table.
SELECT e.last_name, d.department_id, d.department_name
FROM
employees e RIGHT OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
This query retrieves all the rows in the DEPARTMENTS table, which is the table at the right,
even if there is no match in the EMPLOYEES table.
SELECT e.last_name, d.department_id, d.department_name
FROM
employees e FULL OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
This query retrieves all rows in the EMPLOYEES table, even if there is no match in the
DEPARTMENTS table. It also retrieves all rows in the DEPARTMENTS table, even if there is no
match in the EMPLOYEES table.
——— CARTESIAN PRODUCT ————
SELECT last_name, department_name
FROM
employees CROSS JOIN departments ;
Here’s a handy query for finding duplicates in a table. Suppose you want to find all email addresses in a table that exist more than once:
SELECT email, COUNT(email) AS NumOccurrences FROM users GROUP BY email HAVING ( COUNT(email) > 1 )
You could also use this technique to find rows that occur exactly once:
SELECT email FROM users GROUP BY email HAVING ( COUNT(email) = 1 )
Źródło: http://www.petefreitag.com/item/169.cfm
CREATE TABLE HR.JS_TAB1(MIASTO VARCHAR2(20), ULICA VARCHAR2(20));
SELECT * FROM HR.JS_TAB1;
CREATE TABLE HR.JS_TRANS1(ULICA_Z VARCHAR2(30), ULICA_NA VARCHAR(30));
SELECT * FROM HR.JS_TRANS1;
INSERT INTO HR.JS_TAB1 VALUES(‘GDYNIA’,'NAGIETKOWA’);
INSERT INTO HR.JS_TAB1 VALUES(‘GDYNIA’,'SWIETOJANSKA’);
INSERT INTO HR.JS_TAB1 VALUES(‘GDANSK’,'KOLOBRZESKA’);
INSERT INTO HR.JS_TAB1 VALUES(‘GDANSK’,'MALBORSKA’);
INSERT INTO HR.JS_TRANS1 VALUES (‘KOLOBRZESKA’,'CHLOPSKA’);
INSERT INTO HR.JS_TRANS1 VALUES (‘SWIETOJANSKA’,'WLADYSLAWA IV’);
SELECT MIASTO, ULICA FROM HR.JS_TAB1;
SELECT MIASTO,
Nvl((SELECT ULICA_NA FROM HR.JS_TRANS1 TRAN1 WHERE TAB1.ULICA = TRAN1.ULICA_Z),TAB1.ULICA)
FROM HR.JS_TAB1 TAB1;
Strona startowa: www.jaceksen.pl