資料查詢第二回
練習1
請寫出一個SQL,從table DEPT_AUT和DEPT_AUS,找出如result B的結果
DEPT_AUT
DEPT_AUT
| dept_code | dept_name |
| A | IT |
| B | Sales |
| C | FIN |
| D | Logistics |
DEPT_AUS
| dept_code | dept_name |
| A | IT |
| E | MFG |
| B | Sales |
Result B
| dept_code | dept_name |
| A | IT |
| B | Sales |
解答:
select dept_code , dept_name from DEPT_AUT INTERSECT select dept_code , dept_name from DEPT_AUS
練習2
請寫出一個SQL,使用MINUS語法,從table DEPT_AUT和DEPT_AUS,顯示欄位dept_code, dept_name,並將執行結果寫在result BDEPT_AUT
| dept_code | dept_name |
| A | IT |
| B | Sales |
| C | FIN |
| D | Logistics |
DEPT_AUS
| dept_code | dept_name |
| A | IT |
| E | MFG |
| B | Sales |
result B
| dept_code | dept_name |
解答:
select dept_code, dept_name from DEPT_AUT MINUS select dept_code , dept_name from DEPT_AUS
| dept_code | dept_name |
| C | FIN |
| D | Logistics |
練習3
請寫一個SQL,使用subquery 語法,從user_list中,找出所有與ITBD2部門(dept_code)的生日(birth)相同的人,顯示欄位dept_code, EMP_CNAME,birth,並按照birth欄位排序(order by)Result
| Dept_code | EMP_CNAME | Birth |
解答:
select dept_code, EMP_CNAME, birth from User_List where birth IN (select birth from User_List where dept_code = 'ITBD2') order by birth
練習4
請使用exists語法,寫出一個SQL,從table product 及 order_dtl中,列出有收到訂單的產品資訊,如result顯示的欄位及結果.
order_dtl
| order_no | product_ID | qty |
| 1 | a | 10 |
| 1 | b | 20 |
| 2 | a | 100 |
| 2 | d | 200 |
Product
| product_ID | Product_name | unit_price |
| a | LCD panel | 1000 |
| b | mouse | 50 |
| c | keyboard | 100 |
| d | ipad | 1000 |
Result
| Product_ID | Product_name | Unit_Price |
| a | LCD panel | 1000 |
| b | mouse | 50 |
| d | ipad | 1000 |
解答:
select Product_ID, Product_name, Unit_price from Product as b where EXISTS (select a.product_ID from order_dtl as a where b.Product_ID = a.Product_ID)

沒有留言:
張貼留言