牛客SQL練習第02題

2021-10-24 22:42:28 字數 1329 閱讀 3215

查詢入職員工時間排名倒數第三的員工所有資訊,為了減輕入門難度,目前所有的資料裡員工入職的日期都不是同一天

create tableemployees(

emp_noint(11) not null,

birth_datedate not null,

first_namevarchar(14) not null,

last_namevarchar(16) not null,

genderchar(1) not null,

hire_datedate not null,

primary key (emp_no));

如果說員工入職日期有相同的,並且相同入職時間的排序不相同,可以直接排序限制取值。

select

*from employees order

by hire_date desc

limit2,

1

如果說員工入職日期有相同的,並且相同入職時間的排序相同,就可以取倒數第三的時間,可以對入職時間去重排序限制取值。

-- 用distinct去重

select

*from employees

where hire_date=

(select

distinct hire_date from employees order

by hire_date desc

limit2,

1);

-- 用group by分組去重

select

*from employees

where hire_date =

(select hire_date from employees group

by hire_date order

by hire_date desc

limit2,

1);

就本題而言,由於員工的入職日期各不相同,所以用上面哪個都沒問題,去掉distinct或group by也行:

select

*from employees

where hire_date =

(select hire_date from employees order

by hire_date desc

limit2,

1);

牛客SQL練習第57題

使用含有關鍵字exists查詢未分配具體部門的員工的所有資訊。create table employees emp no int 11 notnull birth date date notnull first name varchar 14 not null last name varchar 1...

牛客SQL練習第60題

按照salary的累計和running total,其中running total為前n個當前 to date 9999 01 01 員工的salary累計和,其他以此類推。具體結果如下demo展示。create table salaries emp no int 11 notnull salary...

牛客SQL練習第68題

牛客每天有很多人登入,請你統計一下牛客每個使用者最近登入是哪一天,用的是什麼裝置.有乙個登入 login 記錄表,簡況如下 第1行表示id為2的使用者在2020 10 12使用了客戶端id為1的裝置登入了牛客網 第4行表示id為3的使用者在2020 10 13使用了客戶端id為2的裝置登入了牛客網 ...