SQL查询练习一(From LeetCode)

1 select name,population,area
2 from World
3 where area > 3000000 or population > 25000000
 

3.判断是否是三角形(E)

思路,使用case when进行搭配,使用三角形定义进行判断x+y>z,x+z>y,y+z>x

1 select x,y,z,
2 case
3 when x+y>z and y+z>x and x+z>y then 'Yes'
4 else 'No'
5 end as triangle
6 from triangle

4.找出薪水第二高的员工

思路:先找出最多的薪水的员工,在把他的薪水小于最大的工资即可

1 select Max(Salary) as SecondHighestSalary
2 from Employee
3 where Salary < (select Max(Salary) from Employee)

5.找出每个学科都有多少名学生(M)

思路:将两张表进行左连接,一department表作为主表,然后按照dept_name进行分组,最后按照人数进行降序排列

1 select d.dept_name,count(student_id) as student_number
2 from department d left join Student s
3 on d.dept_id = s.dept_id
4 group by d.dept_name
5 order by student_number desc,d.dept_name

6.找出每个部门薪水最高的员工(M)

思路:将两张表进行连接,内层查询根据department表的name进行分组,每组的最大值,既是每个部门的薪水最大值,然后传递给外层的部门id和薪水即可

1 select d.Name as Department,e.Name as Employee,e.Salary as Salary
2 from Department d join Employee e
3 on e.DepartmentId = d.Id
4 where (e.DepartmentId,e.Salary) in
5 (
6 select DepartmentId,max(Salary)
7 from Employee
8 group by DepartmentId
9 )

7.找出至少有5名下属的领导(M)

思路:使用内层查询查找出有5名下属的ManagerId然后,将外层查询的员工Id=ManagerId就是查询的结果

1 select e1.Name
2 from Employee e1
3 join
4 (
5 select ManagerId from Employee
6 group by ManagerId
7 having count(*) >= 5
8 ) as e2
9 on e1.Id = e2.ManagerId

8.找出得票最多的候选人(M)

思路:先在内层查询中找出最受欢迎的候选人,然后将中间表的候选人Id既是赢家的候选人id,两者相等即可

 1 select c.Name
2 from Candidate c
3 join
4 (
5 select CandidateId from Vote
6 group by CandidateId
7 order by count(*) desc
8 limit 1
9 ) as winner
10 on c.id = winner.CandidateId;

9.根据Score计算等级(M)

思路:将两张表进行自连接,根据Id进行分组,最后根据Rank进行排序

1 select s.Score,count(distinct t.Score) as Rank
2 from Scores s join Scores t
3 on s.Score <= t.Score
4 group by s.Id
5 order by Rank

10.找出二叉树的节点分布(M)

思路:使用case when的结构进行循环判断输出

 1 select id,
2 case
3 when tree.id = (select atree.id from tree atree where atree.p_id is NULL)
4 then 'Root'
5 when tree.id in (select atree.p_id from tree atree)
6 then 'Inner'
7 else
8 'Leaf'
9 end as Type
10 from tree
11 order by id

11.找出每个部门薪水排前三名的员工(H)

思路:先进行表连接,将内层查询的结果和外部的表的Salary相比较,选择前面3个

 1 select d.Name as Department,e.Name as Employee,e.Salary
2 from Employee e join Department d
3 on e.DepartmentId = d.Id
4 where 3 >
5 (
6 select count(distinct e2.Salary)
7 from Employee e2
8 where e2.Salary > e.Salary
9 and e.DepartmentId = e2.DepartmentId
10 )

12.找出2013-10-01到2013-10-03之间的网约车的取消率(H)

思路:计算取消率,使用case when语法,找出Trips中Status变量以canceled_开头的比例

 

13.找出每个部门员工薪水的中位数(H)

思路:将此表进行自关联,计算工资的中位数,使用case when计算中间表的中位数

1 select e.Id,e.Company,e.Salary
2 from Employee e join Employee aliens
3 on e.Company = aliens.Company
4 group by e.Company,e.Salary
5 having sum(case when e.Salary = aliens.Salary then 1 else 0 end) >=
6 abs(sum(sign(e.Salary-aliens.Salary)))
7 order by e.Id

LeetCode SQL的更多相关文章

  1. LeetCode SQL题目(第一弹)

    LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...

  2. [LeetCode]Sql系列4

    ##题目1 626. 换座位 小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id. 其中纵列的 id 是连续递增的 小美想改变相邻俩学生的座位. ...

  3. [Leetcode]Sql系列3

    题目1 产品数据表: Products +---------------+---------+ | Column Name | Type | +---------------+---------+ | ...

  4. [LeetCode]Sql系列2

    题目 1205. 每月交易II Transactions 记录表 +----------------+---------+ | Column Name | Type | +-------------- ...

  5. [Leetcode|SQL] Combine Two Tables

    Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...

  6. LeetCode SQL: Second Highest Salary

    , NULL, salary) as `salary` from ( ,) tmp Write a SQL query to get the second highest salary from th ...

  7. [LeetCode]Sql系列

    题目1 Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. +----+-------+--------+--------------+ ...

  8. LeetCode SQL:Employees Earning More Than Their Managers

    # Write your MySQL query statement below SELECT a.Name FROM Employee AS a INNER JOIN Employee AS b O ...

  9. leetcode 182. Duplicate Emails having的用法 SQL执行顺序

    https://leetcode.com/problems/duplicate-emails/description/ 首先sql的执行顺序是 from-->where-->group b ...

随机推荐

  1. 字符串中连续出现最多的子串 &amp; 字符串中最长反复子串

    字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀 ...

  2. jQuery中focusin()和focus()、find()和children()的差别

    jQuery中focus()和focusin().focus()和children()的差别 focus()和focusin() focus()和focusin()的差别在于focusin()支持事件 ...

  3. 确保 Xcode 每次 Build 时都自己主动更新资源

    參考:p=22" target="_blank">http://quick.cocoachina.com/?p=22 刚建立的quickproject.每次修改lu ...

  4. iOS开发UI调试神器----Reveal

    做iOS的开发,UI是非常非常重要的一环.调试时我们一般用模拟器,提交前用真机做測试.用模拟器来调试UI效果尽管快捷方便,但有时仍然希望有更强大的工具来帮助分析UI,尤其是专注在UI的效果调试时.近期 ...

  5. GitBlit中出现 error: remote unpack failed: error Missing tree

    clu@WASYGSHA01-1020 MINGW64 /d/ChuckLu/Git/Edenred/LISA_5.0.0.0 (local)$ git push origin preaction:p ...

  6. 安装Oracle RAC 11g

    1.Oracle Enterprise Linux 和 iSCSI 上构建 Oracle RAC 11g 集群 2.Oracle RAC 的所有共享磁盘存储将基于 iSCSI,iSCSI 使用在第三个 ...

  7. [JZOJ 5893] [NOIP2018模拟10.4] 括号序列 解题报告 (Hash+栈+map)

    题目链接: https://jzoj.net/senior/#main/show/5893 题目: 题解: 考虑暴力怎么做,我们枚举左端点,维护一个栈,依次加入元素,与栈顶元素和栈内第二个元素相同时弹 ...

  8. 【实战经验】64位Win7安装+32位Oracle + PL/SQL 解决方法

    软件环境:64位win7.32位Oracle 10g. PL/SQL 9.0.4.1644 前言:以前开发用的都是32位系统,突然换到64位上,安装环境真的有点麻烦了,尤其对于PL/SQL只支持32位 ...

  9. SQL 中多个 and or 的组合运算

    sql关系型运算符优先级高到低为:not >and> or AND.OR运算符的组合使用 在WHERE子句中,通过AND.OR运算符可以同时连接多个条件,当然AND.OR运算符也可以同时使 ...

  10. STM8S103 STVD编译空间不足

    关于text空间(理解为代码空间)不足问题 # 关于.bsct和.ubsct问题(着重参考http://www.waveshare.net/article/STM8-3-1-10.htm) map文件 ...