牛客網練習sql題2 出錯和分析

2021-10-24 01:38:51 字數 1888 閱讀 1547

1. 題目

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

create tabledept_emp(

emp_noint(11) not null,

dept_nochar(4) not null,

from_datedate not null,

to_datedate not null,

primary key (emp_no,dept_no));

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

解答:2. 題目

查詢所有員工的last_name和first_name以及對應部門編號dept_no,也包括暫時沒有分配具體部門的員工(請注意輸出描述裡各個列的前後順序)

解答:select e.last_name,e.first_name,d.dept_no

from employees e left join dept_emp d

on e.emp_no=d.emp_no

思路:不需要剔除員工的部門號為空的情況,只需要將全集員工表作為主表,左連線即可✔。

與上一題做對比,就是看誰作為主表的區別。

總結:在工作中時,我常常對左連線、內連線等等連線方式,總是摸不著頭腦,這一題就讓我大致了解他們的使用。

左連線:以左表作為主表。

內連線:以兩表的交集作為查詢結果。

3. 題目

查詢所有員工入職時候的薪水情況,給出 emp_no 以及 salary , 並按照 emp_no 進行逆序(請注意,乙個員工可能有多次漲薪的情況)

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

create tablesalaries(

emp_noint(11) not null,

salaryint(11) not null,

from_datedate not null,

to_datedate not null,

primary key (emp_no,from_date));

解答:

牛客網練習sql題1 出錯和分析

題目1 查詢最晚入職員工的所有資訊,為了減輕入門難度,目前所有的資料裡員工入職的日期都不是同一天 sqlite裡面的注釋為 mysql為comment create tableemployees emp noint 11 not null,員工編號 birth datedate not null,f...

牛客網 程式設計題2

給定乙個陣列序列,需要求選出乙個區間,使得該區間是所有區間中經過如下計算的值最大的乙個 區間中的最小數 區間所有數的和最後程式輸出經過計算後的最大值即可,不需要輸出具體的區間。如給定序列 6 2 1 則根據上述公式,可得到所有可以選定各個區間的計算值 6 6 6 36 2 2 2 4 1 1 1 1...

牛客網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...