LeetCode上的几道数据库题目
来源:互联网 发布:色容差计算软件 编辑:程序博客网 时间:2024/06/09 15:36
Combine Two Tables
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
用Left Join即可,不会看我转载的这篇文章:数据库表的连接(Left join , Right Join, Inner Join)用法详解
select Person.FirstName,Person.LastName,Address.City,Address.State from Person left join Address on Person.PersonId = Address.PersonId;
Second Highest Salary
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 second highest salary is 200. If there is no second highest salary, then the query should return null.
感觉题目并没有说用SecondHighestSalary作为别名啊,一直错,看了Discuss才知道。
select max(Salary) as SecondHighestSalary from Employee where Salary < (select max(Salary) from Employee)
利用不存在的话max会返回null,所以不需要单独判断。
Nth Highest Salary
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.
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INTBEGIN 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
关键是limit的使用,limit m,n是指从第m+1条开始(包括它),取n条。
- LeetCode上的几道数据库题目
- leetcode上数据库的题目(1)
- 搜集的几道C语言和数据库题目
- 几道有意思的题目
- Ruby的几道题目
- leetcode MYSQL数据库题目
- [编程题] LeetCode上的Palindrome(回文)类型的题目
- [编程题] LeetCode上的Tree类型的题目
- Leetcode平台上的TwoSum题目用Java哈希表实现
- LeetCode上Tag为贪心算法(Greedy)的题目整理
- 第一次在leetcode上做hard级别的题目
- 几道C语言的题目
- 几道c语言的题目
- AStar2008-2009的几道题目
- 几道c语言的题目
- 几道c语言的竞赛题目
- 有意思的几道C 题目
- 几道sql的面试题目
- 基础算法之动态规划
- Oracle数据库分页,主页自增长,CONCAT语法
- 【t081】序列长度(贪心做法)
- 笔记-Hibernate
- BaseActivity的简单封装
- LeetCode上的几道数据库题目
- typedef函数指针用法
- Python爬虫入门系列 笔记一
- C++ 不用std::queue 实现读取网络字节缓冲去的队列
- TCP连接的坑总结
- 后缀表达式与中缀表达式互转的理论知识【转】
- 现代编译原理 第一章
- 1040. Longest Symmetric String (25)
- Fragment实现懒加载以及简单封装