1. 程式人生 > >資料庫練習二:查詢語句練習

資料庫練習二:查詢語句練習

先進行sql語句的練習,然後再根據語句進行優化以及建立索引。


# 1檢視僱員編號、名字和部門
select e.emp_no,e.last_name,de.dept_name from 
    employees as e,dept_emp as emp,departments as de where
    e.emp_no = emp.emp_no and de.dept_no=emp.dept_no ;


# 2顯示所有僱員的編號、姓名、工資
select e.emp_no,concat(e.first_name,' ', e.last_name) `name`
,sa.salary from employees as e,salaries as sa where e.emp_no=sa.emp_no limit 100;
# 3查詢在d005號部門工作的僱員 select e.emp_no,concat(e.first_name,' ', e.last_name) `name`,de.dept_name from employees as e,dept_emp as emp,departments as de where de.dept_no='d005' and de.dept_no=emp.dept_no and e.emp_no=emp.emp_no;
# 4要求查詢職位為Engineer和Senior Engineer的僱員姓名(last_name) select e.last_name from employees as e ,titles as ti where e.emp_no=ti.emp_no and (ti.title='Engineer' or ti.title='Senior Engineer') ; select e.last_name from employees as e ,titles as ti where e.emp_no=ti.emp_no and ti.title in
('Engineer','Senior Engineer') ;
# 5查詢職位不是Engineer和Senior Engineer的部門編號,僱員部門及姓名。將姓名顯示為(first_name+last_name命名為”Name”) select concat(e.first_name,' ',last_name) `name`,de.dept_no,de.dept_name from employees as e ,dept_emp as emp , departments as de , titles as ti where e.emp_no = ti.emp_no and e.emp_no = emp.emp_no and emp.dept_no=de.dept_no and ti.title != 'Engineer' and ti.title != 'Senior Engineer' ; ## 會發現資料有重複,那是因為在員工職稱表(title)裡,有些員工是兼顧多個職稱的。 # 6查詢哪些僱員的工資在60000到90000之間 select concat(e.first_name,' ',last_name) `name`,salary from employees as e,salaries where e.emp_no = salaries.emp_no and salaries.salary between 60000 and 90000; # 7查詢哪些僱員的工資不在60000到90000之間 select concat(e.first_name,' ',last_name) `name`,salary from employees as e,salaries where e.emp_no = salaries.emp_no and salaries.salary not between 60000 and 90000; # 8查詢first_name以P開頭,後面僅有四個字母的僱員資訊 select distinct concat(e.first_name,' ',last_name) `name` from employees as e where employees.first_name like "P____"; # 9查詢last_name以K開頭的僱員資訊 select concat(e.first_name,' ',last_name) `name` from employees as e where e.last_name like "K%"; # 10查詢名字以字母K開頭,以i結尾,並且第三個字母為o的僱員名字(First_name)、職位和所在部門號 select concat(e.first_name,' ',last_name) `name` , emp.dept_no,title from employees as e,dept_emp as emp,titles where e.emp_no = emp.emp_no and e.emp_no = titles.emp_no and e.first_name like 'k_o%i'; # 11查詢哪些僱員的職位名不以Se開頭 select concat(e.first_name,' ',last_name) `name` , title from employees as e , titles where e.emp_no = titles.emp_no and titles.title not like 'Se%'; # 12查詢d005號部門裡不是Staff的僱員資訊 select concat(e.first_name,' ',last_name) `name` , e.emp_no,title from employees as e ,titles as ti , dept_emp as emp where emp.dept_no = 'd005' and e.emp_no=emp.emp_no and e.emp_no = ti.emp_no and ti.title !='Staff'; # 13查詢d005號部門工資大於100000的員工的資訊 select concat(e.first_name,' ',last_name) `name` , e.emp_no,salary from employees as e ,salaries , dept_emp as emp where e.emp_no = emp.emp_no and emp.dept_no='d005'and e.emp_no = salaries.emp_no and salary >100000; # 14按字母順序顯示僱員的名字(last_name) select last_name from employees order by last_name; # 15按部門編號降序顯示僱員資訊 select concat(e.first_name,' ',last_name) `name` , dept_no from employees as e ,dept_emp as emp where e.emp_no = emp.emp_no order by emp.dept_no desc ; # 16計算每個部門的平均工資和工資總和.(顯示平均值,總值,部門編號,部門名稱 ) select avg(salary) , count(salary) ,emp.dept_no,de.dept_name from employees as e ,salaries as sa ,dept_emp as emp,departments as de where e.emp_no=sa.emp_no and emp.emp_no=e.emp_no and emp.dept_no =de.dept_no group by dept_name ,dept_no order by dept_no; # 17請算出employees表中每位僱員的平均工資 select first_name,last_name,avg(salary) salary from employees as e , salaries as sa where e.emp_no=sa.emp_no group by first_name,last_name order by salary ; # 18請算出employees表中所有僱員的平均工資 select avg(salary) from employees as e , salaries as sa where e.emp_no=sa.emp_no ; # 19請查詢出employees表中的最低工資的員工資訊 ## 典型的慢查詢 select concat(e.first_name,' ',last_name) `name` ,salary from employees as e , salaries as sa where e.emp_no=sa.emp_no order by salary limit 1; ## 正確姿勢 select concat(e.first_name,' ',last_name) `name` ,salary from employees as e , salaries as sa where e.emp_no=sa.emp_no and sa.salary= (select min(salary) from salaries); # 20請計算出每個部門的平均工資、最高工資和最低工資 select avg(salary) , count(salary) ,max(salary),min(salary),emp.dept_no,de.dept_name from employees as e ,salaries as sa ,dept_emp as emp,departments as de where e.emp_no=sa.emp_no and emp.emp_no=e.emp_no and emp.dept_no =de.dept_no group by dept_name ,dept_no order by dept_no; # 21查詢薪水發放時間在1986-06-26 ~ 1987-06-25薪水高於46135號僱員並且工種與他相同的僱員情況。 select e.* ,sa.from_date from employees as e ,salaries as sa ,titles as ti where ti.title in (select title from titles where emp_no=46135) and sa.from_date>'1986-06-26' and sa.to_date < '1987-06-25' and e.emp_no=sa.emp_no and e.emp_no = ti.emp_no and sa.salary > ( select salary from salaries as sa where sa.from_date>'1986-06-26' and sa.to_date< '1987-06-25' and emp_no=46135); # 22查詢工資在10000到50000之間的僱員所在部門的所有人員的資訊。 select e.*,de.dept_no,de.dept_no,salary from employees as e ,dept_emp as emp,departments as de ,salaries as sa where sa.salary in (select salary from salaries where salary between 10000 and 50000 ) and e.emp_no=emp.emp_no and emp.dept_no = de.dept_no and sa.emp_no =e.emp_no order by salary; # 23查詢出在薪水發放時間在1986-06-26 ~ 1987-06-25的員工資訊 ###(工號,姓名,性別,薪水,職位) select e.emp_no , concat(e.first_name,' ',last_name) `name`, e.gender,salary,title from employees as e , salaries as sa , titles as ti where e.emp_no = sa.emp_no and e.emp_no = ti.emp_no and sa.from_date > '1986-06-26' and sa.to_date < '1987-06-25';