Oracle 数据库 简单查询

 1 select DISTINCT dept_id from s_emp;
 2 desc s_emp;
 3 select * from s_emp where id <= 10;
 4 
 5 --给入职3年以上员工发10万元年终奖
 6 select last_name,salary from s_emp where months_between(START_DATE,sysdate) > 870;
 7 --列出职位是仓库管理员的名字和工资
 8 select last_name,salary,title from s_emp where title = 'Stock Clerk';
 9 
10 select LAST_NAME,salary from s_emp where last_name in ('Smith','Chang','Patel');
11 
12 select * from s_emp where salary BETWEEN 1100 and 1450;
13 
14 select * from s_emp 
15     where salary > all(
16         select salary from s_emp where title = 'Stock Clerk');
17         
18 select DISTINCT title from s_emp where salary BETWEEN 1000 and 1500;
19 
20 select * from s_emp where title = 'Stock Clerk' ORDER BY salary;
21 select * from s_emp ORDER BY salary desc;
22 select * from s_emp ORDER BY salary;
23 select MIN(salary) from s_emp;
24 
25 INSERT INTO s_emp VALUES (
26    26, 'lingling', 'sun', 'hehe',
27    to_date('05-9-18','dd-mm-yy'),NULL, 1, 'Stock Clerk',
28    41, NULL,NULL);
29 INSERT INTO s_emp VALUES (
30    27, 'lingling', 'sun', 'haha',
31    to_date('05-9-18','dd-mm-yy'),NULL, 1, 'Stock Clerk',
32    41, NULL,NULL);
33 commit;
34 
35 select * from s_emp;
36 select * from s_region;
37 select * from s_warehouse;
38 
39 select MIN(salary) from s_emp;
40 select avg(nvl(salary,0)) from s_emp;
41 
42 select count(last_name) from s_emp where salary < 1000;
43 
44 select count(last_name) from s_emp GROUP BY dept_id;
45 select DISTINCT dept_id from s_emp;
46 
47 select count(last_name) from s_emp group by dept_id;
48 select MAX(salary) from s_emp group by dept_id;
49 select sum(salary) from s_emp group by dept_id HAVING max(salary) > 2000;
50 
51 select avg(salary) from s_emp group by dept_id HAVING min(salary) < 1500;
52 select last_name,name from s_emp,s_dept where dept_id = s_dept.id;
53 select count(last_name),name from s_emp,s_dept where dept_id = s_dept.id group by name;
54 
55 select count(e.id),d.id from s_emp e,s_dept d where dept_id = d.id group by d.id;
56 
57 select count(id) from s_dept;
58 select count(id) from s_emp;
59 select count(s_dept.id) from s_dept,s_emp;
60 
61 select s_warehouse.id,last_name from s_emp full outer join s_warehouse on s_emp.id = s_warehouse.manager_id;
一、SQL基础查询
    1、select语句
    格式:select 字段 from 表名;
    2、where 用于限制查询的结果。
   
    3、查询条件 > < >= <= = !=
   
    练习1:查询工资低于2000的员工。
   
    练习2:查询部门id不是10的员工。
   
    练习3:查询91年后入职的员工。
   
    4、与 或(AND,OR)
    练习:查询工资低于2000的员工,并且部门id不是10的员工,并且91年后入职的员工。
   
    5、在 不在(IN,NOT IN)
    练习:查询工号为1,9,11,16且工资低于1000的员工。
   
    6、在 [a,b] (between  val1 and val2)
    练习:查询工资大于1000,小于1500的员工。
   
    7、空 非空(IS NULL,NOT NULL)
    练习:查询公司中最高领导的名字。  
   
    8、全部 任一(ALL,ANY)
        不能单独使用,必须要与关系运算符配合。
    练习:查询职位是仓库管理员的工资。
    练习:查询工资比任一仓库管理员高的员工信息。
    练习:查询工资比所有仓库管理员高的员工信息。
   
    问题:从s_emp 表查询出所有的部门id。
    9、排重 DISTINCT
    练习:工资在1000~1500之间的职位有哪些。
   
二、排序
    1、使用 ORDER BY 语句
        格式:select 字段 from 表名 where 条件 ORDER BY 字段;
        练习:显示员工名字、职位、工号,按年薪从低到高的排列显示。
       
    2、设置升序降序(ASC,DESC)
        格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC;
        练习:显示员工名字、职位、工号,按年薪从高到低的排列显示。
       
    3、多项排序
        格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC,字段 ASC|DESC;
三、聚合函数
    注意:在使用比较运算符时NULL为最大值,在排序时也会受影响。
    把select语句的多条查询结果,汇聚成一个结果,这样的函数叫聚合函数。
   
    1、MAXMIN
    获取最大值和最小值,可以是任何数据类型,但只能获取一个字段。
   
    2、AVGSUM
    获取平均值、总和。
   
    3、COUNT
    统计记录的数量。
   
四、分组查询
    1、GROUP BY
    格式:select 组函数 from 表 group by 字段。
    2、HAVING 组判断条件。
    它的真假决定一组数据是否返回。
    练习:计算部门中最低工资小于700的部门平均工资。
   
五、查询语句的执行顺序
    格式: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、多表查询时有相同字段怎么办
        问题:显示每个部门中有多少个员工,部门ID-员工数。
        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, 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。
       
原文地址:https://www.cnblogs.com/mingyoujizao/p/9602771.html