赞
踩
https://leetcode.cn/problems/combine-two-tables/description/
select p.firstName,p.lastName,a.city,a.state
from Person p
left join Address a on a.personId=p.personId
https://leetcode.cn/problems/employees-earning-more-than-their-managers/
select e1.name as 'Employee'
from Employee e1,Employee e2
where e1.managerId=e2.id
and e1.salary>e2.salary
https://leetcode.cn/problems/duplicate-emails/
select Email
FROM Person
group by Email having count(Email)>1;
https://leetcode.cn/problems/customers-who-never-order/
select name as Customers
from Customers c
where c.id not in
(
select customerId from Orders
)
https://leetcode.cn/problems/rising-temperature/description/
select b.id as 'id'
from Weather a,Weather b
where a.recorddate=subdate(b.recorddate,1) and b.temperature>a.temperature
2023.11.28
https://leetcode.cn/problems/employee-bonus/description/
select
e1.name,b1.bonus
from
Employee e1
left join
Bonus b1
on e1.empId=b1.empId
where b1.bonus<=500 or b1.bonus is null
https://leetcode.cn/problems/find-customer-referee/description/
select name from Customer where referee_id<>2 or referee_id is null
2023.11.29
https://leetcode.cn/problems/customer-placing-the-largest-number-of-orders/description/
SELECT
customer_number
FROM
orders
GROUP BY customer_number
ORDER BY COUNT(*) DESC
LIMIT 1
;
select customer_number
from Orders
group by customer_number
having count(customer_number)=
(
SELECT
COUNT(customer_number) AS 'cnt'
FROM
Orders
GROUP BY customer_number
ORDER BY cnt DESC
LIMIT 1
)
https://leetcode.cn/problems/big-countries/description/
select name,population,area
from World
where area>=3000000 or population>=25000000
2023.11.30
https://leetcode.cn/problems/classes-more-than-5-students/
select class
from Courses
group by class
having count(Student)>=5 #计数某一列要比计数*时间快
https://leetcode.cn/problems/sales-person/description/
select sp.name
from SalesPerson as sp
where sp.sales_id NOT IN
(
select o.sales_id
from Orders o
left join
Company c
ON
o.com_id=c.com_id
where
c.name='RED'
)
2023.12.4
https://leetcode.cn/problems/triangle-judgement/description/
select x,y,z,
case
when x+y>z and x+z>y and y+z>x then 'Yes'
else 'No'
end as 'triangle'
from
triangle
;
https://leetcode.cn/problems/biggest-single-number/description/
select Max(num) as num from MyNumbers
where num not in
(
select num from MyNumbers group by num having count(num) > 1
)
2023/12/5
https://leetcode.cn/problems/not-boring-movies/
select
*
from cinema
where description !='boring' and id%2=1
order by rating desc
https://leetcode.cn/problems/swap-salary/description/
update salary
set
sex=
case sex
when 'm' then 'f'
else 'm'
END;
2023.12.7
https://leetcode.cn/problems/actors-and-directors-who-cooperated-at-least-three-times/description/
select actor_id,director_id
from ActorDirector
group by actor_id,director_id
having count(timestamp)>=3
https://leetcode.cn/problems/product-sales-analysis-i/description/
select p.product_name as product_name,s.year as year,s.price as price
from Sales s
left join
Product p
on
s.product_id=p.product_id
https://leetcode.cn/problems/project-employees-i/description/
select p.project_id,round(AVG(e.experience_years),2) as average_years
from Project p
left join
Employee e
on
p.employee_id=e.employee_id
group by p.project_id
2023.12.8
https://leetcode.cn/problems/sales-analysis-iii/description/
select s.product_id,p.product_name
from Product p
left join Sales s
on p.product_id=s.product_id
group by s.product_id
having count(s.sale_date between '2019-01-01' and '2019-03-31' or null)=count(*)
https://leetcode.cn/problems/user-activity-for-the-past-30-days-i/solutions/2329645/xin-shou-jie-ti-fen-xi-ti-mu-yi-bu-yi-bu-0iaj/
分析:
count(distinct ##)
代码:
select activity_date as day,count(distinct user_id) as active_users
from Activity
where activity_date BETWEEN DATE_ADD('2019-07-27',INTERVAL -29 day) and '2019-07-27'
group by activity_date
SELECT activity_date AS day, COUNT(DISTINCT user_id) AS active_users
FROM Activity
WHERE DATEDIFF(CAST("2019-07-27" AS DATE), activity_date) BETWEEN 0 AND 29
GROUP BY activity_date
https://leetcode.cn/problems/article-views-i/description/
select
distinct author_id as id
from
Views
where author_id=viewer_id
order by author_id
https://leetcode.cn/problems/reformat-department-table/description/
存在的问题:
select id as id, SUM(CASE WHEN month='Jan' THEN revenue END) AS Jan_Revenue, SUM(CASE WHEN month='Feb' THEN revenue END) AS Feb_Revenue, SUM(CASE WHEN month='Mar' THEN revenue END) AS Mar_Revenue, SUM(CASE WHEN month='Apr' THEN revenue END) AS Apr_Revenue, SUM(CASE WHEN month='May' THEN revenue END) AS May_Revenue, SUM(CASE WHEN month='Jun' THEN revenue END) AS Jun_Revenue, SUM(CASE WHEN month='Jul' THEN revenue END) AS Jul_Revenue, SUM(CASE WHEN month='Aug' THEN revenue END) AS Aug_Revenue, SUM(CASE WHEN month='Sep' THEN revenue END) AS Sep_Revenue, SUM(CASE WHEN month='Oct' THEN revenue END) AS Oct_Revenue, SUM(CASE WHEN month='Nov' THEN revenue END) AS Nov_Revenue, SUM(CASE WHEN month='Dec' THEN revenue END) AS Dec_Revenue from Department GROUP BY id ORDER BY id
2024年1月2日
https://leetcode.cn/problems/queries-quality-and-percentage/
select
query_name,
round(avg(rating/position),2) as quality,
round(sum(if(rating<3,1,0))*100/count(*),2) as poor_query_percentage
from
Queries
group by query_name having query_name is not null
https://leetcode.cn/problems/average-selling-price/description/
2024.1.3
SELECT s.student_id, s.student_name, su.subject_name, COUNT(e.subject_name) AS attended_exams FROM Students AS s JOIN Subjects AS su LEFT JOIN Examinations AS e ON e.student_id = s.student_id AND e.subject_name = su.subject_name GROUP BY s.student_id, su.subject_name ORDER BY s.student_id, su.subject_name
having 对应的就是select里面筛选出来的字段
select
p.product_name, sum(unit) unit
from
Products p
left join
Orders o
on
p.product_id=o.product_id
where o.order_date like '2020-02%'
group by p.product_name
having sum(unit)>=100
2024.3.25
https://leetcode.cn/problems/replace-employee-id-with-the-unique-identifier/description/
select b.unique_id,a.name
from Employees a
left join EmployeeUNI b
on a.id=b.id
https://leetcode.cn/problems/top-travellers/description/
select a.name,if (b.travelled_distance is null,0,b.travelled_distance) as travelled_distance
from Users a
left join
(
select distinct r.user_id,sum(r.distance) as travelled_distance
from Rides r
group by r.user_id
) b
on a.id=b.user_id
order by b.travelled_distance desc,a.name asc
2024.03.27
select
sell_date,
count(distinct product) as num_sold,
group_concat(
distinct product
order by product
separator ','
) products
from
Activities
group by sell_date
order by sell_date
符号记得要将符号进行转义
select
*
from
Users
where mail REGEXP '^[a-zA-Z][a-zA-Z0-9\_\.\-]*@leetcode\\.com$'
2024.4.15
https://leetcode.cn/problems/patients-with-a-condition/
select patient_id,patient_name,conditions
from Patients
where conditions like 'DIAB1%' or conditions like '% DIAB1%'
https://leetcode.cn/problems/customer-who-visited-but-did-not-make-any-transactions/
select customer_id,count(customer_id) as count_no_trans
from Visits v
left join Transactions t
on v.visit_id=t.visit_id
where transaction_id IS NULL
group by v.customer_id
https://leetcode.cn/problems/bank-account-summary-ii/
select u.name,sum(t.amount) as balance
from Users u
left join Transactions t
on u.account=t.account
group by u.name
having sum(t.amount)>10000
2024.5.30
https://leetcode.cn/problems/percentage-of-users-attended-a-contest/description/
select
contest_id,
round((count(distinct user_id)*100/(select count(user_id) from Users)),2) as percentage
from Register
group by contest_id
order by percentage desc,contest_id
2024.6.3
https://leetcode.cn/problems/average-time-of-process-per-machine/description/
select machine_id,
round(sum(if(activity_type='end',timestamp,-timestamp))/count(distinct process_id),3) as processing_time
from Activity
group by machine_id
select
a1.machine_id,
round(avg(a2.timestamp -a1.timestamp ),3) as processing_time
from Activity as a1 join Activity as a2 on
a1.machine_id=a2.machine_id and
a1.process_id=a2.process_id and
a1.activity_type ='start' and
a2.activity_type ='end'
group by machine_id;
https://leetcode.cn/problems/fix-names-in-a-table/description/
select user_id,concat(upper(substring(name,1,1)),lower(substring(name,2))) as name
from Users
order by user_id
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。