最近上的数据库开发课程布置了一些sql题目,写到头秃……
select eno from works group by eno having sum(hours) > 1000
select eno from employees where not exists ( select * from relations where employees.eno = relations.eno )
select sname from sailors where not exists ( select * from boats where not exists ( select * from reserves where sailors.sid = reserves.sid and boats.bid = reserves.bid ) )
SELECT group_id, COUNT(group_id) AS match_num FROM matches GROUP BY (group_id)
SELECT match_id, abs(first_score - second_score) as sub FROM matches ORDER BY abs(first_score - second_score) DESC LIMIT 1
select order_date, count(order_id) as num from orders group by order_date order by count(*) desc limit 1
select users.user_id from users, orders, items where users.user_id = orders.buyer_id and orders.item_id = items.item_id and users.favorite_brand = items.item_brand
select round( ifnull( ( select count(*) from ( select distinct requester_id, accepter_id from accepted_requests ) as b ) /( select count(*) from ( select distinct sender_id, send_to_id from friend_requests ) as a ), 0 ), 2 ) as accept_rate
select distinct sender_id as user_id from friend_requests where sender_id not in( select requester_id from accepted_requests )
select distinct author_id id from views where author_id = viewer_id order by id
select distinct viewer_id from views group by view_date, viewer_id having count(distinct article_id) >= 2 ORDER BY viewer_id
select viewer_id, count(article_id) as article_num from views group by viewer_id order by count(*) desc limit 1
select user_id, login_date as date from logins a where not exists( select 1 from logins where user_id = a.user_id and login_date > a.login_date ) order by user_id asc
select departments.department_name as department, avg(employees.salary) as avg_salary from departments, employees where employees.department_id = departments.department_id and department_name = "Technology"
select department_name as department,(max(salary) - min(salary)) as sub from employees, departments where departments.department_id = employees.department_id group by employees.department_id
select title from movie_rating r left join movies m on r.movie_id = m.movie_id where date_format(created_at, '%Y-%m') = '2020-02' group by r.movie_id order by avg(rating) desc, title limit 1
select movies.movie_id, movies.title, avg(movie_rating.rating) as avg_rating, max(movie_rating.rating) as max_rating, min(movie_rating.rating) as min_rating from movie_rating, movies where movie_rating.movie_id = movies.movie_id group by movie_rating.movie_id
select users.user_id, users.name, movies.title, movie_rating.rating from users, movies, movie_rating where users.user_id = movie_rating.user_id and users.user_id = 1 and movies.movie_id = movie_rating.movie_id
select i1.invoice_id, i1.price, i1.user_id, customers.customer_name from customers, invoices as i1,( select max(price) as maxprice from invoices ) as i2 where i1.price = i2.maxprice and i1.user_id = customers.customer_id
select customers.customer_id, customers.customer_name, contacts.contact_name from customers, contacts where customers.customer_id = user_id