SQL的基本语法练习
时间:2021-07-01 10:21:17
帮助过:12人阅读
EMPNO,SAL
from emp
where SAL BETWEEN
1000 and
2000--
从enp中获取sal中1000到2000之间的数据
select ENAME,SAL
from emp
where SAL >=
1000 and SAL <=
2000--
从enp中获取sal中1000到2000之间的数据
select ENAME,SAL
from emp
where SAl IN(
1000,
2300);
select EMPNO
from emp ORDER BY EMPNO;--
默认排序
select EMPNO
from emp ORDER BY EMPNO ASC--
升序
select EMPNO
from emp ORDER BY EMPNO DESC--
降序
select LOWER( ENAME)
from emp --
大小写的转换
select *
from emp;
select *
from emp;
select trim(ename)
from emp--
去掉首尾的空格
select *
from emp;
select *
from emp
where ENAME =
‘KING‘--
从中查找某个指定的
select *
from emp;
select sal,sal+
300 from emp--使用数学运算符“+
”
select sal,sal*
10 from emp--使用数学运算符“*
”
select sal,
10*sal+
300 from emp--
运算符的优先级
select sal,
10*(sal+
300)
from emp--
带括好的运算
select *
from emp;
select sal+
20 AS sal_o
from emp
select *
from emp;
select ENAME || JOB
from emp--
连接符的运算
------数值函数-----
round--
四舍五入
select round(
412,-
1)
from dual--
410
select round(
412.777,
2)
from dual--
412.78
trunc--
截断
select trunc(
412.13,-
1)
from dual--
410
MOD --
求余
select MOd(
200,
11)
from emp
select Mod(
135.23,
15)
from dual
--
日期函数
Months_between()---
两个日期相差的月数
select months_between(sysdate,hiredate)
from emp
Last_day---
本月的最后一天
select last_day(sysdate)
from dual
Next_day()--
指定日期的下个日期
select next_day(sysdate,
‘星期一‘)
from dual
-------------------------------------------------
select *
from emp order by sal DESC
select sal
from emp
order by sal DESC
--------------------------------------------------
Add_months()--
向指定日期加上若干月数
select Add_months(sysdate,
0)
from dual;
-----转换函数----
to_char--
对数字的转换
select to_char(sysdate,
‘yyyy‘)
from dual;
select to_char(sysdate,
‘fmyyyy-mm-dd‘)
from dual;
select to_char(sal,
‘L999,999,999‘)
from emp;
select to_char(sysdate,
‘D‘)
from dual--
返回星期
To_number--
讲字符转换成数字
select to_number(
‘13‘)+to_number(
‘14‘)
from dual---
27
To_date
Select to_date(‘20090210‘,
‘yyyyMMdd‘)
from dual
------通用函数--
NVL()函数
select nvl(comm,
1)
from emp
-----------------------------------------------------------------
--
模糊查询like
select *
from emp
select *
from emp
where job like
‘%*_%‘
-----
逻辑运算符
--
选择在部门30中员工的所有信息
select *
from emp
where deptno=
30;
--
列出职位为(MANAGER)的员工的编号,姓名
select EMPNO,ENAME
from emp
where job=
‘MANAGER‘
--
找出奖金高于工资的员工
select *
from emp
where comm>
sal
--
找出每个员工奖金和工资的总和
select comm+sal,ename
from emp
--
找出部门10中的经理(MANAGER)和部门20中的普通员工(CLERK)
select *
from emp
where (deptno=
10 and job=
‘MANAGER‘)or(deptno=
20 and job=
‘CLERK‘)
--
找出部门10中既不是经理也不是普通员工,而且工资大于等于2000的员工
select *
from emp
where deptno=
10 and job not
in(
‘MANAGER‘,
‘CLERK‘) and sal>=
2000;
--
找出有奖金的员工的不同工作
select distinct job
from emp--
distinct不同
where comm
is not
null and comm>
0;
--
找出没有奖金或奖金低于500的员工
select *
from emp
where comm <
500 or comm
is null;
--
显示雇员信息姓名,根据其服务年限,将最老的雇员排在最前面
select ename
from emp
order by hiredate;
SQL的基本语法练习
标签:sda last 模糊 code distinct between 逻辑运算 div rom