牛客網 sql 題目練習筆記

2021-10-09 03:18:13 字數 3434 閱讀 1916

題目:查詢各個部門當前(dept_manager.to_date='9999-01-01')領導當前(salaries.to_date='9999-01-01')薪水詳情以及其對應部門編號dept_no(注:請以salaries表為主表進行查詢,輸出結果以salaries.emp_no公升序排序,並且請注意輸出結果裡面dept_no列是最後一列)

資料表:

create table `salaries` (

`emp_no` int(11) not null, -- '員工編號',

`salary` int(11) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`from_date`));

create table `dept_manager` (

`dept_no` char(4) not null, -- '部門編號'

`emp_no` int(11) not null, --  '員工編號'

`to_date` date not null,

primary key (`emp_no`,`dept_no`));

資料:insert into salaries values(10002,5000,'9999-01-01','9999-01-01');

insert into salaries values(10006,6000,'9999-01-01','9999-01-01');

insert into salaries values(10005,7000,'9999-01-01','9999-01-01');

insert into dept_manager values('d001',10002,'9999-01-01');

insert into dept_manager values('d002',10006,'9999-01-01');

insert into dept_manager values('d003',10005,'9999-01-01');

sql:

方式1:

select s.* ,d.dept_no from salaries as s ,dept_manager as d where s.emp_no = d.emp_no and s.to_date='9999-01-01' and d.to_date='9999-01-01' order by s.emp_no asc

方式2:

select s.* ,d.dept_no from salaries as s inner join dept_manager as d where s.emp_no = d.emp_no and s.to_date='9999-01-01' and d.to_date='9999-01-01' order by s.emp_no asc

題目:查詢所有已經分配部門的員工的last_name和first_name以及dept_no(請注意輸出描述裡各個列的前後順序)

資料表:

create table `dept_emp` (

`emp_no` int(11) not null,

`dept_no` char(4) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`dept_no`));

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`));

sql:

select e.last_name,e.first_name,d.dept_no from employees as e inner join dept_emp as d where e.emp_no=d.emp_no

題目:找出所有員工當前(to_date='9999-01-01')具體的薪水salary情況,對於相同的薪水只顯示一次,並按照逆序顯示

資料表:

create table `salaries` (

`emp_no` int(11) not null,

`salary` int(11) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`from_date`));

sql:

select distinct(salary) from salaries where to_date='9999-01-01' order by salary desc

題目:獲取所有部門當前(dept_manager.to_date='9999-01-01')manager的當前(salaries.to_date='9999-01-01')薪水情況,給出dept_no, emp_no以及salary(請注意,同乙個人可能有多條薪水情況記錄)

資料表:

create table `dept_manager` (

`dept_no` char(4) not null,

`emp_no` int(11) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`dept_no`));

create table `salaries` (

`emp_no` int(11) not null,

`salary` int(11) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`from_date`));

sql:

select d.dept_no,d.emp_no,s.salary from salaries as s,dept_manager as d where s.to_date='9999-01-01' and d.to_date='9999-01-01' and d.emp_no=s.emp_no

牛客網SQL實戰練習(1)

1.查詢最晚入職員工的所有資訊 create tableemployees emp noint 11 not null,birth datedate not null,first namevarchar 14 not null,last namevarchar 16 not null,genderc...

牛客網練習30

眾所周知,小k是nowcoder的 苟管理,所以小k很擅長踢樹,雖然本題與踢樹無關 小k喜歡將日期排列成yyyy mm dd的形式 位數不足添零補齊 的形式,雖然這與小k只會做回文字串這道水題無關,但小k覺得日期組成的回文串也是挺可愛的。作為乙個涼心出題人,小k決定給你乙個可愛的問題 給你兩個日期,...

牛客網專項練習(七)

1 以下哪個資料結構不是多型資料型別 正確答案 d 棧廣義表有向圖字串分析 多型就是資料元素的型別不確定,字串的每個元素始終都是字元 char 而不會是別的型別。比如棧可以是整數棧 字元棧 物件棧等等。但是字串,它的元素必然是字元。2 以下資料結構中,是非線性資料結構 正確答案 a 你的答案 a 正...