SQL查询练习二(From LeetCode)
请选用MySQL进行测试.
1.将男性和女性的工资互换(E)
思路:使用case when进行条件判断,在使用update进行修改
update salary
set sex =
case sex
when 'm' then 'f'
else 'm'
end
2.找出description不是boring且id是奇数的电影(E)
思路:使用where字句进行筛选,并且使用mod进行奇数偶数的判定
select id,movie,description,rating
from cinema
where description != 'boring'
and mod(id,2) = 1
order by rating desc
3.找出不销售Red厂家的推销员(E)
思路:使用子查询先找出销售RED厂家的销售员id,在将外层查询进行not in
select name
from salesperson
where sales_id not in(
select sales_id
from orders o join company c
on o.com_id = c.com_id
and c.name = 'RED'
)
4.找出数据库中相同的行(E)
思路:使用子查询对内层查询按照Email进行分组,统计出大于1的就是重复的值
select Email from
(
select Email,count(*) as num from Person group by Email
) as sub
where num>1
5.删除相同的数据(E)
思路:找到两张表相同的Email但是不同的id,把这行数据进行删除
delete p1 from Person p1,Person p2
where p1.Email = p2.Email
and p1.id > p2.id
6.找出经度和纬度不同的2016年的投资金额总和
思路:找出2015年投资相同的记录数,再将经度,维度作为分组的条件进行连接,最后查出结果
select sum(insurance.TIV_2016) as tiv_2016
from insurance
where insurance.TIV_2015 in
(
select TIV_2015
from insurance
group by TIV_2015
having count(*) > 1
)
and concat(lat,lon) in
(
select concat(lat,lon)
from insurance
group by lat,lon
having count(*) =1
)
7.找出部门平均工资和公司平均工资的高低
思路:计算公司每月的平均薪水,计算部门每月的平均薪水,然后进行比较
select department_sal.pay_month,department_id,
case
when department_avg > company_avg then 'higher'
when department_avg < company_avg then 'lower'
else 'same'
end as comparison
from
(
select department_id,avg(amount) as department_avg,date_format(pay_date,'%Y-%m') as pay_month
from salary join employee
on salary.employee_id = employee.employee_id
group by department_id,pay_month
) as department_sal
join
(
select avg(amount) as company_avg,date_format(pay_date,'%Y-%m') as pay_month
from salary
group by pay_month
) as company_sal
on department_sal.pay_month = company_sal.pay_month
8.找出谁是facebook上最受欢迎的人
思路:根据request_id和accepter_id,可知3收到两个accept,发送一个request,所以3才是社交最活跃的,采用union all将requester_id,sender_id集合起来,分组才能找到使用最频繁的用户
select ids as id,cnt as num
from
(
select ids,count(*) as cnt
from
(
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted
) as tb1
group by ids
) as tb2
order by num desc
limit 1
9.找出followee和follower
思路:B和D都在follower中,B的Follower是C,D,A不在follower中,D的follower是E
select f1.follower,count(distinct f2.follower) as num
from follow f1 join follow f2
on f1.follower = f2.followee
group by f1.follower
10.找出体育馆人数大于100且连续的天数大于3天的数据
思路:先找出所有大于100人的条件,在使用3次自连接,根据t1,t2,t3的id来进行排序
select distinct t1.*
from stadium t1,stadium t2,stadium t3
where t1.people >= 100 and t2.people >= 100 and t3.people >= 100
and
(
(t1.id-t2.id = 1 and t1.id-t3.id = 2 and t2.id-t3.id = 1)
or
(t2.id-t1.id = 1 and t2.id-t3.id = 2 and t1.id-t3.id = 1)
or
(t3.id-t2.id = 1 and t2.id-t1.id = 1 and t3.id-t1.id = 2)
)
order by t1.id
SQL查询练习二(From LeetCode)的更多相关文章
- SQL查询(二)
常用查询技巧 1.获取数据的前3(n)行 ; 2.SQL语句中if语句 在SQL语句中没有直接的if语句,但是有两个函数:DECODE和CASE,他们能够实现if语句的功能 2.1)decode -- ...
- SQL查询练习一(From LeetCode)
请选用MySQL进行测试. 1.找出后一天比前一天温度高的日期(E) 思路:将该表进行自关联,然后选择出日期靠后的一天,最后选择温度高的日期 select Weather.Id from Weath ...
- SQL总结(二)连表查询
---恢复内容开始--- SQL总结(二)连表查询 连接查询包括合并.内连接.外连接和交叉连接,如果涉及多表查询,了解这些连接的特点很重要. 只有真正了解它们之间的区别,才能正确使用. 1.Union ...
- SQL基础--查询之二--连接查询
SQL基础--查询之二--连接查询
- SQL入门经典(二) 之数据库基本查询、添加、更新和删除
使用SQL查询: SQL查询基本语法: SELECT [ALL|DISTINCT] [TOP (<expression>) [PERCENT] [WITH TIES] ] <col ...
- SQL Server-聚焦深入理解动态SQL查询(三十二)
前言 之前有园友一直关注着我快点出SQL Server性能优化系列,博主我也对性能优化系列也有点小期待,本来打算利用周末写死锁以及避免死锁系列的接着进入SQL Server优化系列,但是在工作中长时间 ...
- SQL开发技巧(二)
本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列文章基于SQLServer系列,且版本为SQLServer2005及以上-- 文章系列目录 SQL开发技巧(一) SQL开 ...
- Oracle常用SQL查询(2)
三.查看数据库的SQL 1 .查看表空间的名称及大小 select t.tablespace_name, round ( sum (bytes / ( 1024 * 1024 )), 0 ) ts ...
- Thinkphp查询 1.查询方式 2.表达式查询 3.快捷查询 4.区间查询 5.组合查询 6.统计查询 7.动态查询 8.SQL 查询
1.使用字符串作为条件查询 $user = M('User'); var_dump($user->where('id=1 AND user="蜡笔小新"')->sele ...
随机推荐
- css3 边框阴影效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AtCoder Beginner Contest 067 C - Splitting Pi
C - Splitting Pile Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Snu ...
- Redux 总结
1.redux: function reducer(state=-,action) { switch(action.type) { case '': return ... case '': retur ...
- 安装成功的nginx,如何添加未编译安装模块(非覆盖安装http_image_filter_module)
背景:1.做了图片上传小项目.2.图片上传,需要多图管理.3.图片上传,需要存储到Fastdfs.4.Fastdfs上的图片,和Nginx结合.5.Nginx从Fastdfs获得的图片,需要使用缩略图 ...
- ubuntu下vim中内容拷贝到浏览器
在vim中编辑好了代码想要复制出来到浏览器或者其它地方.用yy复制后去别的地方粘帖发现根本不是当初复制的内容,非常头疼-- 这是由于vim中有它自己的一套剪贴板系统(clipboard).这套系统和u ...
- Android控件ToggleButton的使用方法
ToggleButton(开关button)是Android系统中比較简单的一个组件,是一个具有选中和未选择状态双状态的button.而且须要为不同的状态设置不同的显示文本. ToggleButton ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- dreamweaver 8的替换功能
dreamweaver 8的替换功能 下面教你用dreamweaver 8的替换功能来删除这些冗余代码. 查找范围:文件夹(然后选取你需要替换的文件夹) 搜索:源代码查找:\btppabs=" ...
- Linux / Windows应用方案不完全对照表
Linux/Windows应用方案不完全对照表 650) this.width=650;" border="0" src="http://img1.51cto. ...
- javaweb三、JDBC访问数据库
JDBC是J2SE的内容,是由java提供的访问数据库的接口,但没有提供具体的实现方法,需要数据库厂商提供,就是对应的数据库驱动. 这样的好处是可以方便的更换数据库,提高了扩展性.这也是面向接口编程的 ...