SQL单表多表查询

课堂练习代码:

复制代码
一、单表查询
1、显示所有职工的基本信息。
mysql> select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   |  出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
   
2、查询所有职工所属部门的部门号,不显示重复的部门号。
mysql> select distinct `部门号` from worker;
+-----------+
| 部门号    |
+-----------+
|       101 |
|       102 |
+-----------+
2 rows in set (0.00 sec)
  
3、求出所有职工的人数。
mysql> select count(姓名) as `人数` from worker;
+--------+
| 人数   |
+--------+
|      6 |
+--------+
1 row in set (0.00 sec)
  
4、列出最高工和最低工资。
mysql> select max(工资) as `最高工资` from worker;
+--------------+
| 最高工资     |
+--------------+
|      8500.00 |
+--------------+
1 row in set (0.00 sec)
 
mysql> select min(工资) as `最低工资` from worker;
+--------------+
| 最低工资     |
+--------------+
|      3200.00 |
+--------------+
1 row in set (0.00 sec)
   
5、列出职工的平均工资和总工资。
mysql> select avg(工资) as `平均工资`,sum(工资) as `总工资` from worker;
+--------------+-----------+
| 平均工资     | 总工资    |
+--------------+-----------+
|  5000.000000 |  30000.00 |
+--------------+-----------+
1 row in set (0.00 sec)
   
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。 
mysql> create table 工作日期表(
    -> 职工号 int primary key,
    -> 姓名 varchar(50),
    -> 参加工作 date
    -> );
Query OK, 0 rows affected (0.10 sec)
 
7、显示所有职工的年龄。
mysql> select 姓名,year(curdate())-year(出生日期) as 年龄 from worker;
+--------+--------+
| 姓名   | 年龄   |
+--------+--------+
| 张三   |     35 |
| 李四   |     28 |
| 王亮   |     42 |
| 赵六   |     31 |
| 钱七   |     33 |
| 孙八   |     29 |
+--------+--------+
6 rows in set (0.00 sec) 
 
8、列出所有姓张的职工的职工号、姓名和出生日期。
mysql> select 职工号,姓名,出生日期 from worker where 姓名 like '张%';
+-----------+--------+--------------+
| 职工号    | 姓名   | 出生日期     |
+-----------+--------+--------------+
|      1001 | 张三   | 1990-07-01   |
+-----------+--------+--------------+
1 row in set (0.00 sec)
 
9、列出1990年以前出生的职工的姓名、参加工作日期。
mysql> select 姓名,工作时间 as 参加工作时间 from worker where year(出生日期) < 1990;
+--------+--------------------+
| 姓名   | 参加工作时间       |
+--------+--------------------+
| 王亮   | 2011-01-04         |
+--------+--------------------+
1 row in set (0.00 sec)
 
10、列出工资在3000-5000之间的所有职工姓名。
 
mysql> select 姓名 from worker where 工资 >= 3000 and 工资 <= 5000;
+--------+
| 姓名   |
+--------+
| 张三   |
| 李四   |
| 钱七   |
| 孙八   |
+--------+
4 rows in set (0.00 sec)
 
11、列出所有陈姓和李姓的职工姓名。
mysql> select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
+--------+
| 姓名   |
+--------+
| 李四   |
+--------+
1 row in set (0.00 sec)
 
12、列出所有部门号为102和103的职工号、姓名、党员否。
mysql> select 职工号,姓名,政治面貌 from worker where 部门号 = 102 or 部门号
= 103;
+-----------+--------+--------------+
| 职工号    | 姓名   | 政治面貌     |
+-----------+--------+--------------+
|      1003 | 王亮   | 党员         |
|      1004 | 赵六   | 群众         |
|      1005 | 钱七   | 党员         |
|      1006 | 孙八   | 党员         |
+-----------+--------+--------------+
4 rows in set (0.00 sec)
 
13、将职工表worker中的职工按出生的先后顺序排序。
mysql> select * from worker order by year(出生日期);
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   |  出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
 
14、显示工资最高的前3名职工的职工号和姓名。 
mysql> select 职工号,姓名 from worker order by 工资 limit 3;
+-----------+--------+
| 职工号    | 姓名   |
+-----------+--------+
|      1002 | 李四   |
|      1001 | 张三   |
|      1006 | 孙八   |
+-----------+--------+
3 rows in set (0.00 sec)
 
15、求出各部门党员的人数。
mysql> select 部门号,count(*) as 党员人数 from worker where 政治面貌 = '党员
' group by 部门号;
+-----------+--------------+
| 部门号    | 党员人数     |
+-----------+--------------+
|       102 |            3 |
+-----------+--------------+
1 row in set (0.00 sec)
 
16、统计各部门的工资和平均工资
mysql> select 部门号,avg(工资) as `平均工资`,sum(工资) as `总工资` from work
er group by 部门号;
+-----------+--------------+-----------+
| 部门号    | 平均工资     | 总工资    |
+-----------+--------------+-----------+
|       101 |  3350.000000 |   6700.00 |
|       102 |  5825.000000 |  23300.00 |
+-----------+--------------+-----------+
2 rows in set (0.00 sec)
 
17、列出总人数大于4的部门号和总人数。
mysql> select 部门号,count(*) from worker group by 部门号 having count(*) >=4;
+-----------+----------+
| 部门号    | count(*) |
+-----------+----------+
|       102 |        4 |
+-----------+----------+
1 row in set (0.00 sec)

二、多表查询
1.创建student和score表
mysql> CREATE TABLE student (
    ->     id INT(10) NOT NULL UNIQUE PRIMARY KEY,
    ->     name VARCHAR(20) NOT NULL,
    ->     sex VARCHAR(4),
    ->     birth YEAR,
    ->     department VARCHAR(20),
    ->     address VARCHAR(50)
    -> );
创建score表。SQL代码如下:
mysql> CREATE TABLE score (
    ->     id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
    ->     stu_id INT(10) NOT NULL,
    ->     c_name VARCHAR(20),
    ->     grade INT(10)
    -> );
Query OK, 0 rows affected (0.03 sec)
 
2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
mysql> INSERT INTO student VALUES(901, '张老大', '男', 1985, '计算机系', '北京市海淀区');
Query OK, 1 row affected (0.03 sec)
 
mysql> INSERT INTO student VALUES(902, '张老二', '男', 1986, '中文系', '北京市昌平区');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO student VALUES(903, '张三', '女', 1990, '中文系', '湖南省永州市');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO student VALUES(904, '李四', '男', 1990, '英语系', '辽宁省阜新市');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO student VALUES(905, '王五', '女', 1991, '英语系', '福建省厦门市');
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO student VALUES(906, '王六', '男', 1988, '计算机系', '湖南省衡阳市');
Query OK, 1 row affected (0.00 sec)
向score表插入记录的INSERT语句如下:
mysql> INSERT INTO score VALUES(NULL,901, '计算机',98);
Query OK, 1 row affected (0.02 sec)
 
mysql> INSERT INTO score VALUES(NULL,901, '英语', 80);
Query OK, 1 row affected (0.01 sec)
 
mysql> INSERT INTO score VALUES(NULL,902, '计算机',65);
Query OK, 1 row affected (0.01 sec)
 
mysql> INSERT INTO score VALUES(NULL,902, '中文',88);
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO score VALUES(NULL,903, '中文',95);
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO score VALUES(NULL,904, '计算机',70);
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO score VALUES(NULL,904, '英语',92);
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO score VALUES(NULL,905, '英语',94);
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO score VALUES(NULL,906, '计算机',90);
Query OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO score VALUES(NULL,906, '英语',85);
Query OK, 1 row affected (0.01 sec)
 
3.查询student表的所有记录
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
 
4.查询student表的第2条到4条记录
mysql> select * from student limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | birth | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 张老二    | 男   |  1986 | 中文系     | 北京市昌平区       |
| 903 | 张三      | 女   |  1990 | 中文系     | 湖南省永州市       |
| 904 | 李四      | 男   |  1990 | 英语系     | 辽宁省阜新市       |
+-----+-----------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)
 
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
mysql> select id,name,department from student;
+-----+-----------+--------------+
| id  | name      | department   |
+-----+-----------+--------------+
| 901 | 张老大    | 计算机系     |
| 902 | 张老二    | 中文系       |
| 903 | 张三      | 中文系       |
| 904 | 李四      | 英语系       |
| 905 | 王五      | 英语系       |
| 906 | 王六      | 计算机系     |
+-----+-----------+--------------+
6 rows in set (0.00 sec)
 
6.从student表中查询计算机系和英语系的学生的信息
mysql> select * from student where department = '计算机系' or department = '
英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)
 
7.从student表中查询年龄18~35岁的学生信息
mysql> select * from student where year(curdate())-birth >= 18 and year(curdate())-birth <= 35;
+-----+--------+------+-------+------------+--------------------+
| id  | name   | sex  | birth | department | address            |
+-----+--------+------+-------+------------+--------------------+
| 903 | 张三   | 女   |  1990 | 中文系     | 湖南省永州市       |
| 904 | 李四   | 男   |  1990 | 英语系     | 辽宁省阜新市       |
| 905 | 王五   | 女   |  1991 | 英语系     | 福建省厦门市       |
+-----+--------+------+-------+------------+--------------------+
3 rows in set (0.00 sec)
 
8.从student表中查询每个院系有多少人
mysql> select department,count(*) as 人数 from student group by department;
 
+--------------+--------+
| department   | 人数   |
+--------------+--------+
| 计算机系     |      2 |
| 中文系       |      2 |
| 英语系       |      2 |
+--------------+--------+
3 rows in set (0.00 sec)
 
9.从score表中查询每个科目的最高分
mysql> select c_name,max(grade) from score group by c_name;
+-----------+------------+
| c_name    | max(grade) |
+-----------+------------+
| 计算机    |         98 |
| 英语      |         94 |
| 中文      |         95 |
+-----------+------------+
3 rows in set (0.00 sec)
 
10.查询李四的考试科目(c_name)和考试成绩(grade)
mysql> select c_name, grade from score where stu_id = (select id from studen
t where name = '李四');
+-----------+-------+
| c_name    | grade |
+-----------+-------+
| 计算机    |    70 |
| 英语      |    92 |
+-----------+-------+
2 rows in set (0.00 sec)
 
11.用连接的方式查询所有学生的信息和考试信息
mysql> select * from student left join score on student.id = score.stu_id;
+-----+-----------+------+-------+--------------+--------------------+------+--------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | id   | stu_id | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+------+--------+-----------+-------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |    1 |    901 | 计算机    |    98 |
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |    2 |    901 | 英语      |    80 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |    3 |    902 | 计算机    |    65 |
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |    4 |    902 | 中文      |    88 |
| 903 | 张三      | 女   |  1990 | 中文系       | 湖南省永州市       |    5 |    903 | 中文      |    95 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |    6 |    904 | 计算机    |    70 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |    7 |    904 | 英语      |    92 |
| 905 | 王五      | 女   |  1991 | 英语系       | 福建省厦门市       |    8 |    905 | 英语      |    94 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |    9 |    906 | 计算机    |    90 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |   10 |    906 | 英语      |    85 |
+-----+-----------+------+-------+--------------+--------------------+------+--------+-----------+-------+
10 rows in set (0.00 sec)
12.计算每个学生的总成绩
mysql> select name,sum(grade) from student left join score on student.id=sco
re.stu_id group by student.name;
+-----------+------------+
| name      | sum(grade) |
+-----------+------------+
| 张老大    |        178 |
| 张老二    |        153 |
| 张三      |         95 |
| 李四      |        162 |
| 王五      |         94 |
| 王六      |        175 |
+-----------+------------+
6 rows in set (0.00 sec)
 
13.计算每个考试科目的平均成绩
mysql> select c_name,avg(grade) from student left join score on student.id=s
core.stu_id group by c_name;
+-----------+------------+
| c_name    | avg(grade) |
+-----------+------------+
| 计算机    |    80.7500 |
| 英语      |    87.7500 |
| 中文      |    91.5000 |
+-----------+------------+
3 rows in set (0.00 sec)
 
14.查询计算机成绩低于95的学生信息
mysql> select * from student left join score on student.id=score.stu_id where c_name='计算机' and grade < 95;
+-----+-----------+------+-------+--------------+--------------------+------+--------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | id   | stu_id | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+------+--------+-----------+-------+
| 902 | 张老二    | 男   |  1986 | 中文系       | 北京市昌平区       |    3 |    902 | 计算机    |    65 |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |    6 |    904 | 计算机    |    70 |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |    9 |    906 | 计算机    |    90 |
+-----+-----------+------+-------+--------------+--------------------+------+--------+-----------+-------+
3 rows in set (0.00 sec)
 
15.查询同时参加计算机和英语考试的学生的信息
mysql> SELECT student.*
    -> FROM student
    -> WHERE student.id IN (
    ->     SELECT stu_id
    ->     FROM score
    ->     WHERE c_name IN ('计算机', '英语')
    ->     GROUP BY stu_id
    ->     HAVING COUNT(DISTINCT c_name) = 2
    -> );
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大    | 男   |  1985 | 计算机系     | 北京市海淀区       |
| 904 | 李四      | 男   |  1990 | 英语系       | 辽宁省阜新市       |
| 906 | 王六      | 男   |  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)
 
16.将计算机考试成绩按从高到低进行排序
mysql> select grade from student left join score on student.id=score.stu_id where c_name='计算机' order by grade desc;
+-------+
| grade |
+-------+
|    98 |
|    90 |
|    70 |
|    65 |
+-------+
4 rows in set (0.00 sec)
 
17.从student表和score表中查询出学生的学号,然后合并查询结果
mysql> select id as student_id from student union select stu_id as student_id from score;
+------------+
| student_id |
+------------+
|        901 |
|        902 |
|        903 |
|        904 |
|        905 |
|        906 |
+------------+
6 rows in set (0.00 sec)
 
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
mysql> select s.name, s.department, sc.c_name, sc.grade from student s left join score sc on s.id = sc.stu_id where s.name like '张%' OR s.name like '王%';
+-----------+--------------+-----------+-------+
| name      | department   | c_name    | grade |
+-----------+--------------+-----------+-------+
| 张老大    | 计算机系     | 计算机    |    98 |
| 张老大    | 计算机系     | 英语      |    80 |
| 张老二    | 中文系       | 计算机    |    65 |
| 张老二    | 中文系       | 中文      |    88 |
| 张三      | 中文系       | 中文      |    95 |
| 王五      | 英语系       | 英语      |    94 |
| 王六      | 计算机系     | 计算机    |    90 |
| 王六      | 计算机系     | 英语      |    85 |
+-----------+--------------+-----------+-------+
8 rows in set (0.00 sec)
 
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
mysql> select s.name, (year(curdate()) - s.birth) as age, s.department, sc.c_name, sc.grade from student s left join score sc ON s.id = sc.stu_id where s.address like '湖南%';
+--------+------+--------------+-----------+-------+
| name   | age  | department   | c_name    | grade |
+--------+------+--------------+-----------+-------+
| 张三   |   35 | 中文系       | 中文      |    95 |
| 王六   |   37 | 计算机系     | 计算机    |    90 |
| 王六   |   37 | 计算机系     | 英语      |    85 |
+--------+------+--------------+-----------+-------+
3 rows in set (0.00 sec)
相关推荐
恒悦sunsite6 小时前
Ubuntu之apt安装ClickHouse数据库
数据库·clickhouse·ubuntu·列式存储·8123
奥尔特星云大使6 小时前
MySQL 慢查询日志slow query log
android·数据库·mysql·adb·慢日志·slow query log
来自宇宙的曹先生6 小时前
MySQL 存储引擎 API
数据库·mysql
间彧7 小时前
MySQL Performance Schema详解与实战应用
数据库
间彧7 小时前
MySQL Exporter采集的关键指标有哪些,如何解读这些指标?
数据库
weixin_446260857 小时前
Django - 让开发变得简单高效的Web框架
前端·数据库·django
mpHH7 小时前
babelfish for postgresql 分析--todo
数据库·postgresql
zizisuo7 小时前
解决在使用Lombok时maven install 找不到符号的问题
java·数据库·maven
程序边界9 小时前
国产之光!金仓数据库KingbaseES Oracle兼容性深度体验大赏
数据库·oracle
A阳俊yi9 小时前
Spring——声明式事务
java·数据库·spring