由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这题怎么做
相关主题
初级SQL问题SQL find distinct values in large table (转载)
SQL combine two columns from two different tables no shared columnserror of executing SQL query of string concatenation (转载
leetcode Nth Highest Salary 谁做出来了?这题咋做?
请教个SQL的问题报offer,加问问题
问个SQL query问一个大家面试时候的技术问题,求讨论,相信对大家也有用的。
这题怎么做?请教一个题,不太容易,要O(n)
求教一个SQL的问题CLRS 16.1-5 怎么做
compare two large tables SQL问一个atoi overflow的问题
相关话题的讨论汇总
话题: sql话题: salary话题: select话题: group话题: max
进入JobHunting版参与讨论
1 (共1页)
r********g
发帖数: 868
1
How will u find the highest and second highest salary in each group using a
single SQL query ?
have to write SQL code used in different platforms: e.g. both Oracle and SQL
Server
a*****e
发帖数: 21
2
select MAX(t1.salary), MAX(t2.salary), group
from table t1, table t2
where t1.group = t2.group and t1.salary > t2.salary
group by t1.group
就是不知道group by能不能这么用...

a
SQL

【在 r********g 的大作中提到】
: How will u find the highest and second highest salary in each group using a
: single SQL query ?
: have to write SQL code used in different platforms: e.g. both Oracle and SQL
: Server

r********t
发帖数: 395
3

almost correct.但是既然t1和t2都要在每个group里比较,那么需要group by两个:
测试过了。。。

【在 a*****e 的大作中提到】
: select MAX(t1.salary), MAX(t2.salary), group
: from table t1, table t2
: where t1.group = t2.group and t1.salary > t2.salary
: group by t1.group
: 就是不知道group by能不能这么用...
:
: a
: SQL

x***y
发帖数: 633
4
great, but need some minor modifications to the condition as
" from table t1, table t2
on t1.group = t2.group
where t1.salary >= t2.salary && t1.primaryKey != t2.primaryKey"
when considering 2 people with the same salary.

on t1.group = t2.group
where t1.salary >= t2.salary && t1.primaryKey != t2.primaryKey

【在 a*****e 的大作中提到】
: select MAX(t1.salary), MAX(t2.salary), group
: from table t1, table t2
: where t1.group = t2.group and t1.salary > t2.salary
: group by t1.group
: 就是不知道group by能不能这么用...
:
: a
: SQL

c******e
发帖数: 73
5
using rank function in oracle?
y*********e
发帖数: 518
6
不用GROUP BY。
找第二高的:
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees)
或者:
SELECT MIN(salary) FROM (
SELECT salary FROM employees ORDER BY salary RANGE 1 TO 2
) AS TEMP_TABLE
找第三高的以此类推。
执行的效率?若是salary上有B+ tree index,那么效率是O(logn)。

a
SQL

【在 r********g 的大作中提到】
: How will u find the highest and second highest salary in each group using a
: single SQL query ?
: have to write SQL code used in different platforms: e.g. both Oracle and SQL
: Server

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一个atoi overflow的问题问个SQL query
Amazon Selection Platform team怎么样?这题怎么做?
SQL query 求解!求教一个SQL的问题
问道sqlcompare two large tables SQL
初级SQL问题SQL find distinct values in large table (转载)
SQL combine two columns from two different tables no shared columnserror of executing SQL query of string concatenation (转载
leetcode Nth Highest Salary 谁做出来了?这题咋做?
请教个SQL的问题报offer,加问问题
相关话题的讨论汇总
话题: sql话题: salary话题: select话题: group话题: max