select * from t limit2offset1;代表是从第1条后面数据开始取出2条数据,即读取第2,3条
select * from t limit2,1;代表跳过前两条数据取出1条数据,即读取第3条数据
NO178.分数排名 题目:编写一个 SQL 查询来实现分数排名。
code:
1 2 3 4
select a.Score, (selectcount(distinct Score) from Scores b where a.Score<=b.Score) asrank from Scores a orderby a.Score desc
题解:相当于统计b表中有多少个大于a表的元素。
NO180.连续出现的数字 题目:编写一个 SQL 查询,查找所有至少连续出现三次的数字。
code:
1 2 3 4 5
selectdistinct a.Num as ConsecutiveNums from Logs a leftouterjoinLogs b on a.Id=b.Id-1 leftouterjoinLogs c on b.Id=c.Id-1 where a.Num=b.Num and a.Num=c.Num
题解:三表join,Id差值为1,Num值相等。
NO184.部门工资最高的员工 题目:编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
code
1 2 3 4
select d.Name as Department,b.Name as Employee,c.max_salary as Salary from Employee b innerjoin (select DepartmentId,max(Salary) as max_salary from Employee a groupby1 ) c on b.DepartmentId=c.DepartmentId and b.Salary=c.max_salary innerjoin Department d on b.DepartmentId = d.Id
题解:inner join 操作
NO185.部门工资前三高的所有员工 题目:
code:
1 2 3 4 5 6 7 8 9 10
selectdistinct e.Name as Department,d.Name as Employee,c.Salary from (select a.DepartmentId,a.Salary, (selectcount(distinct b.Salary) from Employee b where b.Salary>=a.Salary and a.DepartmentId=b.DepartmentId) asrank from Employee a orderby a.DepartmentId,a.Salary desc) c innerjoin Employee d on c.DepartmentId = d.DepartmentId and c.Salary=d.Salary innerjoin Department e on c.DepartmentId = e.Id where c.rank<=3 orderby e.Name asc,c.Salary desc
上面这个方法有点复杂了,比较耗时。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
SELECT d.Name AS'Department', e1.Name AS'Employee', e1.Salary FROM Employee e1 JOIN Department d ON e1.DepartmentId = d.Id WHERE 3 > (SELECT COUNT(DISTINCT e2.Salary) FROM Employee e2 WHERE e2.Salary > e1.Salary AND e1.DepartmentId = e2.DepartmentId )
面试题03.数组中重复的数字 题目:在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
code:
1 2 3 4 5 6 7 8 9 10 11 12
#字典 classSolution: deffindRepeatNumber(self, nums: List[int]) -> int: d = {} for key in nums: d[key] = d.get(key,0)+1 for key in d: if d[key]>=2: return key break else: pass
code:
1 2 3 4 5 6 7 8 9 10 11 12
#遍历用set classSolution: deffindRepeatNumber(self, nums: List[int]) -> int: s = set() for items in nums: ll = len(s) s.add(items) if ll == len(s): return items break else: continue
面试题04.二维数组中的查找 题目: 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
code:
选对方向。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: deffindNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool: if len(matrix) == 0or len(matrix[0]) == 0: returnFalse left = len(matrix) right = len(matrix[0]) i, j = left - 1, 0 while i >= 0and j < right : if matrix[i][j] > target: i = i - 1 elif matrix[i][j] < target: j = j + 1 else: returnTrue returnFalse
classSolution: deflengthOfLongestSubstring(self, s: str) -> int: left, right, res = 0, 0, 0 length = len(s) windows = {} while right < length: c = s[right] if c notin windows: windows[c] = 1 else: windows[c] += 1 right += 1 while c in windows and windows[c]>1: c2 = s[left] windows[c2] = windows[c2] - 1 left += 1 res = max(res,right-left) return res