跳至主要內容

SQL

大约 11 分钟

SQL

1 SQL 入门

  • 简单 29
  • 中等 4
  • 困难 0
题目难度代码
第 1 天 选择
595. 大的国家open in new window简单mysqlopen in new window
1757. 可回收且低脂的产品open in new window简单mysqlopen in new window
584. 寻找用户推荐人open in new window简单mysqlopen in new window
183. 从不订购的客户open in new window简单mysqlopen in new window
第 2 天 排序 & 修改
1873. 计算特殊奖金open in new window简单mysqlopen in new window
627. 变更性别open in new window简单mysqlopen in new window
196. 删除重复的电子邮箱open in new window简单mysqlopen in new window
第 3 天 字符串处理函数/正则
1667. 修复表中的名字open in new window简单mysqlopen in new window
1484. 按日期分组销售产品open in new window简单mysqlopen in new window
1527. 患某种疾病的患者open in new window简单mysqlopen in new window
第 4 天 组合查询 & 指定选取
1965. 丢失信息的雇员open in new window简单mysqlopen in new window
1795. 每个产品在不同商店的价格open in new window简单mysqlopen in new window
608. 树节点open in new window中等mysqlopen in new window
176. 第二高的薪水open in new window中等mysqlopen in new window
第 5 天 合并
175. 组合两个表open in new window简单mysqlopen in new window
1581. 进店却未进行过交易的顾客open in new window简单mysqlopen in new window
1148. 文章浏览 Iopen in new window简单mysqlopen in new window
第 6 天 合并
197. 上升的温度open in new window简单mysqlopen in new window
607. 销售员open in new window简单mysqlopen in new window
第 7 天 计算函数
1141. 查询近 30 天活跃用户数open in new window简单mysqlopen in new window
1693. 每天的领导和合伙人open in new window简单mysqlopen in new window
1729. 求关注者的数量open in new window简单mysqlopen in new window
第 8 天 计算函数
586. 订单最多的客户open in new window简单mysqlopen in new window
511. 游戏玩法分析 Iopen in new window简单mysqlopen in new window
1890. 2020 年最后一次登录open in new window简单mysqlopen in new window
1741. 查找每个员工花费的总时间open in new window简单mysqlopen in new window
第 9 天 控制流
1393. 股票的资本损益open in new window中等mysqlopen in new window
1407. 排名靠前的旅行者open in new window简单mysqlopen in new window
1158. 市场分析 Iopen in new window中等mysqlopen in new window
第 10 天 过滤
182. 查找重复的电子邮箱open in new window简单mysqlopen in new window
1050. 合作过至少三次的演员和导演open in new window简单mysqlopen in new window
1587. 银行账户概要 IIopen in new window简单mysqlopen in new window
1084. 销售分析 IIIopen in new window简单mysqlopen in new window

 

2 $SQL 基础

  • 简单 13
  • 中等 24
  • 困难 1
题目难度代码
第 1 天 数值处理函数
$1699. 两人之间的通话次数open in new window 中等mysqlopen in new window
$1251. 平均售价open in new window 简单mysqlopen in new window
$1571. 仓库经理open in new window 简单mysqlopen in new window
$1445. 苹果和桔子open in new window 中等mysqlopen in new window
第 2 天 数值处理函数
$1193. 每月交易 Iopen in new window 中等mysqlopen in new window
$1633. 各赛事的用户注册率open in new window 简单mysqlopen in new window
$1173. 即时食物配送 Iopen in new window 简单mysqlopen in new window
$1211. 查询结果的质量和占比open in new window 简单mysqlopen in new window
第 3 天 连接
$1607. 没有卖出的卖家open in new window 简单mysqlopen in new window
$619. 只出现一次的最大数字open in new window 简单mysqlopen in new window
$1112. 每位学生的最高成绩open in new window 中等mysqlopen in new window
$1398. 购买了产品 A 和产品 B 却没有购买产品 C 的顾客open in new window中等mysqlopen in new window
第 4 天 连接
$1440. 计算布尔表达式的值open in new window 中等mysqlopen in new window
$1264. 页面推荐open in new window 中等mysqlopen in new window
$570. 至少有 5 名直接下属的经理open in new window 中等mysqlopen in new window
$1303. 求团队人数open in new window 简单mysqlopen in new window
第 5 天 连接
$1280. 学生们参加各科测试的次数open in new window 简单mysqlopen in new window
$1501. 可以放心投资的国家open in new window 中等mysqlopen in new window
184. 部门工资最高的员工open in new window中等mysqlopen in new window
$580. 统计各专业学生人数open in new window 中等mysqlopen in new window
第 6 天 连接
$1294. 不同国家的天气类型open in new window 简单mysqlopen in new window
626. 换座位open in new window中等mysqlopen in new window
$1783. 大满贯数量open in new window 中等mysqlopen in new window
$1164. 指定日期的产品价格open in new window 中等mysqlopen in new window
第 7 天 不等式连接
$603. 连续空余座位open in new window 简单mysqlopen in new window
$1731. 每位经理的下属员工数量open in new window 简单mysqlopen in new window
$1747. 应该被禁止的 Leetflex 账户open in new window 中等mysqlopen in new window
181. 超过经理收入的员工open in new window简单mysqlopen in new window
第 8 天 不等式连接
$1459. 矩形面积open in new window 中等mysqlopen in new window
180. 连续出现的数字open in new window中等mysqlopen in new window
$1988. 找出每所学校的最低分数要求open in new window 中等mysqlopen in new window
第 9 天 子查询
$1549. 每件商品的最新订单open in new window 中等mysqlopen in new window
$1321. 餐馆营业额变化增长open in new window 中等mysqlopen in new window
$1045. 买下所有产品的客户open in new window 中等mysqlopen in new window
第 10 天 子查询
$1341. 电影评分open in new window 中等mysqlopen in new window
$1867. 最大数量高于平均水平的订单open in new window 中等mysqlopen in new window
第 11 天 子查询
$550. 游戏玩法分析 IVopen in new window 中等mysqlopen in new window
262. 行程和用户open in new window困难mysqlopen in new window

 

3 $SQL 进阶

  • 简单 1
  • 中等 17
  • 困难 11
题目难度代码
第 1 天 汇总函数
$1303. 求团队人数open in new window 简单mysqlopen in new window
$1308. 不同性别每日分数总计open in new window 中等mysqlopen in new window
$1501. 可以放心投资的国家open in new window 中等mysqlopen in new window
第 2 天 排名函数
$1077. 项目员工 IIIopen in new window 中等mysqlopen in new window
$1549. 每件商品的最新订单open in new window 中等mysqlopen in new window
$1285. 找到连续区间的开始和结束数字open in new window 中等mysqlopen in new window
$1596. 每位顾客最经常订购的商品open in new window 中等mysqlopen in new window
第 3 天 排名函数
178. 分数排名open in new window中等mysqlopen in new window
177. 第 N 高的薪水open in new window中等mysqlopen in new window
$1951. 查询具有最多共同关注者的所有两两结对组open in new window 中等mysqlopen in new window
$1709. 访问日期之间最大的空档期open in new window 中等mysqlopen in new window
第 4 天 函数
$1949. 坚定的友谊open in new window 中等mysqlopen in new window
$1532. 最近的三笔订单open in new window 中等mysqlopen in new window
$1126. 查询活跃业务open in new window 中等mysqlopen in new window
$1831. 每天的最大交易open in new window 中等mysqlopen in new window
第 5 天 递归、依赖、嵌套
$1613. 找到遗失的 IDopen in new window 中等mysqlopen in new window
$1270. 向公司 CEO 汇报工作的所有人open in new window 中等mysqlopen in new window
$1369. 获取最近第二次的活动open in new window 困难mysqlopen in new window
第 6 天 递归、依赖、嵌套
$1412. 查找成绩处于中游的学生open in new window 困难mysqlopen in new window
$1972. 同一天的第一个电话和最后一个电话open in new window 困难mysqlopen in new window
第 7 天 递归、依赖、嵌套
185. 部门工资前三高的所有员工open in new window困难mysqlopen in new window
$1767. 寻找没有被执行的任务对open in new window 困难mysqlopen in new window
第 8 天 递归、依赖、嵌套
$1384. 按年度列出销售总额open in new window 困难mysqlopen in new window
$569. 员工薪水中位数open in new window 困难mysqlopen in new window
第 9 天 递归、依赖、嵌套
$571. 给定数字的频率查询中位数open in new window 困难mysqlopen in new window
$1225. 报告系统状态的连续日期open in new window 困难mysqlopen in new window
第 10 天 递归、依赖、嵌套
$1454. 活跃用户open in new window 中等mysqlopen in new window
$618. 学生地理信息报告open in new window 困难mysqlopen in new window
$2010. 职员招聘人数 IIopen in new window 困难mysqlopen in new window

 

4 小结

12.5 Flow Control Functionsopen in new window

CASE value WHEN compare_value THEN result [WHEN compare_value THEN result ...] [ELSE result] END
CASE WHEN condition THEN result [WHEN condition THEN result ...] [ELSE result] END
IF(expr1,expr2,expr3): If expr1 is TRUE (expr1 <> 0 and expr1 IS NOT NULL), IF() returns expr2. Otherwise, it returns expr3.
IFNULL(expr1,expr2): If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

12.7 Date and Time Functionsopen in new window

DATE_FORMAT(date,format)

Formats the date value according to the format string.

The specifiers shown in the following table may be used in the format string. The % character is required before format specifier characters. The specifiers apply to other functions as well: STR_TO_DATE(), TIME_FORMAT(), UNIX_TIMESTAMP().

DATEDIFF(expr1,expr2)

DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

13.2.10.3 UNION Clauseopen in new window

UNION DISTINCT and UNION ALL

By default, duplicate rows are removed from UNION results. The optional DISTINCT keyword has the same effect but makes it explicit. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.

You can mix UNION ALL and UNION DISTINCT in the same query. Mixed UNION types are treated such that a DISTINCT union overrides any ALL union to its left. A DISTINCT union can be produced explicitly by using UNION DISTINCT or implicitly by using UNION with no following DISTINCT or ALL keyword.

In MySQL 8.0.19 and later, UNION ALL and UNION DISTINCT work the same way when one or more TABLE statements are used in the union.

12.21.2 Window Function Concepts and Syntaxopen in new window

over_clause:
    {OVER (window_spec) | OVER window_name}

12.4.2 Comparison Functions and Operatorsopen in new window

COALESCE(value,...)

Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

The return type of COALESCE() is the aggregated type of the argument types.

12.21.1 Window Function Descriptionsopen in new window

DENSE_RANK() over_clause

Returns the rank of the current row within its partition, without gaps. Peers are considered ties and receive the same rank. This function assigns consecutive ranks to peer groups; the result is that groups of size greater than one do not produce noncontiguous rank numbers. For an example, see the RANK() function description.

This function should be used with ORDER BY to sort partition rows into the desired order. Without ORDER BY, all rows are peers.

RANK() over_clause

Returns the rank of the current row within its partition, with gaps. Peers are considered ties and receive the same rank. This function does not assign consecutive ranks to peer groups if groups of size greater than one exist; the result is noncontiguous rank numbers.

This function should be used with ORDER BY to sort partition rows into the desired order. Without ORDER BY, all rows are peers.

mysqltest Input Conventionsopen in new window

(全文完)