xmxm76 发表于 2018-10-20 13:02:56

SQL 基础之where过滤和 运算符(五)

  使用WHERE子句,将不满足条件的行过滤掉:
  WHERE过滤的注意事项:
  WHERE子句紧随FROM子句。
  WHERE子句后跟条件表达式
  列名、表达式、常量
  比较运算符:=,   select last_name,job_id,department_id from employees where last_name = 'Whalen';

  3.2 查找入职日期为07年12月19日的员工first_name 和部门ID
  select first_name,department_id,hire_date from employees where hire_date = '19-DEC-07';

  4、比较运算符
操作符                        含义         =               等于      >               大于      >=                大于等于      <                小于      =3000 orader by salary;

  4.3 查找一下工资大于等于10000的员工姓名,并按照 xxx 's salary is 10000的格式输入
  select first_name||q'['s salary is ]' || salary as "salary is 10000 of emp"
  from employees
  where salary=10000;

  4.4查找一下工资在8000-10000之间的员工姓名和员工id
  select employee_id,first_name,salary from employees wheresalary between 8000 and 10000 order by salary;

  4.5 查找一下工资为7000、3100、8100、9000、10000的员工名字
  select first_name,salary from employees where salary in(7000,3100,8000,9000,10000);

  4.6 查找员工名称为S开头的job_id
  select first_name,job_id from employees where first_name like 'S%';

  4.7 查找员工名称中带有s的 job_id
  select first_name,job_id from employees where first_name like '%s';

  4.8 查找名字第二个字母是o的员工信息
  select first_name,job_id from employees where first_name like '_o%';

  4.9 查找名字第三个字母为e和a的员工名字
  select employee_id,last_name,salary,department_id from employees where manager_id= &mgr_num order by &order_col

  4.9.1 查找manager_id 为空的名字
  selectfirst_name,manager_idfrom employees where manager_id is null;

  4.9.2 查找到04年入职的员工姓名
  select last_name,hire_date from employees where hire_date like '%04';

  5、逻辑运算符
操作符含义AND逻辑并,两个条件都为“真”则返回TRUEOR逻辑或,其中一个条件为“真”则返回TRUENOT逻辑否,如果条件为“假”则返回TRUE  5.1、查找工资大于10000并且 job_id 含有MAN的员工信息
  select first_name,employee_id,salary from employees where salary>=1000 andjob_id like '%MAN%';

  5.2、查找工资大于等于10000 或者job_id 含有MAN的员工 的名字和employee_id
  select first_name,employee_id,job_id,salary from employees where salary>=10000orjob_idlike '%MAN%';

  5.3 查找job_id不在'HR_EMP','ST_MAN','ST_CLERK' 这工作id里面的员工名字和job_id
  select last_name,job_id from employeeswhere job_id not in('HR_EMP','ST_MAN','ST_CLERK');

  5.4列出工资不在 5000-12000 范围的员工的姓名和工资
  SQL >select last_name,salary from employees where salary not between 5000 and 12000 order by salary;
  LAST_NAME      SALARY
  ------------------------- ----------
  Olson2100
  Philtanker2200
  Markle2200
  Landry2400
  Gee       2400
  Vargas2500
  Patel2500
  Colmenares2500
  Marlow2500
  Sullivan       2500
  Perkins      2500
  OConnell       2600
  Grant2600
  Matos2600
  Himuro2600
  Mikkilineni2700
  Seo      2700
  Atkinson       2800
  Geoni2800
  Tobias2800
  Jones2800
  Rogers2900
  Baida2900
  Gates2900
  Feeney3000
  Cabrio3000
  Walsh3100
  Fleaur3100
  Khoo3100
  Davies3100
  Stiles3200
  Nayer3200
  Taylor3200
  McCain3200
  Bissot3300
  Mallin3300
  Dellinger      3400
  Rajs      3500
  Dilly      3600
  Ladwig3600
  Chung3800
  Everett         3900
  Bell      4000
  Bull      4100
  Sarchand      4200
  Lorentz         4200
  Whalen4400
  Pataballa      4800
  Austin4800
  Higgins               12008
  Greenberg       12008
  Hartstein               13000
  Partners               13500
  Russell                14000
  De Haan                17000
  Kochhar                17000
  King               24000
  57 rows selected.
  6、优先级(可以使用括号改变优先级顺序)
优先级1算数运算符2连接符3比较符4IS NULL, LIKE, IN 5 BETWEEN6不等于7NOT8AND9OR  1、查找部门为SA_REP或者AD_PRES部门里面工资大于15000的员工名字、job_id、工资等信息
  SELECT last_name, job_id, salary FROM employees WHERE job_id = 'SA_REP' OR job_id = 'AD_PRES' AND salary > 15000;

  2、查找job_id 为SA_REP或者AD_PRES部门里面工资大于15000的员工姓名、job_id
  SELECT last_name, job_id, salary FROM employees WHERE (job_id = 'SA_REP' OR job_id = 'AD_PRES') AND salary > 15000;

  3、列出工资不在 5000-12000,部门在 20 或 50 的员工的姓名和工资
  select last_name,department_id,salary from employees where salary not between 5000 and 12000 and department_id=20 or department_id=50;
  或
  select last_name,department_id,salary from employees where salary not between 5000 and 12000 and department_id in(20,50);

页: [1]
查看完整版本: SQL 基础之where过滤和 运算符(五)