跳转至

where

使用 where

创建一个名为 tb_employee 的表,该表包含了员工的信息,包括员工ID、姓名、年龄、部门ID和薪水。并插入数据

-- 创建表 tb_employee,用于存储员工信息
CREATE TABLE tb_employee (
    id INT(11), -- 员工ID
    name VARCHAR(25), -- 员工姓名
    age INT(11), -- 员工年龄
    deptId INT(11), -- 员工所属部门ID
    salary FLOAT -- 员工薪水
);

-- 向 tb_employee 表插入数据
INSERT INTO tb_employee (id, name, age, deptId, salary)
VALUES
    (1, 'John', 25, 1, 5000.00), -- ID为1的员工名为John,年龄25岁,部门ID为1,薪水为5000.00
    (2, 'Emily', 30, 2, 6000.00), -- ID为2的员工名为Emily,年龄30岁,部门ID为2,薪水为6000.00
    (3, 'Michael', 28, 1, 5500.00), -- ID为3的员工名为Michael,年龄28岁,部门ID为1,薪水为5500.00
    (4, 'Sarah', 32, 3, 7000.00); -- ID为4的员工名为Sarah,年龄32岁,部门ID为3,薪水为7000.00

查询所有行

select * from tb_employee; 

查询年龄大于等于 30 岁的员工

select * from tb_employee where age > 30;  

查询工资高于 6000 的员工的姓名和工资

select name,salary from tb_employee where salary > 6000; 

查询部门 ID 为 1 的员工的所有信息

select * from tb_employee where deptId=1;

查询年龄在 25 到 30 岁之间,并且工资大于 5000 的员工:

select * from tb_employee where age between 20 and 30 and salary > 5000;

查询部门 ID 为 1 或 2 的员工:

select * from tb_employee where deptId in (1,2); 

查询姓名以字母 "J" 开头的员工:

select * from tb_employee where name like 'J%'; 

查询年龄最大的员工


查询工资排名前三的员工


查询年龄小于 30 岁或工资大于 6000 的员工,并按照工资降序排序:


查询部门 ID 为 1 的员工,并根据年龄升序排序:


查询年龄大于平均年龄的员工:


查询名字以 "J" 开头且包含 "ohn" 的员工:


查询工资在 5000 到 7000 之间,且部门 ID 不为 3 的员工: