學習python第四天——Oracle查詢
阿新 • • 發佈:2019-02-24
進行 遍布 其它 們的 解決 同時 派生 sele n-1
3.子查詢(難):
當進行查詢的時候,發現需要的數據信息不明確,需要先通過另一個查詢得到,
此查詢稱為子查詢;
執行順序:先執行子查詢得到結果以後返回給主查詢
組成部分:
1).主查詢部分
2).子查詢部分
【註意事項】:
子查詢一定需要被定義/包裹在小括號內部,可以認為是顯示的提升了代碼執行的優先級
需求1:
查詢薪資比Abel的高的有誰?
分析:
①.先查詢出Abel的薪資是多少?
②.將過濾條件定義為>①,然後進行查詢得到最終需要的結果
代碼實現:
select last_name,salary
from employees
where salary > (
select salary from employees
where last_name = ‘Abel‘
);
需求2:
查詢job_id與141號員工相同,salary比143號員工多的員工的姓名,job_id和salary?
代碼實現:
select last_name,job_id,salary
from employees
where job_id = (
select job_id
from employees
where employee_id = 141
)
and salary > (
select salary
from employees
where employee_id = 143
);
課堂練習:
1).返回公司工資最少的員工的employee_id,job_id和salary
select employee_id,job_id,salary
from employees
where salary = (
select min(salary)
from employees
);
2).查詢平均工資高於公司平均工資的部門有哪些
select department_id,avg(salary)
from employees
group by department_id
having avg(salary) > (
select avg(salary)
from employees
)
order by department_id desc;
3).查詢最低工資大於20號部門最低工資的部門id和最低工資
select department_id,min(salary)
from employees
where department_id is not null
group by department_id
having min(salary) > (
select min(salary)
from employees
having department_id = 20
);
4).返回其它職位中比job_id為‘IT_PROG‘中最低工資低的員工的員工號,姓名,job_id以及salary
select employee_id,last_name,job_id,salary
from employees
where salary < (
select min(salary)
from employees
where job_id = ‘IT_PROG‘
);
4.多表查詢/多表聯查
概念:
使用場景,如果一條select語句中需要查詢的列遍布多張數據表,
那麽我們就必須使用多表查詢了!!
分類:
等值連接和非等值連接
對於等值連接分方向:
1).內連接:返回多張表中共同滿足的數據,取交集
2).外連接(左、右、滿):返回內連接數據的同時還會繼續返回某張表中不匹配的一些記錄數
3).自連接:從始至終都是一張表,模擬一張表派生為兩張(它們的結構式一模一樣的),自己連自己
等值連接中的內連接:
需求:
查詢所有員工的員工號、員工姓名以及部門的名字?
select employee_id,last_name,department_name
from employees,departments;
【註意】
以上查詢得到了2889條記錄,很多都是沒有用的數據(臟數據),
出現的原因是:沒有添加有效的連接條件導致的,
而這種現象我們稱為笛卡爾集現象;
我們日後的學習和開發環境中是絕對要避免的!!
如何保證我們之後的多表查詢絕對不會出現笛卡爾集現象?
1).不能不寫連接條件
2).連接條件必須是有效的
思考:如何修改上述的代碼?
代碼實現如下:
select employee_id,last_name,department_name
from employees,departments
where employees.department_id = departments.department_id;
需求:使用內連接來實現
查詢員工的員工號、姓名、部門號、部門名字?
select employee_id,last_name,department_id,department_name
from employees,departments
where employees.department_id = departments.department_id;
以上代碼出錯了,出錯原因:
因為對於department_id這個列在employees和departments兩張表中都存在,
所以需要顯示的告訴編譯器,我從哪張表中獲取數據內容的!
修改代碼如下:
select employee_id,last_name,departments.department_id,department_name
from employees,departments
where employees.department_id = departments.department_id;
select employee_id,last_name,employees.department_id,department_name
from employees,departments
where employees.department_id = departments.department_id;
思考:沒有重復的列可以使用名字.的形式來定義嗎?---> 可以的
select employee.employee_id,employee.last_name,employees.department_id,departments.department_name
from employees,departments
where employees.department_id = departments.department_id;
上述代碼運行以及結果方面不存在問題,但是在代碼量上比較冗余!!我們可以使用如下的方式解決...
給名字起別名的方式:
修改代碼如下:
select e.employee_id,e.last_name,e.department_id,d.department_name
from employees e,departments d
where e.department_id = d.department_id;
總結:對於多表查詢,如果涉及n張表,至少需要有n-1個連接條件;
非等值連接:
需求:
查詢員工的姓名、薪資以及薪資的等級
select last_name,salary,grade_level
from employees,job_grades
where salary between lowest_sal and highest_sal;
以上代碼有問題,可以看到各個人的薪資等級,但是由於沒有追加連接連接,還是出現了笛卡爾集現象;
我們需要慎用!一般之後非等值連接用的比較少,而且必須配合等值連接一起用;
學習python第四天——Oracle查詢