MySQL查询习题

建立连个数据表,department表和employee表。


| d_id | d_name | function | d_address |
| ------------ | ------------ | ------------ | ------------ |
| 1001 | 人事部 | 人事管理 | 北京 |
| 1002 | 科研部 | 研发产品 | 成都 |
| 1003 | 生产部 | 产品生产 | 天津 |
| 1004 | 销售部 | 产品销售 | 上海 |


| id | name | sex | age | d_id | salary | address |
| :------------: |:------------: |: ------------:| :------------ | :------------: | :------------:| :------------ 😐
| 9001 | Aric | 男 | 25 | 1002 | 4000 | 北京市海淀区 |
| 9002 | Jim | 男 | 26 | 1001 | 2500 | 北京市昌平区 |
| 9003 | Tom | 男 | 20 | 1003 | 1500 | 湖南省永州市 |
| 9004 | Eric | 男 | 30 | 1001 | 3500 | 北京市顺义区 |
| 9005 | Lily | 女 | 21 | 1002 | 3000 | 北京市平谷县 |
| 9006 | Lucy | 女 | 27 | 1004 | 8000 | 上海市静安区 |
| 9007 | Jack | 男 | 23 | 1004 | 4000 | 天津市南开区 |


CREATE TABLE `department` (
  `d_id` int(10) NOT NULL,
  `d_name` varchar(20) NOT NULL,
  `function` varchar(20) DEFAULT NULL,
  `d_address` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`d_id`),
  UNIQUE KEY `d_id` (`d_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `employee` (
  `id` int(10) NOT NULL,
  `name` varchar(20) NOT NULL,
  `sex` varchar(4) DEFAULT NULL,
  `age` int(5) DEFAULT NULL,
  `d_id` varchar(20) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  `address` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

然后在department表和employee表查询记录。
查询的要求如下:
1. 计算所有员工的出生年份,并显示结果中的字段别名为birth_year;

mysql> select name,2017-age as birty_year from employee ;
+------+------------+
| name | birty_year |
+------+------------+
| Aric |       1992 |
| Jim  |       1991 |
| Tom  |       1997 |
| Eric |       1987 |
| Lily |       1996 |
| Lucy |       1990 |
| Jack |       1994 |
+------+------------+
7 rows in set (0.00 sec)
  1. 用limit关键字来查询工资最低的员工的信息;
mysql> select * from employee order by salary limit 0,1;
+------+------+------+------+------+--------+--------------------+
| id   | name | sex  | age  | d_id | salary | address            |
+------+------+------+------+------+--------+--------------------+
| 9003 | Tom  | 男   |   20 | 1003 |   1500 | 湖南省永州市       |
+------+------+------+------+------+--------+--------------------+
  1. 计算男性与女性员工的平均工资;
mysql> select sex,avg(salary) from employee group by sex;                                                       +------+-------------+
| sex  | avg(salary) |
+------+-------------+
| 女   |        5500 |
| 男   |        3100 |
+------+-------------+
2 rows in set (0.01 sec)
  1. 查询在上海工作的员工姓名、性别、年龄和部门名称;
select name,sex,age,department.d_name,d_address from employee,department where employee.d_id=(select d_id from department where d_address='上海') and employee.d_id=department.d_id;
+------+------+------+-----------+-----------+
| name | sex  | age  | d_name    | d_address |
+------+------+------+-----------+-----------+
| Lucy | 女   |   27 | 销售部    | 上海      |
| Jack | 男   |   23 | 销售部    | 上海      |
+------+------+------+-----------+-----------+
2 rows in set (0.00 sec)
  1. 用右连接的方式查询department表和employee表;
mysql> select name,sex,age,department.d_name,d_address,function from employee right join department on employee.d_id=department.d_id;
+------+------+------+-----------+-----------+--------------+
| name | sex  | age  | d_name    | d_address | function     |
+------+------+------+-----------+-----------+--------------+
| Aric | 男   |   25 | 科研部    | 成都      | 研发产品     |
| Jim  | 男   |   26 | 人事部    | 北京      | 人事管理     |
| Tom  | 男   |   20 | 生产部    | 天津      | 产品生产     |
| Eric | 男   |   30 | 人事部    | 北京      | 人事管理     |
| Lily | 女   |   21 | 科研部    | 成都      | 研发产品     |
| Lucy | 女   |   27 | 销售部    | 上海      | 产品销售     |
| Jack | 男   |   23 | 销售部    | 上海      | 产品销售     |
+------+------+------+-----------+-----------+--------------+
7 rows in set (0.00 sec)
  1. 查询名字以字母T为开头的员工姓名、性别、年龄、部门和工作地点;
mysql> select name,sex,age,department.d_name,d_address from employee,department where name like "^T" and employee.d_id=department.d_id;
+------+------+------+-----------+-----------+
| name | sex  | age  | d_name    | d_address |
+------+------+------+-----------+-----------+
| Tom  | 男   |   20 | 生产部    | 天津      |
+------+------+------+-----------+-----------+
1 row in set (0.00 sec)
  1. 查询年龄小于25或者大于30岁的员工信息;
mysql> select * from employee where age>30 or age<25;
+------+------+------+------+------+--------+--------------------+
| id   | name | sex  | age  | d_id | salary | address            |
+------+------+------+------+------+--------+--------------------+
| 9003 | Tom  | 男   |   20 | 1003 |   1500 | 湖南省永州市       |
| 9005 | Lily | 女   |   21 | 1002 |   3000 | 北京市平谷县       |
| 9007 | Jack | 男   |   23 | 1004 |   4000 | 天津市南开区       |
+------+------+------+------+------+--------+--------------------+
  1. 查询家住永州市的员工的信息。
mysql> select * from employee where address like "%永州%";
+------+------+------+------+------+--------+--------------------+
| id   | name | sex  | age  | d_id | salary | address            |
+------+------+------+------+------+--------+--------------------+
| 9003 | Tom  | 男   |   20 | 1003 |   1500 | 湖南省永州市       |
+------+------+------+------+------+--------+--------------------+
1 row in set (0.00 sec)