【175】Combine Two Tables (2018年11月23日,开始集中review基础)

Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people.
FirstName, LastName, City, State 

题解:因为题目要求说person表里面有的项目即使address表里没有也需要展示,所以用 left join

select Person.FirstName as FirstName, Person.LastName as LastName, Address.City as City, Address.State as State from Person left join Address on Person.PersonId = Address.PersonId;

【176】Second Highest Salary (第二高的工资)(2018年11月23日)

Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+ For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+

注意,题目有个要求,如果没有第二高的工资要返回 null,而不是空条目。还有一个问题就是如果表里只有两条,但是两条的工资都是100, 这个需要返回 null,不是 100,所以要用 distinct

我一开始写成了如下,但是没有第二高的工资要返回 null 这个条件不满足。所以 WA。

select Salary as SecondHighestSalary from Employee order by Salary desc limit 1, 1;

后来看了答案,答案说要重新搞一张表。(limit 1,1 和 limit 1 offset 1 是等价的)

select (select distinct Salary from Employee order by Salary desc limit 1 offset 1) as SecondHighestSalary;

【177】Nth Highest Salary (2018年11月23日)

Write a SQL query to get the nth highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+ For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+

题意就是返回第 N 高的工资。和上面一题很像。注意点就是不能直接写 limit N-1, 1 语法会出错。

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
#limit m, n 表示的是从第 m 个条目(m is 0 based)开始的 n 条, 如果下面直接用 N-1 的话不行的,语法错误。
DECLARE M INT;
SET M = N - 1;
RETURN (
select (select distinct Salary from Employee order by Salary desc limit M, 1)
);
END

【178】Rank Scores (2018年11月23日)

【180】Consecutive Numbers (2018年11月23日)

Write a SQL query to find all numbers that appear at least three times consecutively.
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+ For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+

题解:本来不会写,后来找了题解:https://my.oschina.net/Tsybius2014/blog/494823

可以用 select, 也可以 join, 还有一种通解的写法(如果把 3 延长到 N怎么办)

select distinct L1.Num as ConsecutiveNums
from Logs L1, Logs L2, Logs L3
where (L1.Id + 1 = L2.Id AND L1.Num = L2.Num) AND (L1.Id + 2 = L3.Id AND L2.Num = L3.Num)

【181】Employees Earning More Than Their Managers

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+ Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
+----------+
| Employee |
+----------+
| Joe |
+----------+

题解:别名的使用。

select e.Name as Employee
from Employee as e, Employee as m
where e.ManagerId = m.Id and e.Salary > m.Salary

【182】Duplicate Emails (group by ... having ..子句, 2018年11月23日)

Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+ For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
Note: All emails are in lowercase.

题解:

select Email
from Person
group by Email
having count(Email) > 1

【183】Customers Who Never Order

【184】Department Highest Salary

【185】Department Top Three Salaries

【196】Delete Duplicate Emails

【197】Rising Temperature

【262】Trips and Users

【569】Median Employee Salary

【570】Managers with at Least 5 Direct Reports

【571】Find Median Given Frequency of Numbers

【574】Winning Candidate (2018年11月24日)

Table: Candidate
+-----+---------+
| id | Name |
+-----+---------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
+-----+---------+
Table: Vote
+-----+--------------+
| id | CandidateId |
+-----+--------------+
| 1 | 2 |
| 2 | 4 |
| 3 | 3 |
| 4 | 2 |
| 5 | 5 |
+-----+--------------+
id is the auto-increment primary key,
CandidateId is the id appeared in Candidate table.
Write a sql to find the name of the winning candidate, the above example will return the winner B.
+------+
| Name |
+------+
| B |
+------+
Notes:
You may assume there is no tie, in other words there will be at most one winning candidate.

题解:注意 group by 和 order by 一起使用的时候,order by 中的列必须要出现在 group by 中。(solution里面还有别的方法)

select Name
from Candidate
where id = (select CandidateId from Vote group by CandidateId order by count(CandidateId) desc limit 1);

【577】Employee Bonus (2018年11月23日)

Select all employee's name and bonus whose bonus is < 1000.
Table:Employee
+-------+--------+-----------+--------+
| empId | name | supervisor| salary |
+-------+--------+-----------+--------+
| 1 | John | 3 | 1000 |
| 2 | Dan | 3 | 2000 |
| 3 | Brad | null | 4000 |
| 4 | Thomas | 3 | 4000 |
+-------+--------+-----------+--------+
empId is the primary key column for this table.
Table: Bonus
+-------+-------+
| empId | bonus |
+-------+-------+
| 2 | 500 |
| 4 | 2000 |
+-------+-------+
empId is the primary key column for this table.
Example ouput:
+-------+-------+
| name | bonus |
+-------+-------+
| John | null |
| Dan | 500 |
| Brad | null |
+-------+-------+

题解:left join语句,还用到了 sql 的三值逻辑(true, false, unkown)

select emp.name as name, bon.bonus as bonus
from Employee as emp
left join Bonus as bon on emp.empId = bon.empId
where bon.bonus < 1000 or bon.bonus is null

【578】Get Highest Answer Rate Question

【579】Find Cumulative Salary of an Employee

【580】Count Student Number in Departments

【584】Find Customer Referee (2018年11月23日)

Given a table customer holding customers information and the referee.
+------+------+-----------+
| id | name | referee_id|
+------+------+-----------+
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
+------+------+-----------+
Write a query to return the list of customers NOT referred by the person with id '2'.
For the sample data above, the result is:
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

题解:本题需要了解的知识点是, sql 的三值逻辑: true, false, unkown。一切和 null 比较的值都是 unkown, 包括 null 本身。所以 sql 提供了 'is null' 和 'is not null' 这两个关键词。

select name from customer where referee_id <> 2 or referee_id is null;

如果条件中只有 referee_id <> 2 这一个条件的话,那么只会返回 Zack 这一个结果。

【585】Investments in 2016

【586】Customer Placing the Largest Number of Orders

【595】Big Countries

【596】Classes More Than 5 Students

【597】Friend Requests I: Overall Acceptance Rate

【601】Human Traffic of Stadium

【602】Friend Requests II: Who Has the Most Friends

【603】Consecutive Available Seats

【607】Sales Person

【608】Tree Node

【610】Triangle Judgement

【612】Shortest Distance in a Plane

【613】Shortest Distance in a Line

【614】Second Degree Follower

【615】Average Salary: Departments VS Company

【618】Students Report By Geography

【619】Biggest Single Number

【620】Not Boring Movies

【626】Exchange Seats

【sql】leetcode习题 (共 42 题)的更多相关文章

  1. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

  2. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  3. leetcode面试题42. 连续子数组的最大和

      总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目   面试题42. 连续子数 ...

  4. 关于SQL的几道小题详解

    关于SQL的几道小题详解 当我们拿到题目的时候,并不是急于作答,那样会得不偿失的,而是分析思路,采用什么方法,达到什么目的,还要思考有没有简单的方法或者通用的方法等等,这样才会达到以一当十的效果,这样 ...

  5. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  6. 剑指offer 面试42题

    面试42题: 题目:连续子数组的最大和 题:输入一个整形数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为O(n) 解题思路:在数组里从前 ...

  7. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  8. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  9. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

随机推荐

  1. common-dbcp2数据库连接池参数说明(转)

    转自:http://bsr1983.iteye.com/blog/2092467 由于commons-dbcp所用的连接池出现版本升级,因此commons-dbcp2中的数据库池连接配置也发生了变化, ...

  2. java运算注意事项

    /* 对于byte.short.char.插入三种类型来说,如果右侧固执的数值没有超过范围,那么java编译器就会自动隐含地位我们 补上一个(byte) ,(short),(char) 1.如果没有超 ...

  3. Nginx有哪些作用?

    Nginx有哪些作用? http协议代理 搭建虚拟主机 服务的反向代理 在反向代理中配置集群的负载均衡   什么是正向代理? 正向代理,意思是一个位于客户端和原始服务器(origin server)之 ...

  4. OAuth_2

    角色: OAuth2.0为用户和应用定义了如下角色: 资源拥有者.资源服务器.客户端应用.授权服务器 资源拥有者:拥有共享数据的人或应用,比如Facebook的用户就是 资源拥有者,但资源拥有者也可以 ...

  5. css实现下拉框导航条

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  6. 【串线篇】依赖注入DI与控制反转IOC

    DI&IOC 在spring框架中DI与IOC说的其实是一回事 一句话:本来我接受各种参数来构造一个对象,现在只接受一个参数——已经实例化的对象. 也就是说我对对象的『依赖』是注入进来的,而和 ...

  7. 最大流的SAP算法模板

    明天补充~~~先上代码 #include<iostream> #include<string> #include<queue> #include<vector ...

  8. array_shift — 将数组开头的单元移出数组

    <?php $stack = array("orange", "banana", "apple", "raspberry&q ...

  9. jQuery的ajaxFileUpload上传文件插件刷新一次才能再次调用触发change

    jQuery的ajaxFileUpload插件 关于用ajaxfileupload时,遇到一个要刷新一次页面才能再次上传,用live()方法来绑定 file表单 的change事件就能够解决,直接$( ...

  10. JavaSE---多线程---概述

    1.概述 1.1 进程: 系统进行资源分配.调度的一个独立单元: 进程的特征: 1.1.1 独立性: 系统中独立存在的实体,拥有自己独立的资源: 每个进程都拥有自己私有的地址空间,在没有经过进程本身允 ...