sql練習_2
查詢倒數第3個入職的人員資訊
牛客網sql
我的思路: 對hire_date進行倒排 + 取第三個
select *
from employees
order by hire_date
desc limit 2,1;
考慮到可能有多個員工資訊,嚴謹一些可以寫為:
#嚴謹的寫法
select *
from employees
where hire_date=(
select distinct hire_date
from employees
order by hire_date desc
limit 2,1)
從別人那裡學習到的思路1:
使用開窗函式,把所有資料作為乙個組:
select emp_no,birth_date,first_name,last_name,gender,hire_date
from(
select *,
dense_rank() over(order by hire_date desc )as rank
from employees
) awhere rank=3;
從別人那裡學到的思路2,利用笛卡爾積:
正確答案:
select *
from employees
where hire_date=
(select a.hire_date
from employees a,employees b
where a.emp_no!=b.emp_no and a.hire_date該牛人利用了笛卡爾積的特點,不過題解中他給到的題解**為:
select *
from employees
where hire_date=
(select min(a.hire_date)
from employees a,employees b
where a.emp_no!=b.emp_no and a.hire_date我認為上面的count()不應該是小於3,應該為=2,並且不需要min,所以牛人的思路是非常棒的,但是給到的答案可優化。
MYSQL練習隨筆
解法練習 案例1.子查詢練習 字段 說明 film id 電影id title 電影名稱 description 電影描述資訊 category id 電影分類id name 電影分類名稱 last update 電影分類最後更新時間 film id 電影id category id 電影分類id ...
PTA練習隨筆
本文是做題過程中學習到的一些經驗總結 1.對兩個整數做除法想得到浮點數時,可以對任意乙個變數先做乘法,乘1.0,但是要注意一定是先乘1.0再做除法,否則就會出錯 正確 printf d d 2f a,b,a b 1.0 錯誤 printf d d 2f a,b,a b 1.0 由於除數乘1.0還需要...
SQL上機練習題2
14道練習題 oracle自帶hr方案 新知識 9.可以用列號表示排序條件 10.使用 可以彈出input框,讓查詢更加靈活 11.同理 hr 部門需要您協助建立一些查詢。1 因為預算問題,hr 部門需要乙個報表,用於顯示薪金高於 12,000 的員工 的姓氏和薪金。將您的 sql 語句儲存到名為 ...