1.组合两个表

表1: Person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+
PersonId 是上表主键

表2: Address
+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+
AddressId 是上表主键

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
FirstName, LastName, City, State

这一题,比较简单用left join就可以了。

SQL语句如下:

select FirstName, LastName, City, State from Person a left join Address b on a.PersonId=b.PersonId

2.第二高的薪水

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

这题也是一个入门题,找到排名第二的薪水,第一步先找到薪水最高的,然后排除掉,剩下的里面最高的也就是第二个的薪水。当查询不到时,SQL会自动返回为NULL。

SQL语句如下:

SELECT MAX(Salary)  SecondHighestSalary FROM  Employee A  WHERE Salary<>
  (SELECT MAX(Salary) FROM Employee )

3.第N高的薪水

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

这是一个中级题,第一步要排除用户输入的无效数字,比如负数和大于去重后的行数的,赋默认值0,返回null值。

第二步去重,使用distinct去除薪水相同的,然后按从大到小排列,使用top取前n行,再取其中最小的就是第n高的薪水

SQL语句如下:

CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS
BEGIN
declare @num INT
    select @num=count(distinct Salary) from EMPLOYEE
   if(@N<0 or @N>@num)
    set @N=0
    RETURN (
       select min(Salary)  from EMPLOYEE where  Salary in (
       SELECT DISTINCT TOP (@N)  Salary FROM EMPLOYEE  order by Salary desc)        
    );
END

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/second-highest-salary

LeetCode-SQL(一)的更多相关文章

  1. LeetCode SQL题目(第一弹)

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

  2. LeetCode SQL

    SQL查询练习一(From LeetCode) 1 select name,population,area 2 from World 3 where area > 3000000 or popu ...

  3. [LeetCode]Sql系列4

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

  4. [Leetcode]Sql系列3

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

  5. [LeetCode]Sql系列2

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

  6. [Leetcode|SQL] Combine Two Tables

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

  7. LeetCode SQL: Second Highest Salary

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

  8. [LeetCode]Sql系列

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

  9. 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 ...

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

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

随机推荐

  1. 机器学习——k-均值算法(聚类)

    文章目录 k-均值(k-means)聚类 1.k-均值算法 2.k-均值算法的代价函数 3.k-均值算法步骤 4.初始化聚类中心点和聚类个数 5.sklearn实现k-means算法 k-均值(k-m ...

  2. shell脚本启动所有集群节点

    #profile变量追加到.bashrc中 cat /etc/profile >> ~/.bashrc #start-all-cluster.sh  启动脚本 #!/bin/bash ec ...

  3. hive使用beeline配置远程连接

    hive以hadoop集群为基础,提供hdfs的SQL支持: hive一般可以以mysql为元数据存储,默认Derby:hadoop,hive安装自行百度吧: 介绍hive的远程访问: 未配置之前使用 ...

  4. qt中窗体全屏

    原文地址:https://www.cnblogs.com/wiessharling/p/3750461.html 近期在学习QT时遇到了很多问题这也是其中一个,个人通过在各种书籍和网络上的查阅找到了一 ...

  5. FFT和NTT学习笔记_基础

    FFT和NTT学习笔记 算法导论 参考(贺) http://picks.logdown.com/posts/177631-fast-fourier-transform https://blog.csd ...

  6. dfs與bfs常用模板

    基本遍歷: //dfs void dfs(int x) { v[x]=1; for(int i=head[x];i;i=next[i]) { int y=ver[i]; if(v[y]) contin ...

  7. 深入js系列-类型(对象)

    开篇 值的传递方式 1.值传递 表示传递过程中复制了值 2.引用传递 表示传递过程中传递的是值的引用 js的传递方式 值传递 看下面的例子 // 这里值传递很容易理解 var a = 1 var b ...

  8. 绘制matplotlib 饼状图

    参考:https://blog.csdn.net/ScarlettYellow/article/details/80458797 (2)2016年就业人员在三次产业中分布的饼状图. def swap( ...

  9. sql语句之union与join的区别

    union查询: 使用 union 可以将多个select语句的查询结果组合起来. 语法: select 字段1,字段2 from table1 union select 字段1,字段2 from t ...

  10. springboot修改页面不用重启的设置(idea)

       1) “File” -> “Settings” -> “Build,Execution,Deplyment” -> “Compiler”,选中打勾 “Build project ...