sql basic knowledge (notes) (1)

**

sql basic knowledge (notes) (1)

**
At the time, learning sql notes (oracle)

1. Simple query:

select * from 表名;

Among them, select and from are keywords, pan-green, Oracle all pan-green are keywords
* is a wildcard, representing all columns; the
table name is the name of the table that needs to be queried, we need to look at the data in the table, just Write the name of which table, what you find is all the data of this table;
a semicolon is required after each SQL sentence, and the semicolon represents the end of a SQL statement, remember.

select 列1,列2,列3 from 表名;

When we want to query some of the columns in the table, we can't use it, because all the columns are found, such as the transcript table. The language teacher only wants to see the language results, and there is no need to use select *
When we want to query some columns When you want to query the column name instead of *, if you want to query multiple columns, separate the multiple column names with commas. For
example, query the empno and ename of the emp table, as we said above, you can write select empno, ename from emp; the same is true for more columns, they can be separated.
Pay attention to a detail here, the order of the column names we query determines the order of the columns in the result, that is, if empno is queried first, the data When displaying empno first, and then query ename, then ename is displayed second.
The display order of query results is only related to the query order. The order of select * is always fixed, because the order of the columns in the table is fixed, DBA_TAB_COLUMNS There is COLUMN_ID in the table, which determines the order of the columns in the table and also determines the display order;

Table alias and column alias

select b.列1,b.列2 from 表名 b;

When we query some of the columns, there is actually a table name before the column name, that is, the table name. The form of the column name, after the alias is taken, the alias can be used to replace the table name, that is, the alias. The column name form. Because There is only one table, all table names. This part can be omitted.
But we will learn to query data from multiple tables later, and we can’t omit it at this time. Especially when the table name is relatively long, the alias of the table is very practical, which can improve our coding efficiency, and there is no need to repeat the data table name. .

select 列1 as 别名1, 列2 别名2 from 表名;

When listing aliases, you need to pay attention to the naming convention of aliases:
try to use word abbreviations. If there are multiple words, use underscore _ to connect, not underscore; when
numbers, _ begin, you need to wrap the
column name with double quotes . Symbols, such as brackets, etc., need to be wrapped in double quotation marks. The
alias can be in Chinese without quotation marks. This is the only place.

**

2. Filter query:

**

Data type: The
data type refers to the format of the data.The data here does not only refer to numbers, but refers to the contents of the table.

Common data types are as follows:
varchar2: string type, containing various characters, letters, Chinese characters, symbols, etc. Because of the different encoding formats, the number of digits occupied by Chinese characters will be different, GBK Chinese characters occupy 2 digits, UTF -8 occupies 3 places.
Varchar2 is different from varchar. What is the point of memory: It is to store the character type varchar2 is variable, varchar2(40), if I only store 1 letter, the actual place 1 is how much varchar allocates How much, varchar(40), stores a letter, these 40 characters are all occupied, others cannot use it. When using the
string format, it needs to be wrapped in single quotes. (Are there any exceptions?) The maximum length is 4000

number: number type, that is data. 1, 2, 3, etc. Numbers can be directly compared. number(5) represents a number type, with a maximum length of 5 digits. number(4, 2) represents a maximum length of 4 Digits, including 2 decimal places. For
example, number(5), the maximum length is 5 digits, and the data can be 1 digit, such as the number 7, or 3 digits, such as 156, up to 5 digits.
number(4,2): It refers to the maximum length of 4 digits, of which 2 decimal places. That is, the data can be 1, but it is displayed as 1.00 because there are 2 decimal places. That is, the integer has a maximum of 2 digits, and the decimal is always 2 digits. The
longest is 38 digits

date: time type, representing time, such as current time sysdate. to_date(), to_char()

Filtering query: The
previous explanation is to query the entire content of the entire table or the entire content of a few columns. If you want to query a part of the data, you must use the filtering query.
Basic syntax:

select * from 表名 where 列名=值;--查询表中列名的值=值的数据.
比如select * from emp where ename='ADAM';--查询emp表中ename是'ADAM'的人的信息

There are several ways to see the data type of a column :
1. select * from 表名;Place the mouse on a column of data, and the type of data in this column will be displayed below;
2. Ctrl + left mouse button, click on the table name, and you can see the data of each column Type;
3.In the USER_TAB_COLUMNS view, there is information such as the length of the data type of each column, and COLUMN_ID is also in it

A keyword where appears after the table name , which can be understood as the condition is, or the condition is met, followed by the query condition column name=value. The expression means that the condition that the data in the column is equal to the following value is satisfied.

There is more than one = operator, and there are also >,<,>=,<=,<>, between...and, in, not in.
Note that between...and contains the boundary. Please pay attention to it when using it. If If it is statistical data, use it with caution.

At present, we only have one query condition. In practical applications, we often use multiple conditions at the same time to improve the accuracy of the query. Here we need to introduce and and or to satisfy multiple condition queries.

And: means to meet the two conditions before and after the and at the same time
or: it is sufficient to meet any one of the conditions before and after or, and it can also be understood that the query result is a collection of the results that meet the previous conditions and the results that meet the following conditions.

And there is a priority relationship between and and or: ()>and>or
that is, when and and or exist at the same time, execute and first, and then execute or. If there are parentheses, execute the parentheses first. Looking back at the notes, take a look at our previous Practice questions.

In daily coding, we need to use parentheses to improve code readability.

There are two more points in the filter query:

  1. Usage of in: at this stage in can be understood as a combination of multiple ors, and the data within a certain range is similarly not in
  2. Oracle judged empty: is null, is not null when judged not to be empty. The other methods are not correct (='', = null), etc.

Practice questions:

create table emp_0204
(emp_no number(4),
emp_name varchar2(50),
emp_job varchar2(50),
emp_hiredate date,
emp_dept varchar2(50)
);

insert into emp_0204 values(0001,'Sucre','MANAGER',to_date('19850308','YYYYMMDD'),'10');
insert into emp_0204 values(0002,'Alex','SALESMAN',to_date('19820110','YYYYMMDD'),'10');
insert into emp_0204 values(0003,'Bill','SALESMAN',to_date('19950820','YYYYMMDD'),'10');
insert into emp_0204 values(0004,'Shown','SALESMAN',to_date('19871102','YYYYMMDD'),'10');
insert into emp_0204 values(0005,'Han','SALESMAN',to_date('19931008','YYYYMMDD'),'10');
insert into emp_0204 values(0006,'George','MANAGER',to_date('19981126','YYYYMMDD'),'20');
insert into emp_0204 values(0007,'John','SALESMAN',to_date('19830510','YYYYMMDD'),'20');
insert into emp_0204 values(0008,'Peter','SALESMAN',to_date('19930815','YYYYMMDD'),'20');
insert into emp_0204 values(0009,'Matthew','SALESMAN',to_date('19940619','YYYYMMDD'),'20');
insert into emp_0204 values(0010,'Edward','PRESIDENT',to_date('20081028','YYYYMMDD'),'30');
insert into emp_0204 values(0011,'Hamilton','SALESMAN',to_date('20010609','YYYYMMDD'),'30');
insert into emp_0204 values(0012,'Connor','SALESMAN',to_date('19821112','YYYYMMDD'),'30');
commit;

1. Check the entry time of all MANAGER;

select emp_hiredate from emp where job='MANAGER';

2. Query all the names of SALESMAN;

select emp_name from emp where emp_job='SALESMAN';

3. Query the MANAGER of department 20;

select * from emp where emp_job='MANAGER' and emp_dept='20';

4. Query the entry time after 90 years and the position is not SALESMAN;

select *from emp where emp_hiredate>to_date('19900101','YYYYMMDD') and emp_job <>'SALESMAN';

5. Query the MANAGER of department 10 and SALESMAN of department 20;

select * from emp where
(emp_dept='10' and emp_job='MANAGER')
or
(emp_dept='20' and emp_job='SALESMAN');

6. Query the PRESEDENT of department 30 and the MANAGER of department 20;

select * from emp where
(emp_dept='30' and emp_job='PRESIDENT')
or
(emp_dept='20' and emp_job='MANAGER');

7. Query SALESMAN in all departments;

select * from emp where emp_job='SALESMAN';

8. Query all SALESMAN in department 10;

select * from emp where emp_dept='10' and emp_job='SALESMAN';

**

Fuzzy query

**
The concept of fuzzy query: when the conditions are incomplete or insufficient,
the usage of fuzzy query like is needed . The syntax of fuzzy query: select * from table name where column name like'% content%';
meaning of% and _ :% has 0 or more characters before it and it _ there is a character in the current position

select * from emp where ename like '%TH%';--ename中包含TH字样的
select * from emp where ename like '___TH';--查询ename以TH结尾,且长度是5位的

Search for data beginning or ending with fixed content

select * from 表名 where 列名 like '内容%';--以固定内容开头
selct * from 表名 where 列名 like '%内容';--以固定内容结尾的数据
搜索固定长度的数据
select * from 表名 where 列名 like '_____';--搜索长度是5位的数据 考虑函数方式
搜索带%或者_的数据
select * from 表名 where 列名 like '%/%%' escape '/';
select * from 表名 where 列名 like '%/_%' escape '/';
搜索带%和_且不挨着的数据
'%/%%_/_%' escape '/'
'%/_%_/%%' escape '/'

Sort
Sort syntax:

select * from 表名 order by 列名 ;

Sorting method;
order by: sorting, which is also a keyword, sorting according to the following column and sorting method
. Two sorting methods asc, desc: ascending asc (default), descending desc.
Multiple sorting syntax and styles: order by column name 1 asc, column name 2 desc;-sort according to the first column first, when there is duplicate data in the first column, sort according to the second column. If there is no duplicate data in the first column, the sort according to the first column is the final result

select * from 表名		order by 列名1 desc, 列名2 asc;

order by 1, order by 2: 1,2 represents the first column of the query content.
The position used for
sorting How to use
select * from table name where column name Like'%content%' order by column name sort the way;

Exercise:

insert into emp_0204 values (13,'AB_CD','TEST',sysdate-20,'10');
insert into emp_0204 values (14,'A%D','TEST',sysdate-100,'20');
insert into emp_0204 values (15,'A%D_E%F','TEST',sysdate-15,'30');
insert into emp_0204 values (16,'AD_E%F','TEST',sysdate-15,'30');
commit;
--1.查询emp_0204表emp_name包含n的所有人信息;
select * from emp_0204 where emp_name like '%n%';
--2.查询emp_0204表emp_name以H开头的;
select * from emp_0204 where emp_name like 'H%';
--3.查询emp_0204表emp_name以F结尾的;
select * from emp_0204 where emp_name like '%F';
--4.查询emp_0204表emp_name中包含_的;
select * from emp_0204 where emp_name like '%/_%' escape '/';
--5.查询emp_0204表emp_name中包含%的;
select * from emp_0204 where emp_name like '%/%%' escape '/';
--6.查询emp_0204表emp_name中包含%和_的;
select * from emp_0204 where emp_name like '%/_%' escape '/' and emp_name like '%/%%' escape '/';
select * from emp_0204 where emp_name like '%/_%/%%' escape '/' or emp_name like '%/%%/_%' escape '/';
--7.查询emp_0204中emp_dept为10的员工信息,以emp_name升序展示;
select * from emp_0204 where emp_dept='10' order by emp_name asc;
--8.查询emp_0204中emp_dept为20的员工信息,按照emp_job降序展示;
select * from emp_0204 where emp_dept='20' order by emp_job desc;
--9.查询emp_0204中emp_dept为30的员工信息,先按照emp_job升序展示,再按照emp_name降序展示;
select * freom emp_0204 where emp_dept='30' order by emp_job asc,emp_name desc;
--10.查询emp_0204中emp_dept为20的员工信息,按照emp_hiredate降序展示,再按照emp_name升序展示;
select *from emp_0204 where emp_dept='20' order by emp_hiredate desc, emp_name asc;
--11.查询emp_0204中emp_name包含e字母的,按照emp_name升序排序;
select * from emp_0204 where emp_name like '%e%' order by emp_name asc;

**

Grouping:

**
Basic syntax multi-column grouping (syntax and meaning)
group by column name 1, column name 2;
the content after select (aggregate function, what does each mean coount(1), count(2))
group column, aggregate function processing Passed column
MAX-maximum value
MIN-minimum value
SUM-sum
AVG-average number
COUNT-number count(*), count(1), count(2),
count(column name) If there are null values ​​in the column, then The number obtained does not include null values.

select 内容 from 表名 group by 列名;

Content: grouped columns or columns processed by aggregate functions

having and where:
where: filtering for the entire table data
having: filtering for the data within the group

group by column name having content; content
after having:
1. grouping column is available;
2. aggregate function can be used;

The order of group by, having, order by when writing
SQL execution order

select * from 表名 where 条件 group byhaving 内容 order byasc/desc;

1. The writing method when using group by, having and order by at the same time;
2. Execution order:
(1): where condition;
(2): group by;
(3): having filtering;
(4): select;
(5) :order by;

case when 2种写法

case 列名
when1 then 执行语句1
when2 then 执行语句2
when3 then 执行语句3
else 执行语句4 end case;

case 
when>1 then 执行语句1
when>2 and<3 then 执行语句2
when>4 and<5 then 执行语句3
else 执行语句4 end case;

Extension:
Use group by to filter duplicate data
1: distinct
2: group by--max(rowid)/min(rowid) The column name of group by is the grouping column, usually the primary key column
3: union
4: row_number() over(partition by )

rowid--Each row of data has a rowid, which is a unique string, which corresponds to the data one-to-one. It can be used to filter duplicate data.
rownum--fake, can filter the first few rows of data, rownum<4, but cannot rownum> certain Data.

exercise:

--针对emp_0204表:
--1.获取10部门最晚的入职时间;
select max(emp_hiredate) from emp_0204 where emp_dept='10';
--2.获取20部门的最早入职时间;
select min(emp_hiredate) from emp_0204 where emp_dept='20';
--3.获取SALESMAN中最早的入职时间;
select min(emp_hiredate) from emp_0204 where emp_job='SALESMAN';
--4.获取所有MANAGER中入职最晚的;
select max(emp_hiredate) from emp_0204 where emp_job='MANAGER';
--5.筛选人数超过6人的部门;
select emp_dept from emp_0204 group by emp_dept having count(1)>6
--7.查询sal<1000, 1000-3000,3000-5000和>5000的各阶段人数;
select SUM(case when
          sal is null then 1 else 0 end) sal_null,
          SUM(case when sal<1000 then 1 else 0 end) sal_1000,
          sum(case when sal>=1000 and sal<3000 then 1 else 0 end) sal_1000_3000,
          sum(case when sal>=3000 and sal<=5000 then 1 else 0 end) sal_3000_5000,
          sum(case when sal>5000 then 1 else 0 end) sal_5000
          from emp_0204;

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/111878681