1. 比较运算符
比较运算符,如=
, !=
, <
, >
, <=
, >=
,用于比较两个表达式。
SELECT * FROM employees WHERE age > 30;
2. 逻辑运算符
逻辑运算符包括AND
, OR
, NOT
,用于组合多个条件。
SELECT * FROM employees WHERE department = 'Sales' AND age < 40;
3. LIKE运算符
LIKE
运算符用于模式匹配,其中%
表示任意数量的字符,_
表示单个字符。
SELECT * FROM employees WHERE first_name LIKE 'J%';
4. BETWEEN运算符
BETWEEN
运算符用于选取介于两个值之间的数据范围。
SELECT * FROM employees WHERE age BETWEEN 25 AND 30;
5. IN运算符
IN
运算符用于指定括号中的任意值。
SELECT * FROM employees WHERE department IN ('Sales', 'Engineering', 'Marketing');
练习解析
比较运算符练习:
SELECT * FROM employees WHERE salary >= 50000;
返回所有工资大于或等于50000的员工记录。
逻辑运算符练习:
SELECT * FROM employees WHERE department = 'Marketing' OR age > 45;
返回营销部门的所有员工,或者年龄大于45岁的员工。
LIKE运算符练习:
SELECT * FROM employees WHERE last_name LIKE '_a%';
返回所有姓氏的第二个字母为’a’的员工记录。
BETWEEN运算符练习:
SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2020-12-31';
返回所有在2020年被聘用的员工记录。
IN运算符练习:
SELECT * FROM employees WHERE job_title IN ('Sales Manager', 'Software Developer');
返回所有职位为销售经理或软件开发者的员工记录。