MySQL实操学习(一)
mysql任务一
1、项目一:查找重复的电子邮箱(难度:简单)
创建 email表,并插入如下三行数据
±—±--------+
| Id | Email |
±—±--------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
±—±--------+
编写一个 SQL 查询,查找 Email 表中所有重复的电子邮箱。
根据以上输入,你的查询应返回以下结果:
±--------+
| Email |
±--------+
| a@b.com |
±--------+
说明:所有电子邮箱都是小写字母。
解答
-- 项目一 查找重复的电子邮箱
-- 创建表
CREATE TABLE email (
ID INT NOT NULL PRIMARY KEY,
Email VARCHAR(255)
);
-- 插入数据
INSERT INTO email VALUES('1','a@b.com');
INSERT INTO email VALUES('2','c@d.com');
INSERT INTO email VALUES('3','a@b.com');
-- 作业解答
select Email
from email
group by Email
having count(Email) >1;
2、项目二:查找大国(难度:简单)
创建如下 World 表
±----------------±-----------±-----------±-------------±--------------+
| name | continent | area | population | gdp |
±----------------±-----------±-----------±-------------±--------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
±----------------±-----------±-----------±-------------±--------------+
如果一个国家的面积超过300万平方公里,或者(人口超过2500万并且gdp超过2000万),那么这个国家就是大国家。
编写一个SQL查询,输出表中所有大国家的名称、人口和面积。
例如,根据上表,我们应该输出:
±-------------±------------±-------------+
| name | population | area |
±-------------±------------±-------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
±-------------±------------±-------------+
解答
-- 项目二 查找大国,输出表中所有大国家的名称、人口和面积
-- 创建表
CREATE TABLE World (
name VARCHAR(50) NOT NULL,
continent VARCHAR(50) NOT NULL,
area INT NOT NULL,
population INT NOT NULL,
gdp INT NOT NULL
);
-- 插入数据
INSERT INTO World VALUES('Afghanistan','Asia',652230,25500100,20343000);
INSERT INTO World VALUES('Albania','Europe',28748,2831741,12960000);
INSERT INTO World VALUES('Algeria','Africa',2381741,37100000,188681000);
INSERT INTO World VALUES('Andorra','Europe',468,78115,3712000);
INSERT INTO World VALUES('Angola','Africa',1246700,20609294,100990000);
-- 作业解答
select name,population,area
from World
where area >3000000
or (population >25000000 and gdp >20000000);
mysql任务二
3、项目三:超过5名学生的课(难度:简单)
创建如下所示的courses 表 ,有: student (学生) 和 class (课程)。
例如,表:
±--------±-----------+
| student | class |
±--------±-----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
| A | Math |
±--------±-----------+
编写一个 SQL 查询,列出所有超过或等于5名学生的课。
应该输出:
±--------+
| class |
±--------+
| Math |
±--------+
Note:
学生在每个课中不应被重复计算。
解答
-- 项目三 查询所有超过或等于5名学生的课
-- 创建数据库
create database test2database;
use test2database;
--创建表
create table courses(
student varchar(10) not null,
class varchar(50) not null
);
-- 插入数据
insert into courses values('A','Math');
insert into courses values('B','English');
insert into courses values('C','Math');
insert into courses values('D','Biology');
insert into courses values('E','Math');
insert into courses values('F','Computer');
insert into courses values('G','Math');
insert into courses values('H','Math');
insert into courses values('I','Math');
insert into courses values('A','Math');
-- 查看表
select * from courses;
+---------+----------+
| student | class |
+---------+----------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
| A | Math |
+---------+----------+
-- 解答
select class from courses
group by class
having count(distinct(student))>=5;
-- 结果
+-------+
| class |
+-------+
| Math |
+-------+
4、项目四:交换工资(难度:简单)
创建一个 salary表,如下所示,有m=男性 和 f=女性的值 。
例如:
id | name | sex | salary |
---|---|---|---|
1 | A | m | 2500 |
2 | B | f | 1500 |
3 | C | m | 5500 |
4 | D | f | 500 |
交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求使用一个更新查询,并且没有中间临时表。
运行你所编写的查询语句之后,将会得到以下表:
id | name | sex | salary |
---|---|---|---|
1 | A | f | 2500 |
2 | B | m | 1500 |
3 | C | f | 5500 |
4 | D | m | 500 |
解答
-- 项目四 交换所有的 f 和 m 值
-- 创建表
create table salary(
id int not null primary key,
name varchar(10) not null,
sex varchar(50) not null,
salary int not null
);
-- 插入数据
insert into salary values('1','A','m','2500');
insert into salary values('2','B','f','1500');
insert into salary values('3','C','m','5500');
insert into salary values('4','D','f','500');
-- 查看表
select * from salary;
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
+----+------+-----+--------+
-- 解答
mysql> use test2database
Database changed
mysql> update salary
-> set sex = CASE
-> when sex ='f' then 'm'
-> else 'f'
-> end
-> ;
Query OK, 4 rows affected (6.99 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from salary;
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
+----+------+-----+--------+
4 rows in set (0.10 sec)
5、项目五:有趣的电影 (难度:简单)
某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。
作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。
例如,下表 cinema:
±--------±----------±-------------±----------+
| id | movie | description | rating |
±--------±----------±-------------±----------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card| Interesting| 9.1 |
±--------±----------±-------------±----------+
对于上面的例子,则正确的输出是为:
±--------±----------±-------------±----------+
| id | movie | description | rating |
±--------±----------±-------------±----------+
| 5 | House card| Interesting| 9.1 |
| 1 | War | great 3D | 8.9 |
±--------±----------±-------------±----------+
解答
-- 项目五 找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,并按等级 rating 排列
-- 创建表
mysql> create table cinema(
-> id int not null primary key,
-> movie varchar(50) not null,
-> description varchar(50) not null,
-> rating float not null
-> );
Query OK, 0 rows affected (7.92 sec)
-- 插入数据
mysql> insert into cinema(id,movie,description,rating)
-> values
-> ('1','War','great 3D','8.9'),
-> ('2','Science','fiction','8.5'),
-> ('3','irish','boring','6.2'),
-> ('4','Ice song','Fantacy','8.6'),
-> ('5','House card','Interesting','9.1');
Query OK, 5 rows affected (0.28 sec)
Records: 5 Duplicates: 0 Warnings: 0
-- 查看表
mysql> select * from cinema;
+----+------------+-------------+--------+
| id | movie | description | rating |
+----+------------+-------------+--------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card | Interesting | 9.1 |
+----+------------+-------------+--------+
5 rows in set (0.00 sec)
-- 解答
mysql> select * from cinema
-> where description not in ('boring')
-> and id % 2 = 1
-> order by rating desc;
+----+------------+-------------+--------+
| id | movie | description | rating |
+----+------------+-------------+--------+
| 5 | House card | Interesting | 9.1 |
| 1 | War | great 3D | 8.9 |
+----+------------+-------------+--------+
2 rows in set (0.02 sec)
6、项目六:组合两张表 (难度:简单)
在数据库中创建表1和表2,并各插入三行数据(自己造)
表1: Person
±------------±--------+
| 列名 | 类型 |
±------------±--------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
±------------±--------+
PersonId 是上表主键
表2: Address
±------------±--------+
| 列名 | 类型 |
±------------±--------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
±------------±--------+
AddressId 是上表主键
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:FirstName, LastName, City, State
解答
-- 项目六 基于两表提供 person 的以下信息:FirstName, LastName, City, State
-- 创建 Person表
mysql> create table Person(
-> PersonId int not null primary key,
-> FirstName varchar(50) not null,
-> LastName varchar(50) not null
-> );
Query OK, 0 rows affected (1.20 sec)
-- 插入数据
mysql> insert into Person(PersonId,FirstName,LastName)
-> values
-> ('1','Sun','Xiao'),
-> ('2','Yao','Ming'),
-> ('3','Li','Weijia');
Query OK, 3 rows affected (0.23 sec)
Records: 3 Duplicates: 0 Warnings: 0
-- 查看表 Person
mysql> select * from Person;
+----------+-----------+----------+
| PersonId | FirstName | LastName |
+----------+-----------+----------+
| 1 | Sun | Xiao |
| 2 | Yao | Ming |
| 3 | Li | Weijia |
+----------+-----------+----------+
3 rows in set (0.00 sec)
-- 创建表 Address
mysql> create table Address(
-> AddressId int not null primary key,
-> PersonId int not null,
-> City varchar(50) not null,
-> State varchar(50) not null
-> );
Query OK, 0 rows affected (0.70 sec)
-- 插入数据
mysql> insert into Address(AddressId,PersonId,City,State)
-> values
-> ('1','3','Changsha','Wuhan'),
-> ('2','4','Shenzhen','Guangdong'),
-> ('3','2','Chengdu','Sichuan'),
-> ('4','1','Huangpu','Shanghai');
Query OK, 4 rows affected (0.21 sec)
Records: 4 Duplicates: 0 Warnings: 0
-- 查询表 Address
mysql> select * from Address;
+-----------+----------+----------+-----------+
| AddressId | PersonId | City | State |
+-----------+----------+----------+-----------+
| 1 | 3 | Changsha | Wuhan |
| 2 | 4 | Shenzhen | Guangdong |
| 3 | 2 | Chengdu | Sichuan |
| 4 | 1 | Huangpu | Shanghai |
+-----------+----------+----------+-----------+
4 rows in set (0.00 sec)
-- 解答
mysql> select P.FirstName,P.LastName, A.City, A.State
-> from Person as P left join Address as A
-> on P.PersonId = A.PersonId;
+-----------+----------+----------+----------+
| FirstName | LastName | City | State |
+-----------+----------+----------+----------+
| Li | Weijia | Changsha | Wuhan |
| Yao | Ming | Chengdu | Sichuan |
| Sun | Xiao | Huangpu | Shanghai |
+-----------+----------+----------+----------+
3 rows in set (0.00 sec)
7、项目七:删除重复的邮箱(难度:简单)
编写一个 SQL 查询,来删除 email 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。
±—±--------+
| Id | Email |
±—±--------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
±—±--------+
Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person表应返回以下几行:
±—±-----------------+
| Id | Email |
±—±-----------------+
| 1 | a@b.com |
| 2 | c@d.com |
±—±-----------------+
解答
-- 项目七:删除 email 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个
-- 查看表
mysql> select * from email;
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
3 rows in set (1.91 sec)
-- 解答1
mysql> delete a.* from email a, email b
-> where a.Email =b.Email
-> and a.Id >b.Id;
Query OK, 1 row affected (8.58 sec)
-- 解答2
mysql> delete a.* from email as a join email as b
-> on a.Email = b.Email
-> and a.Id >b.Id;
Query OK, 1 row affected (0.14 sec)
-- 结果
mysql> select * from email;
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
+----+---------+
2 rows in set (0.00 sec)
8、项目八:从不订购的客户 (难度:简单)
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
±—±------+
| Id | Name |
±—±------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
±—±------+
Orders 表:
±—±-----------+
| Id | CustomerId |
±—±-----------+
| 1 | 3 |
| 2 | 1 |
±—±-----------+
例如给定上述表格,你的查询应返回:
±----------+
| Customers |
±----------+
| Henry |
| Max |
±----------+
解答
-- 项目八:找出所有从不订购任何东西的客户
-- 创建表 Customers
mysql> create table Customers(
-> Id int not null primary key,
-> Name varchar(50) not null
-> );
Query OK, 0 rows affected (2.97 sec)
-- 插入数据
mysql> insert into Customers(Id,Name)
-> values
-> ('1','Joe'),
-> ('2','Henry'),
-> ('3','Sam'),
-> ('4','Max');
Query OK, 4 rows affected (0.35 sec)
Records: 4 Duplicates: 0 Warnings: 0
-- 查看表 Customers
mysql> select * from Customers;
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
4 rows in set (0.00 sec)
-- 创建表 Orders
mysql> create table Orders(
-> Id int not null primary key,
-> CustomerId int not null
-> );
Query OK, 0 rows affected (0.92 sec)
-- 插入数据
mysql> insert into Orders(Id,CustomerId)
-> values
-> ('1','3'),
-> ('2','1');
Query OK, 2 rows affected (0.25 sec)
Records: 2 Duplicates: 0 Warnings: 0
-- 查看表 Orders
mysql> select * from Orders;
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
2 rows in set (0.00 sec)
-- 解答1
mysql> select C.Name as Customers
-> from Customers as C left join Orders as O
-> on C.Id = O.CustomerId
-> where O.Id is null;
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
2 rows in set (0.00 sec)
-- 解答2
mysql> select Name as Customers
-> from Customers
-> where Id not in(
-> select CustomerId from Orders);
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
2 rows in set (0.06 sec)
9、项目九:超过经理收入的员工(难度:简单)
Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。
±—±------±-------±----------+
| Id | Name | Salary | ManagerId |
±—±------±-------±----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
±—±------±-------±----------+
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。
±---------+
| Employee |
±---------+
| Joe |
±---------+
解答
-- 项目九:给定 Employee 表,获取收入超过他们经理的员工的姓名
-- 创建表
mysql> create table Employee(
-> Id int not null primary key,
-> Name varchar(50) not null,
-> Salary int not null,
-> ManagerId varchar(20)
-> );
Query OK, 0 rows affected (0.69 sec)
-- 插入数据
mysql> insert into Employee(Id,Name,Salary,ManagerId)
-> values
-> ('1','Joe','70000','3'),
-> ('2','Henry','80000','4'),
-> ('3','Sam','60000','NULL'),
-> ('4','Max','90000','NULL');
Query OK, 4 rows affected (0.14 sec)
Records: 4 Duplicates: 0 Warnings: 0
-- 查看表
mysql> select * from Employee;
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
4 rows in set (0.06 sec)
-- 解答
mysql> select e.*, m.*
-> from Employee as e
-> join Employee as m
-> on e.ManagerId = m.Id;
+----+-------+--------+-----------+----+------+--------+-----------+
| Id | Name | Salary | ManagerId | Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+----+------+--------+-----------+
| 1 | Joe | 70000 | 3 | 3 | Sam | 60000 | NULL |
| 2 | Henry | 80000 | 4 | 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+----+------+--------+-----------+
2 rows in set (0.00 sec)
mysql> select e.Name as Employee
-> from Employee as e
-> join Employee as m
-> on e.ManagerId = m.Id
-> where e.Salary > m.Salary;
+----------+
| Employee |
+----------+
| Joe |
+----------+
1 row in set (0.00 sec)