Oracle neutron query, paging query, sorting function popular understanding

1. Subquery
1. Subquery in the where clause
(1) Example: Find employees with the same position as SCOTT
Thought analysis:
1. How is it with xx, how is it with xx, how is it with xx, greater than, less than, equal to , yes, no, for, and other large-scale filtering conditions: must be used where
2. Where should be used in the same position as SCOTT, and the same position in SCOTT? We still don't know what SCOTT is. First, we need to query the SCOTT. This is a subquery.
3. The focus is on the employees at the back. What we ultimately want to query is the employees, not the same position as the SCOTT, so it is a main query.
4. In fact, the where condition filter is followed by real things,
(1) such as where sal > 100 100 is a real thing
(2) such as where job = SCOTT same position, because we need to know that SCOTT same position is real There is something that exists, but we really don’t know what it is now, that is to say, it is not real, so we have to find out what it is and turn it into something real.
select job from emp where ename = 'SCOTT';
5. Therefore, the content in the select query in front of where is to be on the table, it is the main query, and the condition following where must be a real thing, if not, Then query it out and turn it into a real thing, so what needs to be turned into a real thing through the query after where is a subquery.
6. Use where job = statement between the main query and the sub query.

(2) SQL query statement: 1.
The sub query after where is
select job from emp where ename = 'SCOTT';
2. The main query select
select ename, job from emp;
3. The main query and sub query combine
select ename, job from emp where job = (select job from emp where ename = 'SCOTT');
Query result:
write picture description here
In fact, the SQL statement in 3. has been able to successfully query the result. But see 4. Instructions below.

4. The subquery is a where filter condition, which has been fixed. There is a job in the main query, and there are jobs in the subquery. The two must be distinguished. Otherwise,
who knows whether your job is the job to be queried in the main query or the job in the subquery.
To sum up: the sub query is fixed, the main query prevents confusion, and improves.
In this way, the SQL statement structure is more reasonable and comfortable. e is equivalent to an instance of emp, and it is recommended to use 4 in the future, although 3 is also correct.

select e.ename,e.job from emp e where e.job = (select job from emp where ename = 'SCOTT');
Query results:
write picture description here
(3) Multiple subqueries
Query the employees who have SALESMAN in the department but whose position is not SALESMAN Information:
subquery: select deptno from emp where job = 'SALESMAN';
subquery: job <> 'SLAESMAN';
main query: select empno,ename,job,sal,deptno from emp;
main query + subquery
select empno ,ename,job,sal,deptno from emp where deptno in (select deptno from
emp where job = 'SALESMAN') and job <> 'SALESMAN';
write picture description here
the main query plus multiple subqueries is nothing more than adding multiple subqueries after where The statements are logically linked together, and the and operator is used here.

2. Subquery in the having clause
(1) Example: List the department information with employees
Main query: select deptno, dname from dept;
subquery: select * from emp e where e.deptno = d.deptno;
main query Combined with sub-query:
select d.deptno, d.dname from dept d where exists (select * from emp e where e.deptno = d.deptno);
query result: It just verifies that there is no employee in department 40.
write picture description here
(2) Example: Query and list the information of departments whose minimum salary is higher than the minimum salary of department 30.
Idea analysis: divided by department, there is group by and having often follows group by
Main query: select deptno,min(sal) min_sal from emp group by deptno;
subquery: select min(sal) as min_sal from emp where deptno = 30;
combined: select deptno,min(sal) min_sal from emp group by deptno having min(sal) > (select min(sal) as min_sal from emp where deptno = 30);
query results:
write picture description here
(3) Summary: Through the above (2) in the subquery in the having clause, you can find:
1. The main query and the subquery are both select query statements
2. According to the requirements, it is obvious that the stuff at the end is composed of the main query, and the stuff in the middle is composed of subqueries and join clauses.
Taking this example, the query lists information about departments whose minimum salary is higher than the minimum salary of department 30.
Main query: select department information
Sub query: select the minimum salary of department 30
Join clause: the minimum salary is higher than
3. The key is to determine what to use in the join clause. The
minimum salary is higher than, and there are words higher than, you can use where, having, Where is a large filter condition, and having is a small filter condition.
And we can conclude from the requirements that it is grouped by department, then there is group by, and after grouping by department, the amount of information is much less, you can consider using small filter conditions, and having often follows group by. .
So, in the end, the join clause is having min(sal) >
4. It should be noted that the join clause is having min(sal) >, do not write as min_sal or min(sal) as min_sal, after all, only after select Only things on the table can be written as min(sal) as min_sal
5. The final SQL query statement is the main query + join clause + sub query. For multiple subqueries, add an and to connect them.

(4) Example: Query the information of employees whose salary is higher than the average salary of the department.
Main query: select deptno,ename,sal from emp;
subquery: select deptno,avg(sal) as avg_sal from emp group by deptno;
1.select deptno,ename,sal,avg(sal) as avg_sal from emp where sal > avg(sal) group by deptno;
The above statement is wrong, the grouping function cannot be used, because sal, ename cause deptno repetition
2. Correct
select e.deptno, e.ename, e.sal from emp e join (select deptno, avg(sal) as
avg_sal from emp group by deptno) d on e.deptno = d.deptno where
e.sal > d.avg_sal order by e.deptno;
2. Query result:
write picture description here
3. According to the answer in the textbook:
select e .deptno,e.ename,e.sal from emp e,(select deptno,avg(sal) as avg_sal from emp group by deptno) d where e.deptno = d.deptno and e.sal > d.avg_sal order by e .deptno;
3. Query result:
write picture description here
Obviously, the query results of 2. and 3. are the same. Here, the main query and subquery are treated as two tables and connected. 2. feels better than 3.

二、分页查询
1、实例:
1. select rownum,empno,ename,sal from emp;
write picture description here
2. select rownum,empno,ename,sal from emp where rownum = 1;
write picture description here
3.select * from (select rownum m,e.* from emp e);
write picture description here
4. select * from (select rownum m,e.* from emp e) where m between 8 and 10;
write picture description here

2. Example:
select * from (select rownum m,t.*from(select empno,ename,sal from emp order by sal desc) t) where m between 8 and 10;
write picture description here
3. Summary of paging query: rownum
1. To query Field original table - table 1
2. Query (rownum field + table 1) - table 2
3. Query table 2, perform paging conditions
, namely : three checks plus paging
inner table, query requirements select empno, ename... from
middle table, query The table formed by rownum and demand select rownum m,t.* from
the outer table, query the total table select * from

3. Sorting function
1. Description
Format : both XX() over (partition by col1 order by col2),
the whole and min(sal) are of the same nature. It
means that the group is grouped according to col1, and the grade is given according to col2 within the group. Identification is Ranking

The three sorting functions are all in this format, but the ranking of the rank markers is different.
row_number()
rank()
dense_rank()

2. Example of sorting function
1. row_number
select deptno, ename, empno, row_number() over (partition by deptno order by empno) as emp_id from emp;
query result:
write picture description here
Frankly speaking, because the query is deptno, ename, empno, it is not There may be the same data, so rownum must be sorted from 1 after grouping

(1) Now test it, remove ename and empno and replace it with query job. Col2 is ranked according to job, and re-query. This time, let's see how rownum is sorted when encountering the same data.
select deptno,job,row_number() over (partition by deptno order by job) as emp_id from emp;
query result:
write picture description here
we can see that there is the same data: such as 20 ANALYST 20 CLERK 30 SALESMAN
, even if there are duplicates of the same data, it is still in order sorted.

  1. rank()
    is the ranking of the test level identification, here we do not query ename, empno, and replace it with query job, and the ranking is also selected by job
    deptno, job, rank() over (partition by deptno order by job) as rank_id from emp;
    Query results:
    write picture description here
    Obviously: jump sort 1 1 3 3 5

  2. dense_rank
    is the ranking of the test level identification. Here, we do not query ename and empno, but instead query for job. The ranking is also selected according to job
    deptno, job, dense_rank() over (partition by deptno order by job) as rank_id from emp;
    query results :
    write picture description here
    Obviously: 1 1 2 2 3

3. Summary
In fact something like dense_rank() over (partition by deptno order by job) as rank_id can be regarded as a field the same as min(sal) as min_sal, except that dense_rank() over (partition by deptno order by job) as rank_id This special field has its own special function. It can be grouped by deptno and sorted by job.

That is to say: select deptno,job,dense_rank() over (partition by deptno order by job) as rank_id from emp;
field deptno is the query department number
field job is the query job position
field dense_rank() over (partition by deptno order by job) ) as rank_id is the query "group by department, rank by job position"

4. Comparison of clo2 ranking of sorting functions
When the following three sorting functions are performed: sorting, and there is a tie for second place, then the sorting mark of the next column is:
row_number Regardless of whether there are duplicate values, it is always marked down in order: 1 2 3 4
rank has duplicate values, it is jump sort: 1 2 2 4
When dense_rank has duplicate values: 1 2 2 3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325591013&siteId=291194637