SQL

From SkyWiki

(Difference between revisions)
Jump to: navigation, search
m (ANSI joins in Oracle)
(traditional Oracle outer join syntax)
Line 4: Line 4:
===traditional Oracle outer join syntax===
===traditional Oracle outer join syntax===
-
left outer join
+
inner join
<source lang=sql>
<source lang=sql>
 +
select ...
 +
from emp, dept
 +
where emp.deptno = dept.deptno
</source>
</source>
-
right outer join
+
left outer join
<source lang=sql>
<source lang=sql>
 +
select ...
 +
from emp, dept
 +
where emp.deptno(+) = dept.deptno
</source>
</source>
-
inner join
+
right outer join
<source lang=sql>
<source lang=sql>
 +
select ...
 +
from emp, dept
 +
where emp.deptno = dept.deptno(+)
</source>
</source>
-
 
===TOP n SELECT===
===TOP n SELECT===

Revision as of 16:10, 3 October 2008

ANSI joins in Oracle

Presentation about the ANSI syntax introduced in Oracle 9i

traditional Oracle outer join syntax

inner join

SELECT ...
FROM emp, dept
WHERE emp.deptno = dept.deptno

left outer join

SELECT ...
FROM emp, dept
WHERE emp.deptno(+) = dept.deptno

right outer join

SELECT ...
FROM emp, dept
WHERE emp.deptno = dept.deptno(+)

TOP n SELECT

MySQL:

SELECT EMPNO, ENAME, JOB, MGR, SQL
FROM   EMP
LIMIT 10 offset 20

Oracle >= 8.1.6:

SELECT * FROM (SELECT 
    EMPNO, ENAME, JOB, MGR, SQL,
    rank() over(partition BY JOB ORDER BY HIRE_DATE DESC 
) RN
FROM EMP
) 
WHERE RN BETWEEN 20 AND 30
 
SELECT EMPNO, ENAME, JOB, MGR, SAL,
    rank() over
      (ORDER BY SAL DESC NULLS LAST) AS RANK,
    dense_rank() over
      (ORDER BY SAL DESC NULLS LAST) AS DRANK
FROM EMP
ORDER BY SAL DESC NULLS last;

MS SQL Server:

SELECT top 10 EMPNO, ENAME, JOB, MGR 
FROM EMP

Sybase:

SET rowcount 10
SELECT COLUMNS
FROM TABLE

Firebird:

SELECT first 10 COLUMNS
FROM TABLE
Personal tools