当前位置:网站首页>SQL series (basic) - Chapter 2 limiting and sorting data
SQL series (basic) - Chapter 2 limiting and sorting data
2022-07-05 20:53:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
Better look ↑ Code click VIEW PLAN
Chapter two Constrain and sort data
1. stay emp The salary selected in the table is between 1500 To 2500 Information about our employees ;
Be careful : Use between Lower boundary and On the upper border . Conditions include boundary values ;
[email protected]>l
1 select * from emp
2* where sal between 1500 and 2500
[email protected]>/
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
2. stay emp In the table, select the location in 20,30 Information about employees in the Department ;
[email protected]>select *from emp
2 where deptno in (20,30);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
11 rows selected.
3. stay emp The selection in the table is located in the employee's name, including uppercase characters ‘A’ Information about our employees ;
[email protected]>select * from emp
2 where ename like '%A%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7 rows selected.
Be careful : Suppose the name of the query includes % perhaps _ , And when you query, you need to query this kind of information , Transposition code is needed .
Be careful : wildcard %, Express 0 Or more than one character ; wildcard _, Express 1 It's like two characters .
3.1 Create with emp Table structure the same table ;
[email protected]>create table emp_n /* Reference emp Table create a new table emp_n */
2 as select * from emp where 1=2; /* Add where 1=2 Consistent table structure has no data */
Table created.
[email protected]>select * from emp_n;
no rows selected
3.2 Add test data including wildcards ;
[email protected]>insert into emp_n(empno,ename,sal) values(&empno,&ename,&sal);
Enter value for empno: 1001
Enter value for ename: 'whwh%gogo'
Enter value for sal: 1000
old 1: insert into emp_n(empno,ename,sal) values(&empno,&ename,&sal)
new 1: insert into emp_n(empno,ename,sal) values(1001,'whwh%gogo',1000)
1 row created.
[email protected]>/
Enter value for empno: 1002
Enter value for ename: '%whwh'
Enter value for sal: 2000
old 1: insert into emp_n(empno,ename,sal) values(&empno,&ename,&sal)
new 1: insert into emp_n(empno,ename,sal) values(1002,'%whwh',2000)
1 row created.
[email protected]>/
Enter value for empno: 1003
Enter value for ename: 'whwh_gogo'
Enter value for sal: 3000
old 1: insert into emp_n(empno,ename,sal) values(&empno,&ename,&sal)
new 1: insert into emp_n(empno,ename,sal) values(1003,'whwh_gogo',3000)
1 row created.
[email protected]>/
Enter value for empno: 1004
Enter value for ename: '_gogo'
Enter value for sal: 4000
old 1: insert into emp_n(empno,ename,sal) values(&empno,&ename,&sal)
new 1: insert into emp_n(empno,ename,sal) values(1004,'_gogo',4000)
1 row created.
[email protected]>commit;
Commit complete.
[email protected]>select * from emp_n;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1001 whwh%gogo 1000
1002 %whwh 2000
1003 whwh_gogo 3000
1004 _gogo 4000
3.3 The usage of transposition code .( Here to \ As transposition code . Transposition codes can also specify other characters )
example : The search includes % The recorded information of .
[email protected]>select * from emp_n
2 where ename like '%\%%' escape '\';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1001 whwh%gogo 1000
1002 %whwh 2000
[email protected]>select * from emp_n
2 where ename like '%\%%';
no rows selected
example : Search for % Record information at the beginning ;
[email protected]>select * from emp_n
2 where ename like '\%%' escape '\';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1002 %whwh 2000
example : The search includes _ The recorded information of .
[email protected]>select * from emp_n
2 where ename like '%\_%' escape '\';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1003 whwh_gogo 3000
1004 _gogo 4000
example : Search for _ Record information at the beginning ;
[email protected]>select * from emp_n
2 where ename like '\_%' escape '\';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
1004 _gogo 4000
4 The use of composite conditions
4.1 about and The combination of conditions ( To be able to between...and... convert )
example : stay emp The salary selected in the table is between 2000 To 3000 Information about our employees .
[email protected]>select * from emp
2 where sal>=2000 and sal<=3000;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
[email protected]>select * from emp
2 where sal between 2000 and 3000;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
4.2 about or Conditional compounding ( To be able to in() convert )
example : stay emp Select from the table 10 Number and 20 Employee information of department No ;
[email protected]>select * from emp
2 where deptno=10 or deptno=20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
8 rows selected.
[email protected]>select * from emp
2 where deptno in (10,20);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
8 rows selected.
5 For sorting the data in the table
5.1 asc It means to arrange in ascending order according to the given fields ( Default ascending order )
desc It means to arrange in descending order according to the given fields
example : take emp In the table 10 The employee information of department No sal The columns are arranged in ascending order
[email protected]>select * from emp
2 where deptno=10;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
[email protected]>select * from emp
2 where deptno=10
3 order by sal asc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
[email protected]>select * from emp
2 where deptno=10
3 order by sal;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
example : take emp In the table 20 The employee information of department No sal The columns are arranged in descending order
[email protected]>select * from emp
2 where deptno=20
3 order by sal desc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
5.2 hypothesis order by Followed by multiple fields . Then, the result set is set according to section 1 Fields to sort 【 Conditions 1】.
And then press... Again 2 Fields to sort 【 Conditions 2】;
Be careful :【 Conditions 1】 Suppose that according to section 1 When a field can't be separated in order . In accordance with article 2 Field sort .
asc perhaps desc Affected fields , But the field next to it .
example : take emp In the table 10 For the employee information of department number, press sal Descending order ,empno Ascending order ;
[email protected]>select * from emp
2 where deptno=10
3 order by sal desc,empno asc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
example : take emp In the table 20 The employee information of department No empno Descending order ,sal Ascending order ;
[email protected]>select * from emp
2 where deptno=20
3 order by empno desc,sal asc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
5.3 In addition to being able to use field sorting . You can also sort by using the order in which the fields are located ;
Determine the order of fields in the table .
[email protected]>set lines 100
[email protected]>desc emp;
/*
* Tips : This line of code is too long , The system automatically comments without highlighting . One click Copy removes system comments
* Name Null? Type ----------------------------------------------------- -------- ------------------------------------ EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) example : take emp In the table 10 For the employee information of department number, press sal Descending order ,empno Ascending order . [email protected]>select * from emp 2 where deptno=10 3 order by 6 desc,1 asc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 example : take emp In the table 20 The employee information of department No empno Descending order ,sal Ascending order ;[email protected]>select * from emp 2 where deptno=20 3 order by 1 desc,6 asc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 5.4 Besides being able to use number Type of field , You can also use string or time type fields to sort . Be careful : String sort : According to the character corresponding to ASCII The sequence of the codes . Date sort : Sort by date , The more time goes on, the more ; example : take emp The employees in the table are in accordance with job Ascending 、ename In descending order ;( According to the first Job Sort . alike job Press ename Descending )[email protected]>select * from emp 2 order by job asc,ename desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7900 JAMES CLERK 7698 03-DEC-81 950 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7839 KING PRESIDENT 17-NOV-81 5000 10 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 3014 rows selected. example : take emp The employees in the table are in accordance with HIREDATE Ascending ,sal Descending order ( hypothesis HIREDATE Again , according to sal Descending ) First the HIREDATE Display format settings [email protected]>alter session set nls_date_format='yyyy-mm-dd';Session [email protected]>select * from emp 2 order by hiredate asc,sal desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- ---------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 1980-12-17 800 20 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 7566 JONES MANAGER 7839 1981-04-02 2975 20 7698 BLAKE MANAGER 7839 1981-05-01 2850 30 7782 CLARK MANAGER 7839 1981-06-09 2450 10 7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 7839 KING PRESIDENT 1981-11-17 5000 10 7902 FORD ANALYST 7566 1981-12-03 3000 20 7900 JAMES CLERK 7698 1981-12-03 950 30 7934 MILLER CLERK 7782 1982-01-23 1300 10 7788 SCOTT ANALYST 7566 1987-04-19 3000 20 7876 ADAMS CLERK 7788 1987-05-23 1100 2014 rows selected. 6. Sort with aliases in the result set . example : according to emp The annual salary of employees in the table (sal*12) Sort [email protected]>select empno,ename,hiredate,sal*12 year_sal 2 from emp 3 order by year_sal desc; EMPNO ENAME HIREDATE YEAR_SAL---------- ---------- ---------- ---------- 7839 KING 1981-11-17 60000 7902 FORD 1981-12-03 36000 7788 SCOTT 1987-04-19 36000 7566 JONES 1981-04-02 35700 7698 BLAKE 1981-05-01 34200 7782 CLARK 1981-06-09 29400 7499 ALLEN 1981-02-20 19200 7844 TURNER 1981-09-08 18000 7934 MILLER 1982-01-23 15600 7521 WARD 1981-02-22 15000 7654 MARTIN 1981-09-28 15000 7876 ADAMS 1987-05-23 13200 7900 JAMES 1981-12-03 11400 7369 SMITH 1980-12-17 960014 rows selected. Be careful : Of course, you can also follow strings 、 The date is sorted by the corresponding alias ; 7. Suppose the sorted fields include null value , What will happen ? Be careful : When comparing the size of fields ,null It's worth more than anything ; example :emp The information of the employees in the table is in accordance with comm Descending order ;[email protected]>select * from emp 2 order by comm desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- ---------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 1980-12-17 800 20 7782 CLARK MANAGER 7839 1981-06-09 2450 10 7902 FORD ANALYST 7566 1981-12-03 3000 20 7900 JAMES CLERK 7698 1981-12-03 950 30 7876 ADAMS CLERK 7788 1987-05-23 1100 20 7566 JONES MANAGER 7839 1981-04-02 2975 20 7698 BLAKE MANAGER 7839 1981-05-01 2850 30 7934 MILLER CLERK 7782 1982-01-23 1300 10 7788 SCOTT ANALYST 7566 1987-04-19 3000 20 7839 KING PRESIDENT 1981-11-17 5000 10 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 7844 TURNER SALESMAN 7698 1981-09-08 1500 0 3014 rows selected. Be careful :null Sorting between values does not take into account ( because Null and null Can't compare size between ) Suppose I want to comm Change the value part to the top , What should I do ?[email protected]>select * from emp 2 order by comm desc nulls last; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- ---------- ---------- ---------- ---------- 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30 7788 SCOTT ANALYST 7566 1987-04-19 3000 20 7839 KING PRESIDENT 1981-11-17 5000 10 7876 ADAMS CLERK 7788 1987-05-23 1100 20 7900 JAMES CLERK 7698 1981-12-03 950 30 7902 FORD ANALYST 7566 1981-12-03 3000 20 7934 MILLER CLERK 7782 1982-01-23 1300 10 7698 BLAKE MANAGER 7839 1981-05-01 2850 30 7566 JONES MANAGER 7839 1981-04-02 2975 20 7369 SMITH CLERK 7902 1980-12-17 800 20 7782 CLARK MANAGER 7839 1981-06-09 2450 1014 rows selected. Empathy : Verify by yourself Ascending array [email protected]>select * from emp 2 order by comm asc nulls first;</span>
*/
Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117647.html Link to the original text :https://javaforall.cn
边栏推荐
- Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- Abnova丨血液总核酸纯化试剂盒预装相关说明书
- Open source SPL eliminates tens of thousands of database intermediate tables
- Make Jar, Not War
- 基于AVFoundation实现视频录制的两种方式
- MYSQL IFNULL使用功能
- XML建模
- Graph embedding learning notes
- Norgen AAV extractant box instructions (including features)
猜你喜欢
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
14、Transformer--VIT TNT BETR
Use of form text box (II) input filtering (synthetic event)
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
Graph embedding learning notes
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
Duchefa d5124 md5a medium Chinese and English instructions
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Abnova DNA marker high quality control test program
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
随机推荐
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Clear app data and get Icon
Which is the best online collaboration product? Microsoft loop, notion, flowus
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
Abnova DNA marker high quality control test program
序列联配Sequence Alignment
Learning notes of SAS programming and data mining business case 19
启牛2980有没有用?开户安全吗、
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
表单文本框的使用(二) 输入过滤(合成事件)
Write an interface based on flask
模式-“里氏替换原则”
证券开户选择哪个证券比较好?网上开户安全么?
When steam education enters personalized information technology courses
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
手机开户股票开户安全吗?我家比较偏远,有更好的开户途径么?
Selenium element information
Abnova CD81 monoclonal antibody related parameters and Applications
Duchefa cytokinin dihydrozeatin (DHZ) instructions
概率论机器学习的先验知识(上)