#案例1:查询工资大于12000的员工的信息
select * from employees where salary >12000;
#案例2:查询部门编号不等于90号的员工姓名和部门编号
select first_name,last_name,department_id from employees where department_id<>90;
#案例1:查询工资在10000到20000之间的员工的员工名,工资以及奖金
select *from employees;
select first_name,last_name,salary, ifnull(commission_pct,0)*salary*12 from employeeswhere salary>10000 and salary<20000;
#案例2:查询部门编号不是在90到110之间([90,110])的,或者工资高于15000的员工信息
select * from employees where department_id<90 or department_id>110 or salary>15000;
select * from employees where not(department_id>=90 and department_id<=110) or salary>15000;
#案例1;查询员工名中包含字母a的员工信息(筛选条件不具体,模糊匹配)
select * from employees where first_name like '%a%';#%代表通配符,可以是任意一种字符(串)
#案例2:查询员工名中第三个字符为e,第五个字符为a的员工的员工名和工资
select first_name,salary from employees where first_name like '__e_a%';
#案例3:查询员工姓中第二个字符为_的员工的姓(使用转义字符\)
select last_name from employees where last_name like '_\_%';
#转移字符可以自己指定,使用关键字escape(设计这种用法的原因是查询条件中可能会用到转移字符本身代表的字符)
select last_name from employees where last_name like '_$_%' escape '$';
#案例4:查询部门编号大于等于100的员工的信息
select * from employees where department_id like '1__';
#案例1:查询员工编号在100到120之间([100,120])的所有员工的信息
select * from employees where employee_id>=100 and employee_id<=120;
select * from employees where employee_id between 100 and 120;
#案例1:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
select first_name,job_id from employees where job_id='IT_PROG' or job_id='AD_VP' or job_id='AD_PRES';
select first_name,job_id from employees where job_id in('IT_PROG','AD_VP','AD_PRES');
#案例1:查询没有奖金的员工的名和奖金率
select first_name,commission_pct from employees where commission_pct=null;#错误,=运算符不能判断NULL值
select first_name,commission_pct from employees where commission_pct is null;
(可以判断null值,也可以判断其他值)
#案例1:查询没有奖金的员工的名和奖金率
select first_name,commission_pct from employees where commission_pct <=> null;
#案例2:查询工资为12000的员工的信息
select * from employees where salary <=> 12000;
is null仅仅可以判断null值,可读性较高,建议使用
<=>既可以判断null值,又可以判断普通的数值,可读性较低
#1:查询员工号为176的员工姓名和部门号和年薪
select first_name,last_name,department_id,salary*12*(1+ifnull(commission_pct,0))as 年薪 from employees where employee_id=176;
#2. 查询没有奖金,且工资小于18000的员工的salary,last_name
select salary,last_name from employees where commission_pct is null and salary<18000;
#3. 查询job_id不为'IT_PROG'或者工资为12000的员工的信息
select * from employees where job_id <> 'IT_PROG' or salary=12000;
#4. 查看部门departments表的结构
desc departments;
#5. 查询部门departments表中涉及到了哪些位置编号
select distinct location_id from departments;
select * from employees where commission_pct like '%%' and last_name like '%%';
下一篇:JSP 概述(二)