Select statement
1. Basic query:
I. Syntax: select column name from table name
II. Detailed explanation:
select: Specifies the columns of the query.
from: Specifies the table to query.
III. Example:
1). Query some columns:
//Query all employee numbers, names, and mailboxes in the employee table.
select employee_id , first_name , email
from employees
2). Query all columns:
//Query all the information of all employees in the employee table.
select *
from employees;
Note: In a production environment, it is recommended to use column names (high efficiency).
3). Operation on the data in the column: + - * / (%: wildcard)
//Query number, name, annual salary
select employee_id , first_name , salary * 12
from employees;
4). Column alias: column as "alias"
//Query number, name, annual salary
select employee_id as "number" , first_name as "name" , salary * 12 as "annual salary"
from employees;
5). Concatenating strings: string 1 || string 2
// query number and "name" (first_name || last_name)
select employee_id , first_name || '.' || last_name as "姓名"
from employees
Note: Characters or strings in SQL are represented by ' (single quotation marks), strings in query results are concatenated using || and 'string', and "this article" is used in aliases.
6). Deduplication of query results: (in a single column, only one duplicate result is retained)
//Query the IDs of all managers
select distinct manager_id
from employees;
2. 排序:对查询后的数据进行排序。
I. 语法:select ... from ... order by 排序列
II. 详解:order by 排序列 [asc] [desc] 默认升序
III. 用例:
1). 依据单列排序:
//查询员工的编号,名字,薪资。按照工资高低进行升序排序。
select employee_id , first_name , salary
from employees
order by salary desc
2). 依据多列排序:
//查询员工的编号,名字,薪资。按照工资高低进行升序排序(薪资相同时,按照编号进行升序排序)。
select employee_id , first_name , salary
from employees
order by salary asc , employee_id asc
3. 条件查询:筛选符合条件的结果。
I. 语法:select ... from ... where 布尔表达式
II. 详解:where 布尔表达式 //可执行条件筛选
III. 用例:
1). 等值判断(=):
/ /查询薪资是11000的员工信息(编号、名字、薪资)
select employee_id , first_name , salary
from employees
where salary = 11000;
注意:与Java不同,SQL中的"="代表表达式左右的两值相等
2). 多条件查询(and、or、not):
//查询薪资是11000并且提成是0.30的员工信息(编号、名字、薪资)
select *
from employees
where salary = 11000 or commission_pct = 0.30
3). 不等值判断:(>、<、>=、<=、!=、<>)
//查询员工的薪资在6000~10000之间的员工信息(编号,名字,薪资)
select *
from employees
where salary >= 6000 and salary <= 10000;
//查询员工的薪资不是24000员工信息
select *
from employees
where salary <> 24000;
4). 区间判断(between and):
//查询员工的薪资在6000~10000之间的员工信息(编号,名字,薪资)
select *
from employees
where salary between 6000 and 10000;
5). null值判断:DBNull(is null、is not null)
//查询没有提成的员工信息(编号,名字,薪资 , 提成)
select *
from employees
where commission_pct is not null
6). 枚举判断(in(v1,v2,v3,...)):
//查询部门编号为70、80、90的员工信息(编号,名字,薪资 , 部门编号)
select *
from employees
where department_id in(70,80,90);
7). 模糊查询:(%:任意长度的任意字符; _:长度为1的任意字符)
//查询名字以"L"开头的员工信息(编号,名字,薪资 , 部门编号)
select *
from employees
where first_name like 'L%'
//查询名字以"L"开头并且长度为4的员工信息(编号,名字,薪资 , 部门编号)
select *
from employees
where first_name like 'L___'
4. 时间查询:
I. 语法:select 系统时间 from 表名[虚表]
II. 详解:
sysdate:当前系统时间(年、月、日、时、分、秒、星期、上下午),常见格式。
systimestamp:当前系统时间(年、月、日、时、分、秒、毫秒),默认格式
dual表:虚表(一行一列),保证查询语句完整性。
III. 用例:
//查询当前系统时间
select sysdate
from dual;
select systimestamp
from dual;
5. 单行函数:
I. 语法:select 单行函数(列名) from 表名
II. 详解:
to_char( 日期 , '日期格式' ):将日期通过指定格式进行字符串转换。
to_date( 字符串 , '日期格式' ):将字符串通过指定格式进行日期转换。
III. 用例:
1). 日期转字符串:
//将当前系统时间转换成(yyyy-mm-dd hh:mi:ss day)格式。
select to_char(sysdate , 'yyyy-mm-dd hh:mi:ss day')
from dual;
2). 字符串转日期:
//将和日期兼容的字符串按照(yyyy-mm-dd hh24:mi:ss:ff day)转换成日期。
select to_date('19950101','yyyy-mm-dd')
from dual;
3). 查询2008.08.08是星期几?
--1.获得代表这个时间的日期对象
select to_date('2008/8/8','yyyy/mm/dd') from dual;--日期
--2.获得该对象的"星期"组成部分
select to_char( to_date('2008/8/8','yyyy/mm/dd') , 'yyyy-mm-dd day')
from dual;
4). mod(被除数 , 除数):模运算,求余数
select mod(5,3) //余2
from dual;
select mod(salary,10000)
from employees;
//为107行数据的salary列,进行取余的调用,结果为107行
6. 聚合函数(组函数):对表中数据进行统计,并返回统计后的结果(往往都是一行数据)。
I. 语法:select 聚合函数(列名) from 表名
II. 详解:
sum():求和
avg():平均数
max():最大值
min():最小值
count():总行数、计数器
III. 用例:
//统计所有员工每月的工资总和
select sum(salary)
from employees
//统计所有员工每月的平均工资
select avg(salary)
from employees
//查看所有员工最高月薪
select max(salary)
from employees
//查看所有员工最低月薪
select max(salary)
from employees
//统计员工总人数
select count(*)
from employees;
//统计有提成的员工人数
select count(commission_pct)
from employees
//查询所有工号、名字、薪资、提成、月收入
select employee_id , first_name , salary , commission_pct , salary + salary * commission_pct
from employees //Error
注意:当null参与运算时,结果为null。
select nvl(commission_pct , 0) from employees //commission_pct != null ? commission_pct : 0
select employee_id , first_name , salary , commission_pct , salary + salary * nvl(commission_pct , 0)
from employees //Correct
7. 分组查询:
I. 语法:select ... from ... where ... group by 分组依据
II. 详解:分组依据(列) //此列具有几个不同值,及分为几组
III. 用例:
//查询各个部门的总人数
select department_id , count(employee_id)
from employees
group by department_id -- 部门编号相同的多条数据,进行一次统计,返回一行数据。(有多少个不同部门,就有多少组,就要统计多少次)
//查询各个部门的平均工资
select department_id , avg(salary)
from employees
group by department_id
//查询各个部门、各个岗位的人数
select department_id , job_id , count(*)
from employees
group by department_id , job_id
//查询各个部门id、总人数、first_name --无解
注意:分组查询中,select显示的列只能是分组依据、聚合函数的结果,不能出现其他列。
8. 分组过滤:
I. 语法:select ... from ... where ... group by ... having 过滤规则
II. 详解:过滤规则(分组后的数据进行筛选)
III. 用例:
//统计60、70、90号部门的最高工资
select department_id , max(salary)
from employees
where department_id in(60,70,90) //先将符合条件的9个人筛选出来,再进行分组
group by department_id //Error
select department_id , max(salary)
from employees
group by department_id
having department_id in(60,70,90) //Correct 先分组,再筛选
//查询平均工资超过7000的部门编号和人数(可附加:平均工资,并且不显示没有部门编号的)
select department_id , avg(salary) , count(*)
from employees
group by department_id
having avg(salary) > 7000
注意:where可判断表中存在的列(未分组前数据),having可判断表中不存在的列(聚合函数统计出来的列)。