IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 力扣关于数据库的题目(12道) -> 正文阅读

[大数据]力扣关于数据库的题目(12道)

#连接后这两个就是一个表了
select firstName, lastName, city, state
from Person left join Address
on Person.PersonId = Address.PersonId;

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

?

?

?

?

#5.5
select  ifnull((select distinct salary 
        from Employee
        order by salary desc
        limit 1 offset 1), null) as SecondHighestSalary;

?

?

#编写一个函数


CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      # Write your MySQL query statement below.
        select distinct salary 
        #临时表必须命名,窗口函数的使用dense_rank()
        from (select salary, dense_rank() over (order by salary desc) as a from employee) as temp
        where a = N       
  );
END

?

# Write your MySQL query statement below
#命名的时候使用反引号,它是为了区分MYSQL的保留字(就和java中的关键字一样)与普通字符而引入的符号
#加上partition by是指定分组,比如各种学科的时候,这个不需要指定分组啊
select score, dense_rank() over (order by score desc) as `rank`
from Scores;

?

# Write your MySQL query statement below
#在业务层进行筛选
#非要用数据库的话就查3个表
select *
from Logs l1, Logs l2, Logs l3
where l1.Id = l2.Id - 1
    and l2.Id = l3.Id - 1
    and l1.Num = l2.Num
    and l2.Num = l3.Num;

?

# Write your MySQL query statement below
# SELECT
#      a.NAME AS Employee
# FROM Employee AS a JOIN Employee AS b
#      ON a.ManagerId = b.Id
#      AND a.Salary > b.Salary
# ;
#一个表中的查询,换个名字又是一样的新表,用自连接
#左连接返回左边全部,已经两者都有的部分
select a.name as `Employee`
from Employee as a join Employee as b on a.managerId = b.id and a.salary > b.salary;

# Write your MySQL query statement below
#先查找电子邮箱出现的次数, 在查询大于一的个数,这是临时表的方式
select Email
from (select Email, count(Email) as `num`
    from Person
    group by Email) as temp
where num > 1;

#优先顺序: where>group by>having>order by
select Email
from Person
group by Email
having count(Email) > 1;

?

?

# Write your MySQL query statement below
#连接查询五种情况的第一种
select c.Name as Customers 
from Customers as c left join Orders as o on c.Id = o.CustomerId
where o.Id is null

#子查询的方式
select customers.name as 'Customers'
from customers
where customers.id not in
(
    select customerid from orders
);

?

?

# Write your MySQL query statement below

#部门工资最高的员工,不同分组中最高或着最低的问题
#真要是写起来细节很多,要记住前面带上哪个表在.
select temp.Department, temp.Employee, temp.Salary
from (select d.name as `Department`, e.name as `Employee`, e.salary as `Salary`,
            #得按照原两个表中字段值多的一列写,e.department改为d.Department是错误的
            dense_rank() over (partition by e.departmentId order by e.Salary desc) as `dense` 
        from Employee as e join Department as d on e.departmentId = d.id ) as temp
where temp.dense = 1;


?

# Write your MySQL query statement below
select temp.Department, temp.Employee, temp.Salary
from (select d.name as `Department`, e.name as `Employee`, e.salary as `Salary`,
            #得按照原两个表中字段值多的一列写,e.department改为d.Department是错误的
            dense_rank() over (partition by e.departmentId order by e.Salary desc) as `dense` 
        from Employee as e join Department as d on e.departmentId = d.id ) as temp
where temp.dense < 4;

?

# Write your MySQL query statement below
#某种意义上,p2作为临时表了,然后p1中的数据一条一条去对比
delete p1 from Person p1, Person p2 where p1.Email = p2.Email and p1.Id > p2.Id;

?

# Write your MySQL query statement below
# SELECT
#     weather.id AS 'Id'
# FROM
#     weather
#         JOIN
#     weather w ON DATEDIFF(weather.date, w.date) = 1
#         AND weather.Temperature > w.Temperature
# ;

# SELECT DATEDIFF('2008-12-30','2008-12-29') AS DiffDate 结果为1; SELECT DATEDIFF('2008-12-29','2008-12-30') AS DiffDate 结果为-1。
# MySQL 使用 DATEDIFF 来比较两个日期类型的值。
select w.id
from weather as w join weather as wt on 
        datediff(w.recordDate, wt.recordDate) = 1 and w.Temperature > wt.Temperature;

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章           查看所有文章
加:2022-05-08 08:10:56  更:2022-05-08 08:15:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 22:08:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码