美文网首页
刷题笔记 database

刷题笔记 database

作者: 吕琪_e6a3 | 来源:发表于2017-07-12 04:24 被阅读0次

175. Combine Two Tables

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

Note: FirstName, LastName, City, State来自不同的table, 所以用join, 要求必须显示人名,所以用left join(left join = left outer join)

# Write your MySQL query statement below

SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address

ON Person.PersonId = Address.PersonId

176 Second Highest Salary

解法1: using MAX(only used in this problem)

# Write your MySQL query statement below

select MAX(Salary) as SecondHighestSalary from Employee

where Salary < (select MAX(Salary) from Employee)

解法2: using limit & offset

# Write your MySQL query statement below

select (select distinct Salary from Employee order by Salary DESC limit 1 offset 1)

as SecondHighestSalary

Note: if we only write (select distinct Salary from Employee order by Salary DESC limit 1 offset 1) and there is no second highest salary, then the query should return “ ” instead of null

解法3:distinct count (can be used in Nth highest)

select (select MAX(Salary) from Employee as e

where 2 = (select count(distinct Salary) from Employee as p where e.Salary <= p.Salary))

AS SecondHighestSalary

Note: using MAX(Salary) to avoid multiple value return from subquery

177. Nth Highest Salary

解法1:distinct count 

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT

BEGIN

RETURN (

# Write your MySQL query statement below.

select (select Max(Salary) from Employee as e

where N = (select count(distinct Salary) from Employee as p where e.Salary <= p.Salary))

);

END

解法2:limit

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT

BEGIN

DECLARE M INT;

SET M=N-1;

RETURN (

# Write your MySQL query statement below.

SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1

);

END

Append:

Using MySQL LIMIT to Constrain The Number of Rows Returned By SELECT Statement

Introduction to MySQL LIMIT clause

The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result set. The LIMIT clause accepts one or two arguments. The values of both arguments must be zero or positive integers.

The following illustrates the LIMIT clause syntax with two arguments:

SELECT

column1,column2,...

FROM

table

LIMIT offset, count;

Let’s examine the LIMIT clause parameters:

The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.

The count specifies the maximum number of rows to return.

When you use LIMIT with one argument, this argument will be used to specifies the maximum number of rows to return from the beginning of the result set.

SELECT

column1,column2,...

FROM

table

LIMIT count;

相关文章

  • 刷题笔记 database

    175. Combine Two Tables Write a SQL query for a report th...

  • 晨间日记

    计算机刷题 看书写笔记 高数刷题 英语刷题 奋斗到天亮,加油奥利给

  • LeetCode中的SQL

    下面记录LeetCode中所有database中的所有free题目。 爱刷题的人从不怕找不到目录:

  • 谷歌工程师为金三银四筹备1000道Leetcode刷题笔记

    对于刷题相关的文章,在之前我也推荐过不少,今天再给大家推荐一份算法刷题笔记,这份笔记与以往的刷题有所区别,作者把 ...

  • 刷题笔记

    算法思想 一、二分查找 1. 算法思想 算法详解 算法细节 一定要看二分查找细节.md 实现时需要注意以下细节: ...

  • 刷题笔记

    最近在准备面试,发现自己真的菜的不行,就计划接下来的时间把 leetcode 上面刷的 中等题 和 每日一题做个简...

  • 刷题笔记

    题目描述 343 - Integer BreakGiven a positive integer n, break...

  • 抓住小尾巴

    最近忙着刷题,感觉效率不是很高,白天看录播视频做笔记,晚上刷题。参加了一个集训营,只要每天刷完固定的题量,达到了七...

  • 字节总监首发1121道LeetCode算法刷题笔记(含答案)

    关于算法刷题的困惑和疑问也经常听朋友们提及。这份笔记里面共包含作者刷LeetCode算法题后整理的数百道题,每道题...

  • 字节总监首发1121道LeetCode算法刷题笔记(含答案)

    关于算法刷题的困惑和疑问也经常听朋友们提及。这份笔记里面共包含作者刷LeetCode算法题后整理的数百道题,每道题...

网友评论

      本文标题:刷题笔记 database

      本文链接:https://www.haomeiwen.com/subject/tcpvhxtx.html