mysql怎麼刷題 Mysql資料庫刷題1

2021-10-20 23:10:03 字數 1050 閱讀 7761

1、查詢入職員工時間排名倒數第三的員工的所有資訊

create table `employees` (

`emp_no` int(11) not null,

`birth_date` date not null,

`first_name` varchar(14) not null,

`last_name` varchar(16) not null,

`gender` char(1) not null,

`hire_date` date not null,

primary key (`emp_no`));

select *

from employees e1

where 3 =

(select count(distinct hire_date)

from employees e2

where e2.hire_date >= e1.hire_date)

distinct去重,那排名靠前並且同一天入職的員工只需要統計一次。

2、第二高的薪水。如果不存在第二高的薪水,那麼查詢應返回null

select (select distinct salary

from employee

order by salary desc

limit 1,1)

法二select ifnull((select distinct salary

from employee

order by salary desc

limit 1,1), null) as secondhighestsalary

3、分數排名

編寫乙個sql查詢來實現分數排名。如果兩個分數相同,則兩個分數排名(rank)相同。

select score,

(select count(distinct score)

from scores

where score >= s.score)

as rank

from scores s

order by score desc

MySQL 牛客刷題

第二題 思路 1.有條件的查詢,用子查詢 where限制裡寫入內容 2.首先用distinct對資料進行去重處理 3.然後按 hire date 倒序排列 4.用limit找出目標資料 主查詢 5.正常的select語句輸出目標的所有資訊 知識點 1.關鍵字 distinct 在表中,可能會含有重複...

MySQL刷題筆記 日期

日期 年 月 日的匹配 如果我們表中的某個日期字段資料格式為yyyy mm dd hh mm ss,需要匹配這個表下某個日期的資料時,可以使用 select from tablename where date date and time 2020 01 12 匹配某個月份的資料 select fro...

mysql怎麼建數劇表 mysql怎麼建立資料表

建立mysql資料表需要以下資訊 1.表名 2.表欄位名 3.定義每個表字段 語法以下為建立mysql資料表的sql通用語法 create table table name column name column type 以下例子中我們將在 runoob 資料庫中建立資料表runoob tbl cr...