自学内容网 自学内容网

按照要求完成如下DQL语句编写

题目来源于黑马MySQL

创建表格:

CREATE TABLE TB_EMPLOYEE

(

  ID      INT COMMENT '编号',

  WORK_NO   VARCHAR(10) COMMENT '工号',

  NAME     VARCHAR(10) COMMENT '姓名',

  GENDER    CHAR COMMENT '性别',

  AGE     TINYINT UNSIGNED COMMENT '年龄',

  ID_CARD   CHAR(18) COMMENT '身份证号',

  WORK_ADDRESS VARCHAR(50) COMMENT '工作地址',

  ENTRY_DATE  DATE COMMENT '入职时间'

) COMMENT '员工表';

# ================================

INSERT INTO TB_EMPLOYEE (ID, WORK_NO, NAME, GENDER, AGE, ID_CARD, WORK_ADDRESS, ENTRY_DATE)

VALUES (1, '1', '柳岩', '女', 20, '123456789012345678', '北京', '2000-01-01'),

    (2, '2', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01'),

    (3, '3', '韦一笑', '男', 38, '123456789012345670', '上海', '2005-08-01'),

    (4, '4', '赵敏', '女', 18, '123456789012345670', '北京', '2009-12-01'),

    (5, '5', '小昭', '女', 16, '123456789012345678', '上海', '2007-07-01'),

    (6, '6', '杨逍', '男', 28, '12345678901234567X', '北京', '2006-01-01'),

    (7, '7', '范瑶', '男', 40, '123456789012345670', '北京', '2005-05-01'),

    (8, '8', '黛绮丝', '女', 38, '123456789012345670', '天津', '2015-05-01'),

    (9, '9', '范凉凉', '女', 45, '123456789012345678', '北京', '2010-04-01'),

    (10, '10', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01'),

    (11, '11', '张士诚', '男', 55, '123456789012345670', '江苏', '2015-05-01'),

    (12, '12', '常遇春', '男', 32, '123456789012345670', '北京', '2004-02-01'),

    (13, '13', '张三丰', '男', 88, '123456789012345678', '江苏', '2020-11-01'),

    (14, '14', '灭绝', '女', 65, '123456789012345670', '西安', '2019-05-01'),

    (15, '15', '胡青牛', '男', 70, '12345678901234567X', '西安', '2018-04-01'),

    (16, '16', '周芷若', '女', 18, NULL, '北京', '2012-06-01');

查看表格

给表格重新起一个名字

alter table TB_EMPLOYEE rename emp;

 开始练习:

1、查询年龄为20,21,22,23的员工信息;

select * from emp where age in(20,21,22.23);
select * from emp where age between 20 and 23;
select * from emp where age>=20 and age<=23;

2、查询性别为男,并且年龄在20-40岁(包含)的姓名为三个字的员工  

select *from emp where gender='男' and age between 20 and 40 and name like '___';

3、统计员工表中,年龄小于60岁的,男性员工和女性员工的人数  

select gender, count(*) from emp where age<60 group by gender;

3.1、统计员工表中,年龄小于60岁的,男性员工和女性员工的人数大于6的性别人数

select gender, count(*) from emp where age<60 group by gender having count(*)>6;

4、查询所有年龄小于等于35岁的员工的姓名和年龄,并对查询的结果按年龄升序,如果年龄相同按入职时间降序排序

select name,age from emp where age<=35 order by age,ENTRY_DATE desc;
select name,age from emp where age<=35 order by age asc,ENTRY_DATE desc;

5、查询性别为男,且年龄在20-40岁(包含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序

select * from emp where gender ='男' and age between 20 and 40 order by age,ENTRY_DATE limit 5;
select * from emp where gender ='男' and age between 20 and 40 order by age asc,ENTRY_DATE asc limit 5;


原文地址:https://blog.csdn.net/wjl990316fddwjl/article/details/135653576

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!