資料庫刷題2

2021-10-05 09:22:32 字數 1436 閱讀 2195

1、至少連續出現三次的數字

select distinct(l1.num) as consecutivenums

from logs l1, logs l2, logs l3

where l1.id + 1= l2.id

and l2.id + 1= l3.id

and l1.num = l2.num

and l2.num = l3.num

2、查詢重複的電子郵箱

select distinct(p1.email)

from person p1, person p2

where p1.email = p2.email

and p1.id != p2.id

3、部門工資最高的員工

select d.name as department, e1.name as employer, e1.salary as salary

from employee e1 join department d

on e1.departmentid = d.id

where e1.salary >= all(select salary

from employee e2

where e1.departmentid = e2.departmentid)

select d.name as department, e.name as employee, e.salary as salary

from employee e join department d

on e.departmentid = d.id

where (e.departmentid, e.salary) in

(select departmentid, max(salary)

from employee

group by departmentid)

4、部門工資前三高的員工

編寫乙個sql查詢,找出每個部門獲得前三高工資的所有員工。

select d.name as department, e1.name as employee, e1.salary as salary

from employee e1 join department d

on e1.departmentid = d.id

where 3 >= (select count(distinct salary)

from employee e2

where e1.departmentid = e2.departmentid

and e1.salary <= e2.salary)

order by department, employee, salary desc

資料庫刷題

考慮到可能不是每個人都有位址資訊,我們應該使用outer join而不是預設的inner join。注意 如果沒有某個人的位址資訊,使用 where 子句過濾記錄將失敗,因為它不會顯示姓名資訊。2.編寫乙個 sql 查詢,獲取 employee 表中第二高的薪水 salary select max ...

LeetCode刷題 資料庫

目錄 175.組合兩個表 176.第二高的薪水 表1 person 列名 型別 personid int firstname varchar lastname varchar personid 是上表主鍵表2 address 列名 型別 addressid int personid int city...

面試刷題 資料庫

1 請回答一下什麼是事務?概念 資料庫事務 transaction 是訪問並可能操作各種資料項的乙個資料庫操作序列,這些操作要麼全部執行,要麼全部不執行,是乙個不可分割的工作單位。事務由事務開始與事務結束之間執行的全部資料庫操作組成。性質 作用 模型 優點 2 內連線和外連線有什麼區別?內連線 外連...