sql
刷题
The Great Ant
做一个功成名就的人
展开
-
2021-06-25
#获取最近第二次的活动写一条SQL查询展示每一位用户 最近第二次 的活动,如果用户仅有一次活动,返回该活动。一个用户不能同时进行超过一项活动,以 任意 顺序返回结果。展示效果:+------------+--------------+-------------+-------------+| username | activity | startDate | endDate |+------------+--------------+-------------+------原创 2021-06-25 23:15:38 · 62 阅读 · 0 评论 -
2021-06-17
电影评分请你编写一组 SQL 查询:查找评论电影数量最多的用户名,如果出现平局,返回字典序较小的用户名。查找在2020 年 2 月 平均评分最高的电影名称,如果出现平局,返回字典序较小的电影名称。展示效果:+--------------+| results |+--------------+| Daniel || Frozen 2 |+--------------+建表语句:Create table If Not Exists 77_Movies (mov原创 2021-06-17 21:48:16 · 102 阅读 · 0 评论 -
2021-06-09
列出指定时间段内所有的下单产品写一个 SQL 语句,要求获取在 2020 年 2 月份下单的数量不少于 100 的产品的名字和数目。返回结果表单的顺序无要求。展示效果:+--------------------+---------+| product_name | unit |+--------------------+---------+| Leetcode Solutions | 130 || Leetcode Kit | 100 |+---原创 2021-06-09 22:02:17 · 72 阅读 · 0 评论 -
2021-06-08
餐馆营业额变化增长写一条 SQL 查询计算以 7 天(某日期 + 该日期前的 6 天)为一个时间段的顾客消费平均值查询结果格式的例子如下:查询结果按 visited_on 排序average_amount 要 保留两位小数,日期数据的格式为 (‘YYYY-MM-DD’)展示效果:+--------------+--------------+----------------+| visited_on | amount | average_amount |+---------原创 2021-06-08 22:07:09 · 52 阅读 · 0 评论 -
2021-06-07
释放缓存和缓存缓存:(1)dataFrame.cache(2)sparkSession.catalog.cacheTable(“tableName”)释放缓存:(1)dataFrame.unpersist (2)sparkSession.catalog.uncacheTable(“tableName”)原创 2021-06-07 22:23:17 · 33 阅读 · 0 评论 -
2021-06-07
cache缓存级别DataFrame的cache默认采用 MEMORY_AND_DISKRDD 的cache默认方式采用MEMORY_ONLY原创 2021-06-07 22:22:41 · 38 阅读 · 0 评论 -
2021-06-06
#不同性别每日分数总计写一条SQL语句查询每种性别在每一天的总分,并按性别和日期对查询结果排序展示效果:+--------+------------+-------+| gender | day | total |+--------+------------+-------+| F | 2019-12-30 | 17 || F | 2019-12-31 | 40 || F | 2020-01-01 | 57 || F原创 2021-06-07 22:22:03 · 36 阅读 · 0 评论 -
2021-06-04
求团队人数编写一个 SQL 查询,以求得每个员工所在团队的总人数。查询结果中的顺序无特定要求。展示效果:+-------------+------------+| employee_id | team_size |+-------------+------------+| 1 | 3 || 2 | 3 || 3 | 3 || 4 | 1原创 2021-06-04 22:06:43 · 99 阅读 · 0 评论 -
2021-06-03
不同国家的天气类型需求:写一段 SQL 来找到表中每个国家在 2019 年 11 月的天气类型。天气类型的定义如下:当 weather_state 的平均值小于或等于15返回 Cold,当 weather_state 的平均值大于或等于 25 返回 Hot,否则返回 Warm。展示效果:+--------------+--------------+| country_name | weather_type |+--------------+--------------+| USA原创 2021-06-03 22:39:06 · 41 阅读 · 0 评论 -
2021-06-02
找到连续区间的开始和结束数字需求:编写一个 SQL 查询得到 Logs 表中的连续区间的开始数字和结束数字。展示效果:+------------+--------------+| start_id | end_id |+------------+--------------+| 1 | 3 || 7 | 8 || 10 | 10 |+------------+原创 2021-06-02 22:13:46 · 70 阅读 · 0 评论 -
2021-06-01
学生们参加各科测试的次数需求:写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序。展示效果:+------------+--------------+--------------+----------------+| student_id | student_name | subject_name | attended_exams |+------------+--------------+--------------+-原创 2021-06-01 22:10:35 · 235 阅读 · 0 评论 -
2021-05-28
Team Scores in Football Tournament需求一:写出一条SQL语句以查询每个队的 team_id,team_name 和 num_points。结果根据 num_points 降序排序,如果有两队积分相同,那么这两队按 team_id 升序排序。展示效果:team_idteam_namenum_points10Leetcode FC720NewYork FC350Toronto FC330Atlanta FC14原创 2021-05-28 21:59:55 · 91 阅读 · 0 评论 -
2021-05-26
Queries Quality and Percentage需求:编写一组 SQL 来查找每次查询的名称(query_name)、质量(quality) 和 劣质查询百分比(poor_query_percentage)。质量(quality) 和劣质查询百分比(poor_query_percentage) 都应四舍五入到小数点后两位。展示效果:query_namequalitypoor_query_percentageDog2.5033.33Cat0.6633.3原创 2021-05-26 22:07:15 · 30 阅读 · 0 评论 -
2021-05-25
Last Person to Fit in the Elevator需求:查找最后一个能进入电梯且不超过重量限制的 person_name 。题目确保队列中第一位的人可以进入电梯 。展示效果:person_nameThomas JeffersonCreate table If Not Exists 60_Queue (person_id int, person_name varchar(30), weight int, turn int);Truncate table 60_原创 2021-05-25 22:04:39 · 36 阅读 · 0 评论 -
2021-05-21
每月交易需求一:查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。展示效果:monthcountrytrans_countapproved_counttrans_total_amountapproved_total_amount2018-12US21300010002019-01US11200020002019-01DE11200020002019-05US21300010002019-0原创 2021-05-21 22:11:00 · 55 阅读 · 0 评论 -
2021-05-20
重新格式化部门表需求一:编写一个 SQL 查询来重新格式化表,使得新的表中有一个部门 id 列和一些对应 每个月 的收入(revenue)列。展示效果:idJan_RevenueFeb_RevenueMar_Revenue…Dec_Revenue1800070006000…null29000nullnull…null3null10000null…nullCreate table If Not Exists 57_Departm原创 2021-05-20 22:13:01 · 38 阅读 · 0 评论 -
2021-05-19
Immediate Food Delivery需求一:查询语句获取即时订单所占的百分比, 保留两位小数。展示效果:immediate_percentage42.86Create table If Not Exists 56_Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date);Truncate table 56_Delivery;inser原创 2021-05-19 22:08:13 · 38 阅读 · 0 评论 -
2021-05-18
文章浏览需求一:找出所有浏览过自己文章的作者,结果按照 id 升序排列。展示效果:id47Create table If Not Exists 53_Views (article_id int, author_id int, viewer_id int, view_date date);Truncate table 53_Views;insert into 53_Views (article_id, author_id, viewer_id, view_date) v原创 2021-05-18 22:07:02 · 37 阅读 · 0 评论 -
2021-05-17
每位学生的最高成绩需求一:查询每位学生获得的最高成绩和它所对应的科目,若科目成绩并列,取 course_id 最小的一门。查询结果需按 student_id 增序进行排序。展示效果:student_idaverage_yearsgrade129922953382Create table If Not Exists 48_Enrollments (student_id int, course_id int, grade int);Truncate t原创 2021-05-17 22:06:04 · 47 阅读 · 0 评论 -
2021-05-14
每日新用户统计需求一:编写一个 SQL 查询,以查询从今天起最多 90 天内,每个日期该日期首次登录的用户数。假设今天是 2019-06-30.展示效果:login_dateuser_count2019-05-0112019-06-212Create table If Not Exists 47_Traffic (user_id int, activity ENUM('login', 'logout', 'jobs', 'groups', 'homepage'),原创 2021-05-14 22:04:22 · 71 阅读 · 0 评论 -
2021-05-13
小众书籍需求:筛选出订单总量少于10本的书籍 。展示效果:book_idname1Kalila And Demna228 Letters5The Hunger GamesCreate table If Not Exists 46_Books (book_id int, name varchar(50), available_from date);Create table If Not Exists 46_Orders (order_id int, book原创 2021-05-13 22:03:55 · 47 阅读 · 0 评论 -
2021-05-12
销售分析Create table If Not Exists 45_Product (product_id int, product_name varchar(10), unit_price int);Create table If Not Exists 45_Sales (seller_id int, product_id int,buyer_id int, sale_date date, quantity int, price int);Truncate table 45_Product;ins原创 2021-05-12 22:23:10 · 43 阅读 · 1 评论 -
2021-05-10
销售分析需求一:编写一个 SQL 查询,查询总销售额最高的销售者,如果有并列的,就都展示出来。展示效果:seller_id13Create table If Not Exists 45_Product (product_id int, product_name varchar(10), unit_price int);Create table If Not Exists 45_Sales (seller_id int, product_id int,buyer_id i原创 2021-05-10 22:50:11 · 157 阅读 · 0 评论 -
2021-05-08
院系无效的学生写一条 SQL 语句以查询那些所在院系不存在的学生的 id 和姓名,可以以任何顺序返回结果展示效果:+------+----------+| id | name |+------+----------+| 2 | John || 7 | Daiana || 4 | Jasmine || 3 | Steve |+------+----------+建表语句:Create table If Not Exists 78原创 2021-05-08 22:45:44 · 94 阅读 · 0 评论 -
2021-05-07
项目员工需求一:查询每一个项目中员工的平均工作年限,精确到小数点后两位。展示效果:project_idaverage_years12.0022.50Create table If Not Exists 44_Project (project_id int, employee_id int);Create table If Not Exists 44_Employee (employee_id int, name varchar(10), experience_ye原创 2021-05-07 23:00:31 · 86 阅读 · 0 评论 -
2021-05-06
产品销售分析需求一:获取产品表 Product 中所有的 产品名称 product name 以及 该产品在 Sales 表中相对应的 上市年份 year 和 价格 price。展示效果:product_nameyearpriceNokia20085000Nokia20095000Apple20119000Create table 43_Sales (sale_id int, product_id int, year int, quantity in原创 2021-05-06 22:04:19 · 87 阅读 · 0 评论 -
2021-05-05
合作过至少三次的演员和导演需求:编写一个 SQL 查询,查询语句获取合作过至少三次的演员和导演的 id 对 (actor_id, director_id)展示效果:actor_iddirector_id11Create table If Not Exists 42_ActorDirector (actor_id int, director_id int, timestamp int);Truncate table 42_ActorDirector;insert into原创 2021-05-05 20:41:35 · 75 阅读 · 0 评论 -
2021-05-04
买下所有产品的用户需求:编写一个 SQL 查询,从 Customer 表中查询购买了 Product 表中所有产品的客户的 id。展示效果:customer_id13Create table If Not Exists 41_Customer (customer_id int, product_key int);Create table 41_Product (product_key int);Truncate table 41_Customer;insert in原创 2021-05-04 22:40:25 · 29 阅读 · 0 评论 -
2021-05-03
交换性别需求:给定一个 salary 表,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。展示效果:idnamesexsalary1Af25002Bm15003Cf55004Dm500create table if not exists 40_salary(id int, name varchar(100原创 2021-05-03 22:52:12 · 58 阅读 · 0 评论 -
2021-05-02
换座位需求:编写一个 SQL 查询,小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。展示效果:idstudent1Doris2Abbot3Green4Emerson5Jeames现在小美想改变相邻俩学生的座位(若学生人数为奇数,则无需改变最后一位同学的座位),现在需要你编写SQL输出小美想要的的结果。Create table If Not Exists 39_seat(id int,原创 2021-05-02 22:40:51 · 50 阅读 · 0 评论 -
2021-05-01
有趣的电影需求:编写一个 SQL 查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列展示效果:idmoviedescriptionrating5House cardInteresting9.11Wargreat 3D8.9Create table If Not Exists 38_cinema (id int, movie varchar(255), description varchar原创 2021-05-01 23:49:08 · 36 阅读 · 0 评论 -
2021-04-30
只出现一次的最大数字需求:编写一个 SQL 查询,找到只出现过一次的数字中,最大的一个数字。如果没有只出现一次的数字,输出 null 。展示效果:num6Create table If Not Exists 37_my_numbers (num int);Truncate table 37_my_numbers;insert into 37_my_numbers (num) values (8);insert into 37_my_numbers (num) values原创 2021-04-30 23:31:01 · 29 阅读 · 1 评论 -
2021-04-29
节点树需求:写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。表 tree,id 是树节点的编号, p_id 是它父节点的 id 。树中每个节点属于以下三种类型之一: 叶子:如果这个节点没有任何孩子节点。 根:如果这个节点是整棵树的根,即没有父节点。 内部节点:如果这个节点既不是叶子节点也不是根节点。idp_id1null21314252展示效果:idType1Root原创 2021-04-29 22:12:32 · 71 阅读 · 0 评论 -
2021-04-26
直线上最近距离需求:找到这些点中最近两个点之间的距离。展示效果:shortest1CREATE TABLE If Not Exists 33_point (x INT NOT NULL, UNIQUE INDEX x_UNIQUE (x ASC));Truncate table 33_point;insert into 33_point (x) values (-1);insert into 33_point (x) values (0);insert into 33_原创 2021-04-26 22:27:52 · 31 阅读 · 0 评论 -
2021-04-25
判断是否是三角形需求:编写一个 SQL 查询,判断三条线段是否能形成一个三角形。展示效果:xyztriangle131515No102015YesCreate table If Not Exists 31_triangle (x int, y int, z int);Truncate table 31_triangle;insert into 31_triangle (x, y, z) values (13, 15, 30);insert into原创 2021-04-25 22:52:29 · 44 阅读 · 0 评论 -
2021-04-23
连续空余座位需求:编写一个 SQL 查询,获取所有空余座位,并将它们按照 seat_id 排序展示效果:seat_id345Create table If Not Exists 28_cinema (seat_id int primary key auto_increment, free bool);Truncate table 28_cinema;insert into 28_cinema (seat_id, free) values (1, 1);inse原创 2021-04-23 22:17:55 · 33 阅读 · 0 评论 -
2021-04-22
27. 体育馆人流量需求:请编写一个查询语句,找出人流量的高峰期。高峰期定义,至少连续三行记录中的人流量不少于100。展示效果:idvisit_datepeople52017-01-0514562017-01-06145572017-01-0719982017-01-08188Create table If Not Exists 27_stadium (id int, visit_date DATE NULL, people int);T原创 2021-04-22 19:10:42 · 93 阅读 · 0 评论 -
2021-04-21
好友申请需求一:写一个查询语句,求出好友申请的通过率,用 2 位小数表示。通过率由接受好友申请的数目除以申请总数。展示效果:accept_rate0.80Create table If Not Exists 26_friend_request ( sender_id INT NOT NULL, send_to_id INT NULL, request_date DATE NULL);Create table If Not Exists 26_request_accepted原创 2021-04-21 22:53:18 · 66 阅读 · 0 评论 -
2021-04-17
寻找用户推荐人需求:写一个查询语句,返回一个编号列表,列表中编号的推荐人的编号都 不是 2展示效果:nameWillJaneBillZackCREATE TABLE IF NOT EXISTS 21_customer (id INT,name VARCHAR(25),referee_id INT);Truncate table 21_customer;insert into 21_customer (id, name, referee_id) values原创 2021-04-17 22:02:27 · 29 阅读 · 0 评论 -
2021-04-16
统计各专业人数需求:查询 department 表中每个专业的学生人数 (即使没有学生的专业也需列出)。展示效果:dept_namestudent_numberEngineering2Science1Law0CREATE TABLE IF NOT EXISTS 20_student (student_id INT,student_name VARCHAR(45), gender VARCHAR(6), dept_id INT);CREATE TABLE I原创 2021-04-16 22:12:14 · 128 阅读 · 0 评论