select
b.dno,
a.eno,
b.max_salary as salary
from
employees as a,(
select
dno,
max(salary) as max_salary
from
employees
group by
dno
) as b
where
a.dno = b.dno
and a.salary = b.max_salary
order by
b.dno asc
select
s.sname
from
sailors s
where
s.age > 35
and not exists(
select
*
from
reserves r,
boats b
where
r.sid = s.sid
and r.bid = b.bid
and b.color = "RED"
and r.reserve_date between '2020-09-01'
and '2020-09-30'
)
select sname from(select
s.sname,
s.rating
from
sailors s,
reserves r,
boats b
where
r.sid = s.sid
and r.bid = b.bid
and b.color = "GREEN"
and r.reserve_date between '2020-05-01'
and '2020-05-31'
order by
s.rating desc) as t1
limit
1
select
sname
from(
select
distinct s.sname
from
sailors s,
reserves r,
boats b
where
r.sid = s.sid
and r.bid = b.bid
and b.color = "GREEN"
and r.reserve_date between '2020-08-01'
and '2020-08-31'
and s.age > 35
) as t1
where
exists(
select
distinct s.sname
from
sailors s,
reserves r,
boats b
where
r.sid = s.sid
and r.bid = b.bid
and b.color = "RED"
and r.reserve_date between '2020-08-01'
and '2020-08-31'
and s.age > 35
)
select
customer_id,
count(order_id) as order_num
from
orders
where
order_date between "2020-08-01"
and "2020-08-31"
group by
customer_id
order by
count(*) desc
limit
1
select
name customer_name,
order_id,
order_date
from
(
select
o1.customer_id,
o1.order_id,
o1.order_date,
(
select
1 + count(*)
from
orders o7
where
o7.customer_id = o1.customer_id
and o7.order_date > o1.order_date
) as rnk
from
orders o1
) t
left join customers using(customer_id)
where
rnk <= 3
order by
name asc,
order_date desc
select
player_id,
count(match_id) as match_num
from(
select
first_player as player_id,
match_id
from
matches
union all
select
second_player as player_id,
match_id
from
matches
) as ps
group by
player_id
order by
match_num desc
limit
1
select
group_id,
player_id
from(
select
p.group_id,
p.player_id,
sum(ps.score) as score
from
players as p
inner join(
select
first_player as player_id,
first_score as score
from
matches
union all
select
second_player as player_id,
second_score as score
from
matches
) as ps on p.player_id = ps.player_id
group by
player_id
order by
group_id,
score desc,
player_id
) as top_scores
group by
group_id
select
u.user_id,
join_date,
count(o.order_id) as orders_in_2019
from
users u
left join orders o on u.user_id = o.buyer_id
and year(o.order_date) = '2019'
group by
u.user_id
select
users.user_id as seller_id,
if(
O3.item_id is not null
and users.favorite_brand = items.item_brand,
'yes',
'no'
) as 'if_fav_brand'
from
users
left join (
select
seller_id,(
select
item_id
from
orders O2
where
O2.seller_id = O1.seller_id
order by
order_date
limit
1, 1
) as item_id
from
orders O1
group by
seller_id
) O3 on users.user_id = O3.seller_id
left join items on O3.item_id = items.item_id
select
a_m1.mon,
a_m2.count2 / a_m1.count1 as accept_rate
from
(
select
mon,
count(*) as count1
from(
select
distinct sender_id,
send_to_id,
month(request_date) as mon
from
friend_requests
) as t1
group by
mon
) as a_m1,(
select
mon,
count(*) as count2
from(
select
distinct requester_id,
accepter_id,
month(accept_date) as mon
from
accepted_requests
) as t1
group by
mon
) as a_m2
where
a_m1.mon = a_m2.mon
and a_m1.count1 / a_m2.count2 != 0
group by
a_m1.mon asc
select
user_id,
count(*) friend_num
from
(
select
requester_id user_id
from
accepted_requests
union all
select
accepter_id user_id
from
accepted_requests
) as t1
group by
user_id
order by
friend_num desc
limit
1;
select
round(
count(distinct user_id) * 1.0 /(
select
count(distinct user_id)
from
logins
),
3
) as rate
from
logins
where
(user_id, login_date) in (
select
user_id,
DATE_ADD(min(login_date), INTERVAL 1 DAY)
from
logins
group by
user_id
);
select
t1.login_date,
count(t2.user_id) as new_user_num
from
(
select
distinct login_date
from
logins
) t1
left join (
select
user_id,
min(login_date) first_date
from
logins
group by
user_id
) t2 on t1.login_date = t2.first_date
group by
t1.login_date
having
new_user_num >= 2
order by
t1.login_date asc
select
t1.login_date as date,
round(
count(distinct logins.user_id) / count(t1.user_id),
3
) as rate
from
(
select
user_id,
min(login_date) as login_date
from
logins
group by
user_id
) as t1
left join logins on logins.user_id = t1.user_id
and logins.login_date = DATE_ADD(t1.login_date, INTERVAL 1 DAY)
group by
t1.login_date
union
select
login_date as date,
0.000 as rate
from
logins
where
login_date not in(
select
min(login_date)
from
logins
group by
user_id
)
order by
date
select
d.department_name as department,
e.name,
e.salary
from
employees e,
departments d
where
e.department_id = d.department_id
and salary = IFNULL(
(
select
distinct e1.salary
from
employees as e1
where
e.department_id = e1.department_id
order by
e1.salary desc
limit
1, 1
), 0
)
order by
department
select
d.department_name as department,
e1.name as name,
e1.salary
from
employees e1
join departments d on e1.department_id = d.department_id
where
3 > (
select
count(distinct e2.salary)
from
employees e2
where
e2.salary > e1.salary
and e1.department_id = e2.department_id
)
order by
department asc,
e1.salary desc
select
contacts.user_id,
contacts.contact_name,
contacts.contact_email
from
contacts,(
select
user_id
from
invoices
group by
user_id
having
count(invoice_id) > 1
) as t1
where
contacts.user_id = t1.user_id
select
invoices.invoice_id,
t1.customer_name,
invoices.price,
count(contacts.user_id) as contacts_cnt,
count(t2.email) as trusted_contacts_cnt
from
invoices
join customers t1 on invoices.user_id = t1.customer_id
left join contacts on t1.customer_id = contacts.user_id
left join customers t2 on contacts.contact_email = t2.email
group by
invoices.invoice_id
select t1.c_id,courses.c_name,t1.max_score,t1.min_score,t1.不及格率 as '不及格率',t1.中等率 as '中等率',t1.优秀率 as '优秀率' from courses,(select c_id,max(s_score) as max_score,min(s_score) as min_score,round((sum(case when s_score < 60 then 1 else 0 end)/ count(*)),2) as 不及格率,round((sum(case when s_score >= 60 and s_score <90 then 1 else 0 end)/ count(*)),2) as 中等率,round((sum(case when s_score >= 90 then 1 else 0 end)/ count(*)),2) as 优秀率 from scores group by c_id) as t1 where t1.c_id=courses.c_id