Oracle 数据库常用SQL语句(2)查询语句

摘要:
select*froms_empwheresalarybetween1000and1500;7、空非空8、全部任一不能单独使用,必须要与关系运算符配合。select*froms_empwheretitle='StockClerk';查询工资比任一仓库管理员高的员工信息。selectlast_name,title,id,salaryfroms_emporderbysalarydesc;3、多项排序格式:select字段from表名where条件ORDERBY字段ASC|DESC,字段ASC|DESC;三、聚合函数注意:在使用比较运算符时NULL为最大值,在排序时也会受影响。c、groupby字段名,确定数据的分组依据。e、orderby对组数据进行排序。

一、SQL基础查询

1、select语句

格式:select 字段 from 表名;

2、where 用于限制查询的结果。
3、查询条件 > < >= <= = !=
4、与 或(AND,OR)
5、在 不在(IN,NOT IN)
练习:查询工号为1,9,11,16且工资低于1000的员工。
6、在 [a,b] (between val1 and val2)
查询工资大于1000,小于1500的员工。

select * from s_emp where salary between 1000 and 1500;

7、空 非空(IS NULL,NOT NULL)
8、全部 任一(ALL,ANY)
不能单独使用,必须要与关系运算符配合。
查询职位是仓库管理员的工资。

select * from s_emp where title = 'Stock Clerk';

查询工资比任一仓库管理员高的员工信息。

select * from s_emp where salary > any(select salary from s_emp where title = 'Stock Clerk');

9、排重 DISTINCT
练习:工资在1000~1500之间的职位有哪些。

select distinct title from s_emp where salary>=1000 and salary <1500 ;

二、排序

1、使用 ORDER BY 语句
格式:select 字段 from 表名 where 条件 ORDER BY 字段;
显示员工名字、职位、工号,按年薪从低到高的排列显示。

select last_name,title,id,salary from s_emp order by salary;

2、设置升序降序(ASC,DESC)
格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC;
显示员工名字、职位、工号,按年薪从高到低的排列显示。

select last_name,title,id,salary from s_emp order by salary desc; 

3、多项排序
格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC,字段 ASC|DESC;

三、聚合函数

注意:在使用比较运算符时NULL为最大值,在排序时也会受影响。
把select语句的多条查询结果,汇聚成一个结果,这样的函数叫聚合函数。
1、MAXMIN
获取最大值和最小值,可以是任何数据类型,但只能获取一个字段。

select * from s_emp where salary = (select MAX(salary) from s_emp);

2、AVGSUM
获取平均值、总和。
3、COUNT
统计记录的数量。

四、分组查询

1、GROUP BY
格式:select 组函数 from 表 group by 字段。

select count(last_name) from s_emp group by dept_id;

2、HAVING 组判断条件。
它的真假决定一组数据是否返回。
计算部门中最低工资小于700的部门平均工资。

select count(last_name) from s_emp group by dept_id having max(salary) > 2000;

五、查询语句的执行顺序

格式:select sum(salary) from 表名 where bool order by group by having
a、from 表名,先确定数据的来源。
b、where 确定表中的哪些数据有效。
c、group by 字段名,确定数据的分组依据。
d、having 确定组数据是否返回。
e、order by 对组数据进行排序。

六、关联查询

1、多表查询
select 字段 from 表1,表2 where;
2、多表查询时有相同字段怎么办
1、表名.字段名
2、表名如果太长,可以给表起别名 (from 表 别名)
3、笛卡尔积
a 8条数据 b 9条数据 = 笛卡尔积
在多表查询时,一定要设置where条件,否则将得到笛卡尔积。

七、连接查询

当使用多个表进行关联查询时,根据设置的条件会得到不同的结果(能匹配成功和不能匹配成功的)。
1、内连接:右边两连能匹配成功的数据。

select last_name,name from s_emp,s_dept where dept_id = s_dept.id;

2、外连接:左右两边不能匹配的数据。

select last_name,name from s_emp, left outer join s_dept on dept_id = s_dept.id。

3、左外连接:匹配成功的数据+左表不能匹配的数据

select last_name,name from s_emp, left outer join s_dept on dept_id = s_dept.id。

4、右外连接:匹配成功的数据+右表不能匹配的数据

select last_name,name from s_emp, right outer join s_dept on dept_id = s_dept.id。

5、全外连接:匹配成功的数据+左表不能匹配的数据+右表不能匹配的数据

select last_name,name from s_emp, full outer join s_dept on dept_id = s_dept.id。

免责声明:文章转载自《Oracle 数据库常用SQL语句(2)查询语句》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS pod封装和升级【转】C#字符串连接的效率问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Elasticsearch之批量操作bulk

1、bulk相当于数据库里的bash操作。 2、引入批量操作bulk,提高工作效率,你想啊,一批一批添加与一条一条添加,谁快? 3、bulk API可以帮助我们同时执行多个请求 4、bulk的格式: action:index/create/update/delete metadata:_index,_type,_id request body:_sourc...

DB2查询前100到后200之间的数据

select T.sicCd form Table T where T.sicCd not in ( select sicCd form Table fetch first 100 rows only ) fetch first 100 rows only...

@Mapper @Insert 注解式方法批量入库(ORACLE数据库)

方法一:使用 Insert All into 实现 1.创建实体类(DemoBean)方便处理数据 public classDemoBean { privateString demo1; privateString demo2; privateString demo3; privateString demo4; p...

SQL SERVER 2019新功能

1、错误代码行 BEGIN TRY    SELECT 1/0END TRYBEGIN CATCH    THROW END CATCH2、二进制截断列名值 create table #ls(a varchar(1))insert into #ls values('abc') 符串或二进制数据将在表“tempdb.dbo.#ls_00000000005B”...

oracle expdp/impdp 用法详解

http://hi.baidu.com/hzfsai/item/4a4b3fc4b1cf7e51ad00efbd oracle expdp/impdp 用法详解 Data Pump 反映了整个导出/导入过程的完全革新。不使用常见的 SQL 命令,而是应用专用 API(direct path api etc) 来以更快得多的速度加载和卸载数据。1.Data...

Windows下手动配置Oracle Client的要点

Windows下手动配置Oracle Client的要点我的Windows上原有Oracle 9的客户端, 想使用cx_Oracle和python些点东西, cx_Oracle官网上没有for python2.7和Oracle9的安装包, 但有for oracle10的安装包. 所以需要先安装Oracle 10 client. 可能是因为卸载Oracle...