
数据库刷题
每天都要被自己菜醒
这个作者很懒,什么都没留下…
展开
-
1485 按照日期分组销售产品 group_concat() 列抓行(没写出来)
Create table If Not Exists Activities (sell_date date, product varchar(20))Truncate table Activitiesinsert into Activities (sell_date, product) values ('2020-05-30', 'Headphone')insert into Activities (sell_date, product) values ('2020-06-01', 'Pencil')原创 2020-09-15 12:44:42 · 161 阅读 · 0 评论 -
1479 周内每天的销售情况 (列转行 )首先得到一个标签,任何group by sum(if(标签))
Create table If Not Exists Orders (order_id int, customer_id int, order_date date, item_id varchar(30), quantity int)Create table If Not Exists Items (item_id varchar(30), item_name varchar(30), item_category varchar(30))Truncate table Ordersinsert into原创 2020-09-15 11:17:43 · 159 阅读 · 0 评论 -
1468 计算税后工资:利用max () over 和case when 组合使用打上标签
Create table If Not Exists Salaries (company_id int, employee_id int, employee_name varchar(13), salary int)Truncate table Salariesinsert into Salaries (company_id, employee_id, employee_name, salary) values ('1', '1', 'Tony', '2000')insert into Salarie原创 2020-09-15 10:36:30 · 384 阅读 · 0 评论 -
1459 矩形面积(单个表经常要两个from 加上筛选条件)
Create table If Not Exists Points (id int, x_value int, y_value int)Truncate table Pointsinsert into Points (id, x_value, y_value) values ('1', '2', '8')insert into Points (id, x_value, y_value) values ('2', '4', '7')insert into Points (id, x_value, y_原创 2020-09-15 10:17:07 · 148 阅读 · 0 评论 -
1454 活跃用户连续5天连续登录(没写出来) 日期减去排序号归为一类
Create table If Not Exists Accounts (id int, name varchar(10))Create table If Not Exists Logins (id int, login_date date)Truncate table Accountsinsert into Accounts (id, name) values ('1', 'Winston')insert into Accounts (id, name) values ('7', 'Jonatha原创 2020-09-14 09:55:28 · 386 阅读 · 0 评论 -
1445 苹果和橘子 和1393 一模一样的题目 我用了别的方法没想到
Create table If Not Exists Sales (sale_date date, fruit ENUM('apples', 'oranges'), sold_num int)Truncate table Salesinsert into Sales (sale_date, fruit, sold_num) values ('2020-05-01', 'apples', '10')insert into Sales (sale_date, fruit, sold_num) values原创 2020-09-13 21:56:56 · 163 阅读 · 0 评论 -
1440 计算布尔表达式的值 :笛卡尔积 后在于表left join
Create Table If Not Exists Variables (name varchar(3), value int)Create Table If Not Exists Expressions (left_operand varchar(3), operator ENUM('>', '<', '='), right_operand varchar(3))Truncate table Variablesinsert into Variables (name, value) va原创 2020-09-13 10:43:37 · 133 阅读 · 0 评论 -
1435 制作会话柱状图 (没有完全写出):连用select ‘[0-5>‘ as bin 利用union all 取得4个标签,这样不会漏掉
Create table If Not Exists Sessions (session_id int, duration int)Truncate table Sessionsinsert into Sessions (session_id, duration) values ('1', '30')insert into Sessions (session_id, duration) values ('2', '199')insert into Sessions (session_id, dura原创 2020-09-13 09:58:20 · 160 阅读 · 0 评论 -
1412 查找成绩处于中游的学生 (不完全是求中位数 不能完全那个逻辑)
Create table If Not Exists Student (student_id int, student_name varchar(30))Create table If Not Exists Exam (exam_id int, student_id int, score int)Truncate table Studentinsert into Student (student_id, student_name) values ('1', 'Daniel')insert into原创 2020-09-11 17:12:46 · 189 阅读 · 0 评论 -
1398. 购买了产品A和产品B却没有购买产品C的顾客 子查询+外加case when 给标签
Create table If Not Exists Customers (customer_id int, customer_name varchar(30))Create table If Not Exists Orders (order_id int, customer_id int, product_name varchar(30))Truncate table Customersinsert into Customers (customer_id, customer_name) values原创 2020-09-11 15:49:49 · 184 阅读 · 0 评论 -
1393 股票的资本损益 思路:将股票的买入时设为负数
Create Table If Not Exists Stocks (stock_name varchar(15), operation ENUM('Sell', 'Buy'), operation_day int, price int)Truncate table Stocksinsert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Buy', '1', '1000')insert in原创 2020-09-11 15:14:30 · 153 阅读 · 0 评论 -
1369 获取最近第二次的活动 cont() over () 和 row_number() over()一起用
Create table If Not Exists UserActivity (username varchar(30), activity varchar(30), startDate date, endDate date)Truncate table UserActivityinsert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Travel', '2020-02-12', '2020-原创 2020-09-10 15:28:22 · 173 阅读 · 0 评论 -
1364 顾客的可信联系人数量 完成表前半部分,在完成表的后半部分
Create table If Not Exists Customers (customer_id int, customer_name varchar(20), email varchar(30))Create table If Not Exists Contacts (user_id int, contact_name varchar(20), contact_email varchar(30))Create table If Not Exists Invoices (invoice_id int,原创 2020-09-10 14:58:20 · 158 阅读 · 0 评论 -
1355 活动参与者 体会到昨天的 where a in (max (a))不能这么写
Create table If Not Exists Friends (id int, name varchar(30), activity varchar(30))Create table If Not Exists Activities (id int, name varchar(30))Truncate table Friendsinsert into Friends (id, name, activity) values ('1', 'Jonathan D.', 'Eating')inser原创 2020-09-10 13:53:16 · 137 阅读 · 0 评论 -
1350 院系无效的学生 left join处理购买不支付问题
Create table If Not Exists Departments (id int, name varchar(30))Create table If Not Exists Students (id int, name varchar(30), department_id int)Truncate table Departmentsinsert into Departments (id, name) values ('1', 'Electrical Engineering')insert原创 2020-09-10 00:02:29 · 181 阅读 · 0 评论 -
1341 电影评分 注意union 的括号问题
Create table If Not Exists Movies (movie_id int, title varchar(30))Create table If Not Exists Users (user_id int, name varchar(30))Create table If Not Exists Movie_Rating (movie_id int, user_id int, rating int, created_at date)Truncate table Moviesinse原创 2020-09-09 19:02:55 · 425 阅读 · 0 评论 -
1327. 列出指定时间段内所有的下单产品
Create table If Not Exists Products (product_id int, product_name varchar(40), product_category varchar(40))Create table If Not Exists Orders (product_id int, order_date date, unit int)Truncate table Productsinsert into Products (product_id, product_nam原创 2020-09-09 18:37:06 · 212 阅读 · 0 评论 -
1322 广告效果 sum (if ()) group by 进行判断
Create table If Not Exists Ads (ad_id int, user_id int, action ENUM('Clicked', 'Viewed', 'Ignored'))Truncate table Adsinsert into Ads (ad_id, user_id, action) values ('1', '1', 'Clicked')insert into Ads (ad_id, user_id, action) values ('2', '2', 'Clicke原创 2020-09-09 18:20:52 · 216 阅读 · 0 评论 -
1285 找到连续区间的开始和结束数字没有完成(中等)601 题联动利用id -减去行号 如果是连续的数字
Create table If Not Exists Logs (log_id int)Truncate table Logsinsert into Logs (log_id) values ('1')insert into Logs (log_id) values ('2')insert into Logs (log_id) values ('3')insert into Logs (log_id) values ('7')insert into Logs (log_id) values ('原创 2020-09-09 17:59:33 · 177 阅读 · 0 评论 -
1231餐馆营业额变换增长(中等)sum() over(order by visited_on rows 6 preceding)
Create table If Not Exists Customer (customer_id int, name varchar(20), visited_on date, amount int)Truncate table Customerinsert into Customer (customer_id, name, visited_on, amount) values ('1', 'Jhon', '2019-01-01', '100')insert into Customer (custom原创 2020-09-09 17:24:21 · 1019 阅读 · 0 评论 -
1308 不同性别的每日分数统计 sum()over()
Create table If Not Exists Scores (player_name varchar(20), gender varchar(1), day date, score_points int)Truncate table Scoresinsert into Scores (player_name, gender, day, score_points) values ('Aron', 'F', '2020-01-01', '17')insert into Scores (player原创 2020-09-09 16:45:08 · 208 阅读 · 0 评论 -
1303 求团队人数(简单) count() over()
Create table If Not Exists Employee (employee_id int, team_id int)Truncate table Employeeinsert into Employee (employee_id, team_id) values ('1', '8')insert into Employee (employee_id, team_id) values ('2', '8')insert into Employee (employee_id, team_i原创 2020-09-09 16:29:54 · 439 阅读 · 0 评论 -
1294 不同国家的天气类型(简单)
SQL 架构:Create table If Not Exists Countries (country_id int, country_name varchar(20))Create table If Not Exists Weather (country_id int, weather_state int, day date)Truncate table Countriesinsert into Countries (country_id, country_name) values ('2',原创 2020-09-09 16:21:01 · 254 阅读 · 0 评论 -
1281 学生们参加各科测试的次数
Create table If Not Exists Students (student_id int, student_name varchar(20))Create table If Not Exists Subjects (subject_name varchar(20))Create table If Not Exists Examinations (student_id int, subject_name varchar(20))Truncate table Studentsinsert原创 2020-09-09 15:58:27 · 160 阅读 · 0 评论 -
1270 向公司ceo汇报工作的所有人(中等 ) 三表联立
Create table If Not Exists Employees (employee_id int, employee_name varchar(30), manager_id int)Truncate table Employeesinsert into Employees (employee_id, employee_name, manager_id) values ('1', 'Boss', '1')insert into Employees (employee_id, employee原创 2020-09-04 23:53:45 · 836 阅读 · 0 评论 -
1264页面推荐(中等 union all )稍微的难点在于找1的朋友
Create table If Not Exists Friendship (user1_id int, user2_id int)Create table If Not Exists Likes (user_id int, page_id int)Truncate table Friendshipinsert into Friendship (user1_id, user2_id) values ('1', '2')insert into Friendship (user1_id, user2_i原创 2020-09-04 23:40:22 · 142 阅读 · 0 评论 -
1251 平均售价(简单)
Create table If Not Exists Prices (product_id int, start_date date, end_date date, price int)Create table If Not Exists UnitsSold (product_id int, purchase_date date, units int)Truncate table Pricesinsert into Prices (product_id, start_date, end_date, p原创 2020-09-04 23:00:57 · 189 阅读 · 0 评论 -
1241 每个帖子的评论数(自己写出)
Create table If Not Exists Submissions (sub_id int, parent_id int)Truncate table Submissionsinsert into Submissions (sub_id, parent_id) values ('1', 'None')insert into Submissions (sub_id, parent_id) values ('2', 'None')insert into Submissions (sub_id,原创 2020-09-04 22:32:29 · 192 阅读 · 0 评论 -
1212 查询球队积分(中等自己写出)用到了union all
Create table If Not Exists Teams (team_id int, team_name varchar(30))Create table If Not Exists Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int)Truncate table Teamsinsert into Teams (team_id, team_name) values ('10'原创 2020-09-04 22:14:07 · 160 阅读 · 0 评论 -
1211. 查询结果的质量和占比(自己写出 简单题)
sql 架构Create table If Not Exists Queries (query_name varchar(30), result varchar(50), position int, rating int)Truncate table Queriesinsert into Queries (query_name, result, position, rating) values ('Dog', 'Golden Retriever', '1', '5')insert into Quer原创 2020-09-02 16:17:32 · 218 阅读 · 0 评论 -
1205 每月交易II leetcode数据库刷题 (没写出来)
1205 每月交易IIsql 架构;create table if not exists Transactions (id int, country varchar(4), state enum('approved', 'declined'), amount int, trans_date date)create table if not exists Chargebacks (trans_id int, trans_date date)Truncate table Transactionsins原创 2020-09-02 16:02:03 · 188 阅读 · 0 评论 -
1204 最后一个进入电梯的人 leetcode 数据库刷题(自己写出)
1204sql 架构:Create table If Not Exists Queue (person_id int, person_name varchar(30), weight int, turn int)Truncate table Queueinsert into Queue (person_id, person_name, weight, turn) values ('5', 'George Washington', '250', '1')insert into Queue (pers原创 2020-09-02 12:17:52 · 202 阅读 · 0 评论 -
1193 每月的交易 leetcode 数据库刷题(自己写出)
1193 每月的交易SQL 架构:create table if not exists Transactions (id int, country varchar(4), state enum('approved', 'declined'), amount int, trans_date date)Truncate table Transactionsinsert into Transactions (id, country, state, amount, trans_date) values ('原创 2020-09-02 12:06:38 · 167 阅读 · 0 评论 -
1179 重新格式化部门表(列转行问题)(自己没写出来)
重新格式化部门表SQL 架构Create table If Not Exists Department (id int, revenue int, month varchar(5))Truncate table Departmentinsert into Department (id, revenue, month) values ('1', '8000', 'Jan')insert into Department (id, revenue, month) values ('2', '9000.原创 2020-09-02 11:46:42 · 128 阅读 · 0 评论 -
1174. leetcode 刷题即时食物配送 II
# Write your MySQL query statement below #错误写法,有点懵select round(sum(if(min_date=customer_pref_delivery_date,1,0)) /count(*)*100 ,2)as 'immediate_percentage'from (select distinct delivery_id,customer_id ,min(order_date) as 'min_date',customer_pref_del原创 2020-08-31 03:14:28 · 364 阅读 · 0 评论 -
1173 即时食物配送 I(简单题)
select round(sum(if(order_date=customer_pref_delivery_date,1,0)) /count(*) *100,2)as 'immediate_percentage'from Delivery原创 2020-08-31 02:22:06 · 221 阅读 · 0 评论 -
1164. 指定日期的产品价格(中等题)
select product_id,if(new_price>0,new_price,10) as ' price'FROM(select distinct product_id,10 as 'price'from Products) uleft join (SELECT product_id as 'nid',new_price FROM (select product_id, new_price,change_date, #对月份逆向排序 选择最大的.原创 2020-08-31 02:12:22 · 286 阅读 · 0 评论 -
leetcode 数据库刷题 1159. 市场分析 II
# Write your MySQL query statement belowSELECT distinct user_id as 'seller_id',case when favorite_brand = item_brand then 'yes' else 'no'end as '2nd_item_fav_brand'FROM #用户卖出列表 (select user_id,favorite_brand #,b.order_date,b.seller_id,原创 2020-08-31 01:17:23 · 246 阅读 · 0 评论