real-world performance training - oracle · title: how to use the powerpoint template author:...

28

Upload: phammien

Post on 08-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Real-World Performance TrainingSQL Introduction

Real-World Performance Team

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL

• SQL

– Structured Query Language

– Declarative• You express what you want to do, not how to do it

– Despite the name, provides ability to modify (create, update, delete) data too

Basics

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Basic Anatomy of a SQL Statement

SQL

select dname, job, avg(sal), max(sal), count(*)

from emp e

join dept d

on d.deptno = e.deptno

where job != 'CLERK'

group by dname, job

having avg(sal) > 1500

order by dname, avg(sal)

Select listTables

Table Aliases

Join

Predicate

Aggregation

HavingSorting

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Sub-query ( correlated )

SQL

select empno, ename, sal

from emp e1

where sal > (

select avg(sal)

from emp e2

where e2.deptno = e1.deptno

group by deptno

)

Sub-query

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Inline View

SQL

select empno, ename, sal, avg_sal

from emp e

join ( select deptno

, avg(sal) avg_sal

from emp

group by deptno

) v

on e.deptno = v.deptno

InlineView

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Common Table Expression (CTE)

SQL

with

avg_sal as

( select deptno

, avg(sal) avg_sal

from emp

group by deptno

)

select empno, ename, sal, avg_sal

from emp e

join avg_sal a

on e.deptno = a.deptno

CommonTable Expression

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Join

SQL

from A

join B

on A.col = B.col

A B

Inner Join

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

EMP

ENAME JOB DEPTNO

CLARK MANAGER 10

KING PRESIDENT 10

FORD ANALYST 20

WARD SALESMAN 30

BILLY MECHANIC 50

DEPT

DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

9

SQLExample Tables

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Inner Join

SQL

SELECT e.ename,

e.job,

e.deptno,

d.dname,

d.loc

FROM emp e

JOIN dept d

ON e.deptno = d.deptno;

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL

RESULTS

ENAME JOB DEPTNO DNAME LOC

CLARK MANAGER 10 ACCOUNTING NEW YORK

KING PRESIDENT 10 ACCOUNTING NEW YORK

FORD ANALYST 20 RESEARCH DALLAS

WARD SALESMAN 30 SALES CHICAGO

Inner Join Results

11

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Outer Joins

SQL

A B

from A

full outer join B

on A.col = B.col

Full Outer Join

from A

left outer join B

on A.col = B.col

A B

Left Outer Join

from A

right outer join B

on A.col = B.col

Right Outer Join

A B

12

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Outer Joins

SQL

SELECT e.ename,

e.job,

e.deptno,

d.dname,

d.loc

FROM emp e

LEFT/FULL/RIGHT OUTER JOIN dept d

ON e.deptno = d.deptno;

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL

INNER JOIN

ENAME JOB DEPTNO DNAME LOC

CLARK MANAGER 10 ACCOUNTING NEW YORK

KING PRESIDENT 10 ACCOUNTING NEW YORK

FORD ANALYST 20 RESEARCH DALLAS

WARD SALESMAN 30 SALES CHICAGO

Inner Join Results

14

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL

LEFT OUTER JOIN

ENAME JOB DEPTNO DNAME LOC

CLARK MANAGER 10 ACCOUNTING NEW YORK

KING PRESIDENT 10 ACCOUNTING NEW YORK

FORD ANALYST 20 RESEARCH DALLAS

WARD SALESMAN 30 SALES CHICAGO

BILLY MECHANIC 50

Left Outer Join Results

15

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL

RIGHT OUTER JOIN

ENAME JOB DEPTNO DNAME LOC

CLARK MANAGER 10 ACCOUNTING NEW YORK

KING PRESIDENT 10 ACCOUNTING NEW YORK

FORD ANALYST 20 RESEARCH DALLAS

WARD SALESMAN 30 SALES CHICAGO

40 OPERATIONS BOSTON

Right Outer Join Results

16

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL

FULL OUTER JOIN

ENAME JOB DEPTNO DNAME LOC

CLARK MANAGER 10 ACCOUNTING NEW YORK

KING PRESIDENT 10 ACCOUNTING NEW YORK

FORD ANALYST 20 RESEARCH DALLAS

WARD SALESMAN 30 SALES CHICAGO

40 OPERATIONS BOSTON

BILLY MECHANIC 50

Full Outer Join Results

17

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Set Operators

SQL

select * from A

union

select * from B

select * from A

minus

select * from B

select * from A

intersect

select * from B

Union IntersectMinus

A

B

A

B

A

B

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function

SQL

• Generally a window function has a number of components

– An analytic function itself• eg. sum(), avg(), stddev(), rank

– A partitioning clause• To logically divide the data into sets

– A windowing clause• Defines the set of rows for the function to work on, with respect to the “current row”

– Order by clause• Defines the order of the rows within the partition or window

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function

SQL

select dname, ename, sal

, rank()

over (

partition by e.deptno

order by sal desc

) sal_rank

from emp e

join dept d

on e.deptno = d.deptno

order by dname, sal_rank

analytic function

partition by clause

order by clause forfunction

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function

SQL

deptno=10

deptno=20

deptno=30

partition by

Rows sorted andRANKed within eachpartition

1

23

3

2

1

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function – rank()

SQL

DNAME ENAME SAL SAL_RANK

-------------- ---------- ---------- ----------

ACCOUNTING KING 5000 1

ACCOUNTING CLARK 2450 2

ACCOUNTING MILLER 1300 3

RESEARCH FORD 3000 1

RESEARCH SCOTT 3000 1

RESEARCH JONES 2975 3

RESEARCH ADAMS 1100 4

RESEARCH SMITH 800 5

SALES BLAKE 2850 1

SALES ALLEN 1600 2

SALES TURNER 1500 3

SALES WARD 1250 4

SALES MARTIN 1250 4

SALES JAMES 950 6

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function

SQL

select dname, ename, hiredate, sal

, avg(sal) over (

partition by e.deptno

order by hiredate

rows between 2 preceding

and 2 following

) mv_avg_sal

from emp e

join dept d

on e.deptno = d.deptno

order by dname, hiredate

analytic function

partition by

order by forfunction

windowingclause

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function

SQL

Start of “window”

Current row

End of “window”

deptno=10

deptno=20

deptno=30

partition by

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

Window Function – moving average

SQL

DNAME ENAME HIREDATE SAL MVG_AVG_SAL

-------------- ---------- --------- ---------- -----------

ACCOUNTING CLARK 09-JUN-81 2450 2916.66667

ACCOUNTING KING 17-NOV-81 5000 2916.66667

ACCOUNTING MILLER 23-JAN-82 1300 2916.66667

RESEARCH SMITH 17-DEC-80 800 2258.33333

RESEARCH JONES 02-APR-81 2975 2443.75

RESEARCH FORD 03-DEC-81 3000 2175

RESEARCH SCOTT 09-DEC-82 3000 2518.75

RESEARCH ADAMS 12-JAN-83 1100 2366.66667

SALES ALLEN 20-FEB-81 1600 1900

SALES WARD 22-FEB-81 1250 1800

SALES BLAKE 01-MAY-81 2850 1690

SALES TURNER 08-SEP-81 1500 1560

SALES MARTIN 28-SEP-81 1250 1637.5

SALES JAMES 03-DEC-81 950 1233.33333

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |

SQL Definitions

Query Retrieves data based on specific criteria

DML Data Manipulation Language

SQL used to create, update and delete data

DDL Data Definition Language

SQL to create, alter objects such as tables, views, indexes etc

Basics

26

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |