MySQL 练习<2>

Posted Al_tair

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 练习<2>相关的知识,希望对你有一定的参考价值。

mysql 练习

MySQL 练习

大家好呀,我是小笙,今天我来分享一些 Leetcode 上的MySQL的练习

1667. 修复表中的名字

编写一个 SQL 查询来修复名字,使得只有第一个字符是大写的,其余都是小写的返回按 user_id 排序的结果表

# 创建表
Create table If Not Exists Users (
    user_id int, 
    name varchar(40)
)

# 插入数据
insert into Users (user_id, name) values ('1', 'aLice')
insert into Users (user_id, name) values ('2', 'bOB')

示例:

输入:
Users table:
+---------+-------+
| user_id | name  |
+---------+-------+
| 1       | aLice |
| 2       | bOB   |
+---------+-------+
输出:
+---------+-------+
| user_id | name  |
+---------+-------+
| 1       | Alice |
| 2       | Bob   |
+---------+-------+

代码实现

select 
	user_id,concat(ucase(left(name,1)),lcase(substring(name,2,length(name)-1))) as name
from 
	Users
order by 
	user_id

183.从不订购的客户

编写一个 SQL 查询,找出所有从不订购任何东西的客户

# 新建表 Customers
Create table If Not Exists Customers 
(
    id int, 
    name varchar(255)
)
# 新建表 Orders
Create table If Not Exists Orders 
(
    id int, 
    customerId int
)

# 插入数据 Customers
insert into Customers (id, name) values ('1', 'Joe')
insert into Customers (id, name) values ('2', 'Henry')
insert into Customers (id, name) values ('3', 'Sam')
insert into Customers (id, name) values ('4', 'Max')
# 插入数据 Orders
insert into Orders (id, customerId) values ('1', '3')
insert into Orders (id, customerId) values ('2', '1')

某网站包含两个表,Customers 表和 Orders

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

代码实现

select Name as Customers from Customers
        where Customers.Id not in (select CustomerId from Orders);
        
-- 左外连接
select Name as Customers from Customers
        left join Orders on Customers.Id = Orders.CustomerId
        where Orders.CustomerId is null

175. 组合两个表

编写一个SQL查询来报告 Person 表中每个人的姓、名、城市和州。如果 personId 的地址不在 Address 表中,则报告为空 null

# 新建表 Person
Create table If Not Exists Person 
(
    personId int, 
    firstName varchar(255), 
    lastName varchar(255)
)
# 新建表 Address
Create table If Not Exists Address 
(
    addressId int, 
    personId int, 
    city varchar(255), 
    state varchar(255)
)

# 添加数据 Person
insert into Person (personId, lastName, firstName) values ('1', 'Wang', 'Allen')
insert into Person (personId, lastName, firstName) values ('2', 'Alice', 'Bob')

# 添加数据 Address
insert into Address (addressId, personId, city, state) values ('1', '2', 'New York City', 'New York')
insert into Address (addressId, personId, city, state) values ('2', '3', 'Leetcode', 'California')

示例

输入: 
Person:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1        | Wang     | Allen     |
| 2        | Alice    | Bob       |
+----------+----------+-----------+
Address:
+-----------+----------+---------------+------------+
| addressId | personId | city          | state      |
+-----------+----------+---------------+------------+
| 1         | 2        | New York City | New York   |
| 2         | 3        | Leetcode      | California |
+-----------+----------+---------------+------------+
输出: 
+-----------+----------+---------------+----------+
| firstName | lastName | city          | state    |
+-----------+----------+---------------+----------+
| Allen     | Wang     | Null          | Null     |
| Bob       | Alice    | New York City | New York |
+-----------+----------+---------------+----------+
解释: 
地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。
addressId = 1 包含了 personId = 2 的地址信息。

代码实现

select firstName,lastName,city,state from Person 
left join Address 
on Person.PersonId = Address.PersonId;

总结

  • A inner join B:取交集
  • A left join B:取A全部,B没有对应的值,则为null
  • A right join B:取B全部,A没有对应的值,则为null
  • A full outer join B:取并集,彼此没有对应的值为null

607. 销售员

编写一个SQL查询,报告没有任何与名为 “RED” 的公司相关的订单的所有销售人员的姓名

Create table If Not Exists SalesPerson (
    sales_id int, 
    name varchar(255), 
    salary int, 
    commission_rate int, 
    hire_date date
)

Create table If Not Exists Company (
    com_id int, 
    name varchar(255), 
    city varchar(255)
)

Create table If Not Exists Orders (
    order_id int, 
    order_date date, 
    com_id int, 
    sales_id int, 
    amount int
)

Truncate table SalesPerson
insert into SalesPerson (sales_id, name, salary, commission_rate, hire_date) values ('1', 'John', '100000', '6', '4/1/2006')
insert into SalesPerson (sales_id, name, salary, commission_rate, hire_date) values ('2', 'Amy', '12000', '5', '5/1/2010')
insert into SalesPerson (sales_id, name, salary, commission_rate, hire_date) values ('3', 'Mark', '65000', '12', '12/25/2008')
insert into SalesPerson (sales_id, name, salary, commission_rate, hire_date) values ('4', 'Pam', '25000', '25', '1/1/2005')
insert into SalesPerson (sales_id, name, salary, commission_rate, hire_date) values ('5', 'Alex', '5000', '10', '2/3/2007')
Truncate table Company
insert into Company (com_id, name, city) values ('1', 'RED', 'Boston')
insert into Company (com_id, name, city) values ('2', 'ORANGE', 'New York')
insert into Company (com_id, name, city) values ('3', 'YELLOW', 'Boston')
insert into Company (com_id, name, city) values ('4', 'GREEN', 'Austin')
Truncate table Orders
insert into Orders (order_id, order_date, com_id, sales_id, amount) values ('1', '1/1/2014', '3', '4', '10000')
insert into Orders (order_id, order_date, com_id, sales_id, amount) values ('2', '2/1/2014', '4', '5', '5000')
insert into Orders (order_id, order_date, com_id, sales_id, amount) values ('3', '3/1/2014', '1', '1', '50000')
insert into Orders (order_id, order_date, com_id, sales_id, amount) values ('4', '4/1/2014', '1', '4', '25000')

代码实现

# 将 Company 和 Orders 进行左连接查出对应的 Company.name = 'RED' 的 sales_id    
select s.name from SalesPerson s 
    where s.sales_id not in 
    	(select Orders.sales_id from Orders
        	left join Company on Orders.com_id = Company.com_id
        	where Company.name = 'RED');

181. 超过经理收入的员工

编写一个SQL查询来查找收入比经理高的员工

# 新建表
Create table If Not Exists Employee (
    id int, 
    name varchar(255), 
    salary int, 
    managerId int
)
    
# 插入数据
insert into Employee (id, name, salary, managerId) values ('1', 'Joe', '70000', '3')
insert into Employee (id, name, salary, managerId) values ('2', 'Henry', '80000', '4')
insert into Employee (id, name, salary, managerId) values ('3', 'Sam', '60000', 'None')
insert into Employee (id, name, salary, managerId) values ('4', 'Max', '90000', 'None')

内连接

select worker.name as 'Employee' from Employee worker,Employee boss
    where worker.managerId =  boss.id and boss.salary < worker.salary;

使用 JOIN 语句

SELECT
     a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
     ON a.ManagerId = b.Id
     AND a.Salary > b.Salary

以上是关于MySQL 练习<2>的主要内容,如果未能解决你的问题,请参考以下文章

MySQL 练习<1>

MySQL 练习<1>

MySQL 练习<3>

MySQL 练习<3>

MySQL 练习<4>

MySQL 练习<4>