實驗名稱:關係查詢與連線
一、實驗目的
1、理解select語句的操作和基本使用方法,熟練掌握簡單查詢。
2、掌握select語句的巢狀使用,實現表的複雜查詢,進一步理解select語句的高階使用方法。
3、理解join語句的操作和基本使用方法,掌握內連線、外連線、自連線的概念和使用。
二、實驗環境
使用sql-server2000資料庫。
三、實驗示例
1、查詢表中所有姓劉的職工的工號,部門,薪水
select emp_no,emp_name,dept,salary
from employee
where emp_name like '劉%'
2、查詢所有定單金額高於20000的所有客戶編號
select cust_id
from sales
where tot_amt>20000
3、查詢出職稱為經理和職稱為職員的女員工的姓名、職稱、性別。
select emp_name,title,***
from employee
where title='經理'or title='職員'and ***='f'
4、選取銷售數量最多的前5條訂單訂單號、數量。
select top 5 with ties order_no,sup_id,qty
from sale_item
order by qty desc
5、按部門進行彙總,統計每個部門的總工資
select dept,sum(salary)
from employee
group by dept
6、由employee表中查詢出薪水最高的員工資訊。
select *
from employee
where salary=
(select max(salary )
from employee )
7、由sales表中查詢出訂單金額大於「e0013業務員在1996/10/15這天所接每一張訂單的金額」的所有訂單。
select *
from sales
where tot_amt>all
(select tot_amt
from sales
where sale_id='e0013'and order_date='1996/10/15')
order by tot_amt
8、 檢索product 表和sale_item表中數量大於2的相同產品的產品編號、產品名稱、數量、單價。
select
from sale_item as a inner join product as b /*如果改成left join/right join 試分析結果*/
on ( and >2
order by
9、查詢出employee表中住址相同的員工的姓名、性別、職稱、薪水、住址。
select
from employee as a inner join employee as b
on ( and (> and (
四、實驗內容與步驟
1、 查詢所有經理的姓名、職稱、薪水。
select emp_name,title,salary
from employee
where title like '經理'
2、 查詢出姓「王」並且姓名的最後乙個字為「功」的員工。
select *
from employee
where emp_name like '王%功'
3、 查詢住在上海或北京的女員工,並顯示其姓名、所屬部門、職稱、住址。
select emp_name,dept,title,addr,***
from employee
where *** ='f' and addr like'北京%'or *** ='f'and addr like '上海%'
4、 在表sales中挑出銷售金額大於等於10000元訂單。
select *
from sales
where tot_amt>=10000
5、 選取訂單金額最高的前10%的訂單資料。
select *
from sales
where order_no>=0.1
6、 查詢出職稱為經理或職稱為職員的女員工的資訊。
select *
from employee
where ***='f'and title like '經理' or ***='f' and title like '職員'
7、 刪除sales表中作廢的訂單(其發票號碼為i000000004)。
delete
from sales
where invoice_no='i000000004'
8、計算出一共銷售了幾種產品。
select sum(order_no)
from sale_item
9、顯示sale_item表中每種個別產品的訂購金額總和,並且依據銷售金額由大到小排列來顯示出每一種產品的排行榜。
select prod_id,(qty*unit_price)as sum_money
from sale_item
order by sum_money
10、計算每一產品每月的銷售金額總和,並將結果按銷售(月份,產品編號)排序。
select order_date,prod_id,sum(unit_price)as total_unit_price
from sale_item
where order_date is not null
group by prod_id,order_date
order by total_unit_price
11、由sales表中查詢出銷售金額最高的訂單。
select top 1 tot_amt,order_no
from sales
12、 由sales表中查詢出訂單金額大於「e0013業務員在1996/10/15這天所接任一張訂單的金額」的所有訂單,並顯示承接這些訂單的業務員和該條訂單的金額。
select order_no as '訂單編號',sale_id as '業務員編號',tot_amt as '訂單金額'
from sales
where (tot_amt>any(
select tot_amt from sales where order_date='1996/10/15'))
13、 找出公司女業務員所接的訂單。
select order_no as '訂單編號',sale_id as '業務員編號',tot_amt as '訂單金額'
from sales
where (sale_id = any (
select emp_no from employee where *** ='f'))
14、 找出公司中姓名相同的員工,並且依據員工編號排序相識這些員工資訊。
select*
from employee as a inner join employee as b
on( and(
order by
15、 找出目前業績未超過200000元的員工。
select * from employee
where emp_no in (select sale_id from sales where tot_amt<20000)
16、 計算公司內各個部門的工資支出總和。
select dept,sum(salary)as sum_salary
from employee
group by dept
17、 計算每一產品銷售數量總和與平均銷售單價。
select order_no,prod_name,sum(qty)as sum_qty,**g(unit_price)as **g_unit_price
from sale_item,product
group by order_no,prod_name
18、 查詢出employee表中部門相同且住址相同的女員工的姓名、性別、職稱、薪水、住址。
select
from employee as a inner join employee
as b on ( and ( (
where ( like 'f') and ( like 'f')
19、 檢索product 表和sale_item表中相同產品的產品編號、產品名稱、數量、單價。
select
from sale_item as a inner join product as b
on(order by
20、 檢索product 表和sale_item表中單價高於2400元的相同產品的產品編號、產品名稱、數量、單價。
select
from sale_item as a inner join product as b
on( >2400
order by
五、實驗報告
通過對本次實驗的學習,我了解了在資料庫中對錶的巢狀查詢,也更深一步的了解了select語句的用法,以及如何在sql語句中實現巢狀查詢,使得很多查詢變得更加容易,相信在以後資料庫方面,能為我提供很大的幫助。
資料庫實驗
淮海工學院計算機工程學院 實驗報告書 課程名 資料庫原理及應用 題目資料操縱 班級g計算機131班 學號2013150233 姓名顧建雲 一 目的與要求 1 鞏固資料庫的基礎知識。2 掌握資料操縱的各種方法。二 實驗內容 1 利用企業管理器向職工表和職工工資表中輸入教材p117中的資料 2 用sql...
資料庫實驗
實驗二 sql server資料庫更新操作 1.為companyinfo資料庫的各表錄入記錄 sql語言 insert category values 1,飲料 軟飲料 咖啡 茶 啤酒和淡啤酒 insert category values 2,計算機耗材 列印紙等 insert category v...
資料庫實驗實驗六
資料庫原理與應用 實驗報告 學號 13521230 姓名黃玉歡 日期 11月13日 成績1.實驗名稱 2.實驗目的 1.熟悉使用update insert delete語句進行表操作 2.能將這些更新操作應用於實際操作中去 3.實驗要求 1 完成下面的實驗內容,並提交實驗報告 2 在實驗報告中附上相...