37
V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

Embed Size (px)

Citation preview

Page 1: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0 OE NIK 2013 1

DBMAN5

ViewsRownum/LIMITExamples

Page 2: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

SELECTDisplayed order of suffixes

1. INTO2. FROM3. WHERE4. GROUP BY5. HAVING6. UNION/MINUS7. INTERSECT8. ORDER BY

2OE NIK 2013

Page 3: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0 OE NIK 2013 3

DBMAN5

ViewsRownum/LIMITExamples

Page 4: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Views• CREATE VIEW {name} AS {subquery};• CREATE [OR REPLACE] …; DROP VIEW;• To avoid or shorten sub-queries

Queries can be „modularized”, split into parts Queries can be easier to check and verify One view for a specific task (e.g. create a list), which

can be re-used later It's possible to save big joins as one view name

4OE NIK 2013

Page 5: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Views

• CREATE [OR REPLACE] VIEW …; • It can contain only SELECT commands!• It can be used mostly as SELECT … FROM {viewname}

it is mostly used as a source, not as a target – BUT it is possible to use it as DML (update / delete / insert) target (if „updatable view” then directly, otherwise using an „instead of” trigger)

5OE NIK 2013

Page 6: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Views• create or replace view NumOfWorkers AS select mgr as

BossID, count(*) as Num from emp group by mgr;• select ename, Num from emp, NumOfWorkers where

empno=BossID;• drop view NumOfWorkers;• As the emp changes the view changes too, because

the view's inner select is only evaluated when the big select is executed

• Create table vs Create view

6OE NIK 2013

Page 7: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Views

• Generally speaking: views don't make queries faster, only simpler to write and to understand

• We will never write queries where speed matters that much

• If optimization is needed: „Materialized view” (Oracle/Sybase/DB2) , „Indexed view” (MSSQL)

• PostgreSQL: 9.3+ (2013. september)• MySQL: Only solvable with trigger

7OE NIK 2013

Page 8: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Views + Order By

• SQL> create or replace view MyView as select * from emp order by ename;ORA & MySQL View created.

• SQL> create or replace view MyView as (select * from emp order by ename);ORA ORA-00907: missing right parenthesisMySQL View created

• SQL> create or replace view MyView as ( select * from (select * from emp order by ename) );ORA View created.MySQL ERROR subquery in the FROM???

8OE NIK 2013

Page 9: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0 OE NIK 2013 9

DBMAN5

ViewsRownum/LIMITExamples

Page 10: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum• Oracle solves the initial problem by assigning a number

for every result-row, this is the hidden field ROWNUM • It can be used in the WHERE:

select ename, rownum from emp where rownum<=3 order by ename;

• The ORDER BY is behind the WHERE It won't work as expected Sub-queries are required!!!

10OE NIK 2013

Page 11: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum

• SELECT rownum, ename FROM (select ename from emp order by ename) WHERE rownum<=3;

• SELECT rownum as order_num, ename, physical_num FROM (select rownum as physical_num, ename from emp order by ename) WHERE rownum<=3;

• WHERE rownum>1 ? WHERE rownum>=10 AND rownum<20 ?

11OE NIK 2013

Page 12: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum

• SELECT rownum, ename FROM (select ename from emp order by ename) WHERE rownum>1;

EMPTY output, the condition will never be true…

• SELECT * FROM (select ename, rownum as order from (select ename from emp order by ename) sub) WHERE order>=3 and order<6;

12OE NIK 2013

Page 13: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum + View

• create or replace view my_order as (select rownum as order, sub.* from (select * from emp order by ename) sub);

• select * from my_order where order=6;

• select * from my_order where order>=3 and order<6;

13OE NIK 2013

Page 14: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum?• It is only mentioned in the SQL:2003 standard: window

functions (analytical functions)• This is the most implementation-dependent part of the

SQL language…

Only the knowledge of the ORACLE

dialect is expected!!!

Now, only some google-compatible keywords…14OE NIK 2013

Page 15: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum - numbering

• MSSQL: select rownum=identity(int,1,1), ename into #temp from emp order by ename;select ename from #temp where rownum between {start} and {end};

• SYBASE: select rownum = identity(3), ename into #tempA from emp order by ename;select ename from #tempA where rownum between {start} and {end};

15OE NIK 2013

Page 16: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum – numbering, MySQL

• set @num = 0;SELECT emp.*, @num := (@num + 1) as row_number from emp;

• set @num = 0;SELECT emp.ename, emp.sal, @num := (@num + 1) as row_number from emp order by emp.ename WHERE row_number between {start} and {end};

16OE NIK 2013

Page 17: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum - filtering

• MSSQL / SYBASE: set rowcount {num}• SYBASE: select TOP {length} START AT {start} ename

from emp order by ename;• MSSQL: select TOP {length} ename from emp order by

ename OFFSET {start}; (2012)

• Postgresql, MySQL: select ename from emp order by ename LIMIT {start},{length}

[= LIMIT X OFFSET Y – Sybase és SQLite is]

17OE NIK 2013

Page 18: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Rownum – Exotic approaches

• INGRES: SELECT FIRST 10 * from T;• INFORMIX: SELECT SKIP 20 FIRST 10 * FROM T order

by c, d;• INTERBASE, FIREBIRD: SELECT FIRST 10 SKIP 20 *

FROM T;• FIREBIRD: SELECT * FROM T ROWS 20 TO 30;

18OE NIK 2013

Page 19: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Standards• SQL:2008 ( DB2, Sybase, PostgreSQL )

SELECT * FROM EMP FETCH FIRST 10 ROWS ONLY;• SQL:2003 ( SQL SERVER 2005, Oracle 8i (2000),

PostgreSQL 8.4 (2009) )SELECT ROW_NUMBER() OVER (ORDER BY ENAME ASC) AS RNUM, ENAME FROM EMP;

19OE NIK 2013

Page 20: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0 OE NIK 2013 20

DBMAN5

ViewsRownum/LIMITExamples

Page 21: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Example

Display the two people with the lowest salaries in every departments. In addition, display the salary rate of the department: which is 15% of the difference between the biggest and the smallest salary in a department

21OE NIK 2013

Page 22: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Necessary data• The difference between the minimum and the maximum

salary in the departments Data1 This will be later used in the JOIN of the big SELECT

• The two people with the lowest salaries in the departments Data2 This will be later used in the WHERE of the big SELECT

22OE NIK 2013

Page 23: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data1

• CREATE OR REPLACE VIEW data1 AS SELECTmin(sal) as Minimum,max(sal) as Maximum,max(sal)-min(sal) as Delta,0.15*(max(sal)-min(sal)) as DeltaPct,deptno FROM emp GROUP BY deptno;

• select * from data1;

23OE NIK 2013

Page 24: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2

• The two people with the lowest salaries … Too deep subquery chain (which can be avoided, but still…)

• IF (!!!) possible, then search for an alternative!

• Question: how many people are in a department who earn LESS than the person who has the minimum salary in the department?

• Question: how many people are in a department who earn LESS than the person who earns the second to minimum salary in the department?

24OE NIK 2013

Page 25: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2CREATE OR REPLACE VIEW data2 ASSELECT empno FROM emp outerQWHERE

(SELECT count(*) FROM emp innerQWHERE

(innerQ.sal<outerQ.sal) and (innerQ.deptno=outerQ.deptno))<=1;

25OE NIK 2013

Page 26: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Solution• SELECT ename, sal, emp.deptno, deltapct

FROM emp, data1WHERE emp.deptno=data1.deptno AND

empno IN (select * from data2);

• SELECT ename, sal, emp.deptno, deltapct FROM emp, data1, data2WHERE emp.deptno=data1.deptno AND

emp.empno=data2.empno;

26OE NIK 2013

Page 27: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Alternative solution

• Aim:CREATE OR REPLACE VIEW data2 AS

SELECT empnoFROM sal_order_in_deptWHERE order<=2;

So: We shall use row numbering…

27OE NIK 2013

Page 28: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2 alternative (rownum, Oracle)

• Global order:CREATE OR REPLACE VIEW deptsal AS(SELECT rownum as dsal, al.* FROM (select * from emp order by deptno,sal) al);

• From every department, we need the first two– Advanced function: ROW_NUMBER() OVER (ORDER

BY sal PARTITION BY deptno)– Now:

1 + {number} - {minimum_number_of_department}

28OE NIK 2013

Page 29: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2 alternative (rownum, Oracle)

• Order within the departments:

CREATE OR REPLACE VIEW deptsal2 AS (SELECT a.*,(1+a.dsal-

(select min(dsal) from deptsal bwhere b.deptno=a.deptno)

) AS dsal2FROM deptsal a

);

29OE NIK 2013

Page 30: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Solution (rownum, Oracle)

• CREATE OR REPLACE VIEW data2 ASSELECT empno FROM deptsal2 WHERE dsal2<=2;

• SELECT ename, sal, emp.deptno, deltapct FROM emp, data1, data2WHERE emp.deptno=data1.deptno AND

emp.empno=data2.empno;

different number of rows (6 instead of 7)!!!(cause: two people with the smallest salary VS people with

the two smallest salaries)

30OE NIK 2013

Page 31: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2 alternative (rownum, MySQL)

• set @num = 0;create or replace view deptsal as

SELECT empno, deptno, sal, @num := (@num + 1) as row_number

FROM emp ORDER BY deptno, sal;

• View's SELECT contains a variable or parameter

• Variable + UPDATE/VIEW BLOOD, SWEAT, TEARS… Or stored procedure … Or use lame tables instead of views?

31OE NIK 2013

Page 32: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2 alternative (rownum, MySQL)

drop table if exists deptsal;

set @num = 0;create table deptsal as

SELECT empno, deptno, sal, @num := (@num + 1) as dsal

FROM emp ORDER BY deptno, sal;

select * from deptsal;

32OE NIK 2013

Page 33: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Data2 alternative (rownum, MySQL)

drop table if exists deptsal2;

create table deptsal2 AS SELECT a.*,

( 1+a.dsal-(select min(dsal) from deptsal bwhere b.deptno=a.deptno) ) AS dsal2

FROM deptsal a;

select * from deptsal2;33OE NIK 2013

Page 34: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

Solution (rownum, MySQL)

• CREATE OR REPLACE VIEW data2 ASSELECT empno FROM deptsal2 WHERE dsal2<=2;

• SELECT ename, sal, emp.deptno, deltapct FROM emp, data1, data2WHERE emp.deptno=data1.deptno AND

emp.empno=data2.empno;

Hey, this slide is the same as "Solution (rownum, Oracle)" – that's why we use VIEWS!

34OE NIK 2013

Page 35: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0

EXERCISE• Display every worker’s name, their department’s name,

their boss’ name. The list should include the income parameter for both the boss and the worker.

• Income parameter: (income) – (the average income of those who entered the company the same year)

35OE NIK 2013

Page 36: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

V 1.0 OE NIK 2013 36

Page 37: V 1.0 OE NIK 2013 1 DBMAN 5 Views Rownum/LIMIT Examples

37OE NIK 2013