Multi-line function
- multiple rows scope, returns a value
No |
name |
Types of |
description |
1 |
EMPNO |
NUMBER(4) |
No: Four-digit |
2 |
ENAME |
VARCHAR2(10) |
Name: 10 -digit character |
3 |
JOB |
VARCHAR2(9) |
position |
4 |
MGR |
NUMBER(4) |
No leadership: Leadership is the company employees |
5 |
HIREDATE |
DATE |
Entry date |
6 |
WILL |
NUMBER(7,2) |
Base salary, to two decimal places, five integer, a total of seven |
7 |
COMM |
NUMBER(7,2) |
Year-end awards |
8 |
DEPTNO |
NUMBER(2) |
Employee's department number |
Figure 1-1 ( emp employee table)
Grouping function: acting on a plurality of lines, a return value.
l count the number of records count (*); Note: This function ignores null value;
l the minimum value query min ();
l maximum query max ();
l Query average AVG ();
l summing function SUM ();
Group by press columns specified if the data into groups, then the set of data for multi-line statistical function.
Eg: the number of queries for each department: select deptno, count (*) from emp group by deptno;
Eg: the average salary for each department query: select deptno, avg (sal) from emp group by deptno;
- Such as the use of group function, SQL grouping criteria can only group by fields and check out grouping functions, there can be other fields.
Eg:select deptno,job,avg(sal) from emp group by deptno;--错误
Eg:select deptno,job,avg(sal) from emp group by deptno,job;--正确
- The grouping function is used, without using a group by a field, the value of the packet can only query functions;
Eg:select avg(sal) from emp;
Filtering data packets having
Eg: check out Sectors average salary greater than 2000, with having and where can achieve:
Select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
Select * from (Select deptno,avg(sal) sal from emp group by deptno )where sal>2000;
Eg: Query sector wages is greater than the sector average wage of 1500:
Select deptno,avg(sal) from emp where sal>1500 group by deptno;
Multi- table query
- multiple rows scope, returns a value
NO |
name |
Types of |
description |
1 |
DEPTNO |
NUMBER(2) |
It represents the department number by two digits |
2 |
DNAME |
VARCHAR2(14) |
Department name, of up to 14 characters |
3 |
PLACE |
VARCHAR2(13) |
Location department |
Figure 1-1 division table (dept)
No |
name |
Types of |
description |
1 |
EMPNO |
NUMBER(4) |
No: Four-digit |
2 |
ENAME |
VARCHAR2(10) |
Name: 10 -digit character |
3 |
JOB |
VARCHAR2(9) |
position |
4 |
MGR |
NUMBER(4) |
No leadership: Leadership is the company employees |
5 |
HIREDATE |
DATE |
Entry date |
6 |
WILL |
NUMBER(7,2) |
Base salary, to two decimal places, five integer, a total of seven |
7 |
COMM |
NUMBER(7,2) |
Year-end awards |
8 |
DEPTNO |
NUMBER(2) |
Employee's department number |
FIG. 1-2 ( EMP employee table)
What is a multi-table query
l a statement need to display data from multiple tables, you must apply to the operation of multi-table queries.
What is the Cartesian product?
l mathematical concept: the set of relational operations (the set of all combinations of relationships)
With realize sql Cartesian product: select * from emp e, dept d order by e.empno, e.deptno;
solution must issue invalid data Cartesian product ---- (en:
- Implicit within the connector: . From A, B = B WHERE A. associated field associated field
- Explicit inner connecting: .. From the Join Inner A B ON A relationship field associated field = b):
select * from emp e,dept d where e.deptno = d.deptno;--隐式
select * from emp e inner join dept d on e.deptno = d.deptno;--显式
to sum up
L The connection requires two data tables can meet the requirements of this display data.
l Question: Is there no department employees. To display information associated with the query department? ---- external connection
An outer connector ( in Oracle has a special written in: )
- Left outer: a reference table to the left, the left table are all displayed data, no data is displayed at right Null
From B Table left join A on a. The associated field = b. Relevance field
- Right external connection: the reference table to the right, the right table are all displayed data, no data is displayed left table Null
From a table right join b on a. The associated field = b. Relevance field
- External connection (there is a special written in Oracle), it has been left connected, for example
FROM A, B WHERE A. relevance field (+) = b related fields - such an approach is not recommended, because it is not common
Since query
- Left outer: a reference table to the left, the left table are all displayed data, no data is displayed at right Null
Eg: identify the employee's name, position, leadership Name: select * from emp e, emp m where e.mgr = m.empno;
Problem: no leadership, there is no employee information.
Solution: left join together
Select * from emp e left join emp m on e.mgr = m.empno;
Why recommended left the Join , right the Join , Inner the Join ON ?
- First, in terms of the outer connecting OARACLE specific (+) writing is not common, only for oracle.
- Left join on, right join on, inner join on the structure clearer, easier to debug sql.
- ( +) Of the primary and secondary table, and Left join on, right join on, inner join on a clear distinction between them.