SQL【121-135】

10-122 A3-2查询国家为Mexico、Germany的客户数量

查询出顾客表(customers)中查询国家(Country)MexicoGermany的客户数量,并重命名为custCount

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
CustomerIDvarchar5顾客编号
CompanyNamevarchar40公司名称
ContactNamevarchar30联系姓名
ContactTitlevarchar30联系头衔
Addressvarchar60地址
Cityvarchar15城市
Regionvarchar15区域
PostalCodevarchar10邮政编码
Countryvarchar15国家
Phonevarchar24电话
Faxvarchar24传真

表样例

customers表:

CustomerIDCompanyNameContactNameContactTitleAddressCityRegionPostalCodeCountryPhoneFax
ALFKIAlfreds FutterkisteMaria AndersSales RepresentativeObere Str. 57Berlin12209Germany030-0074321030-0076545
ANATRAna Trujillo Emparedados y heladosAna TrujilloOwnerAvda. de la Constitucin 2222Mxico D.F.05021Mexico(5) 555-4729(5) 555-3745
ANTONAntonio Moreno TaqueraAntonio MorenoOwnerMataderos 2312Mxico D.F.05023Mexico(5) 555-3932
AROUTAround the HornThomas HardySales Representative120 Hanover Sq.LondonWA1 1DPUK(171) 555-7788(171) 555-6750
BERGSBerglunds snabbkpChristina BerglundOrder AdministratorBerguvsvgen 8LuleS-958 22Sweden0921-12 34 650921-12 34 67
BLAUSBlauer See DelikatessenHanna MoosSales RepresentativeForsterstr. 57Mannheim68306Germany0621-084600621-08924
BLONPBlondesddsl pre et filsFrdrique CiteauxMarketing Manager24, place KlberStrasbourg67000France88.60.15.3188.60.15.32
BOLIDBlido Comidas preparadasMartn SommerOwnerC/ Araquil, 67Madrid28023Spain(91) 555 22 82(91) 555 91 99
BONAPBon app'Laurence LebihanOwner12, rue des BouchersMarseille13008France91.24.45.4091.24.45.41
BOTTMBottom-Dollar MarketsElizabeth LincolnAccounting Manager23 Tsawassen Blvd.TsawassenBCT2F 8M4Canada(604) 555-4729(604) 555-3745

输出样例:

custCount
4
select count(*) custCount from customers where Country in ('Mexico','Germany')

10-123 A3-3查找产品表中最低的单价

产品表(products)中查询最低的单价(UnitPrice),并重命名为minUnitPrice

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
ProductIDint11产品编号
ProductNamevarchar40产品名称
SupplierIDint11供应商编号
CategoryIDint11种类编号
QuantityPerUnitvarchar20数量
UnitPricedecimal10,4单价
UnitsInStocksmallint2库存数量
UnitsOnOrdersmallint2订购数量
ReorderLevelsmallint2再次订购量
Discontinuedbit1中止

表样例

products表:

ProductIDProductNameSupplierIDCategoryIDQuantityPerUnitUnitPriceUnitsInStockUnitsOnOrderReorderLevelDiscontinued
1Chai1110 boxes x 20 bags1839010
2Chang1124 - 12 oz bottles19174025
3Aniseed Syrup1212 - 550 ml bottles10137025
4Chef Anton's Cajun Seasoning2248 - 6 oz jars225300
5Chef Anton's Gumbo Mix2236 boxes21.35000
6Grandma's Boysenberry Spread3212 - 8 oz jars25120025
7Uncle Bob's Organic Dried Pears3712 - 1 lb pkgs.3015010
8Northwoods Cranberry Sauce3212 - 12 oz jars40600
9Mishi Kobe Niku4618 - 500 g pkgs.972900
10Ikura4812 - 200 ml jars313100

输出样例:

minUnitPrice
10
select min(UnitPrice) minUnitPrice from products

10-124 A3-4查询产品表中最大库存量

产品表(products)中查询最多的库存数量(UnitsInStock),并重命名为maxUnitsInStock

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
ProductIDint11产品编号
ProductNamevarchar40产品名称
SupplierIDint11供应商编号
CategoryIDint11种类编号
QuantityPerUnitvarchar20数量
UnitPricedecimal10,4单价
UnitsInStocksmallint2库存数量
UnitsOnOrdersmallint2订购数量
ReorderLevelsmallint2再次订购量
Discontinuedbit1中止

表样例

products表:

ProductIDProductNameSupplierIDCategoryIDQuantityPerUnitUnitPriceUnitsInStockUnitsOnOrderReorderLevelDiscontinued
1Chai1110 boxes x 20 bags1839010
2Chang1124 - 12 oz bottles19174025
3Aniseed Syrup1212 - 550 ml bottles10137025
4Chef Anton's Cajun Seasoning2248 - 6 oz jars225300
5Chef Anton's Gumbo Mix2236 boxes21.35000
6Grandma's Boysenberry Spread3212 - 8 oz jars25120025
7Uncle Bob's Organic Dried Pears3712 - 1 lb pkgs.3015010
8Northwoods Cranberry Sauce3212 - 12 oz jars40600
9Mishi Kobe Niku4618 - 500 g pkgs.972900
10Ikura4812 - 200 ml jars313100

输出样例:

maxUnitsInStock
120
select max(UnitsInStock) maxUnitsInStock from products

10-125 A4-1查找订单表中每位顾客的平均运费

查询订单表(orders)中每位顾客的平均运费,结果显示为顾客编号(CustomerID)平均运费(avgFreight)

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
OrderIDint11订单编号
CustomerIDvarchar5顾客编号
EmployeeIDint11员工编号
OrderDatedatetime,订购日期
RequiredDatedatetime,预计到达日期
ShippedDatedatetime,发货日期
ShipViaint11运货商
Freightdecimal10,4运费
ShipNamevarchar40货主姓名
ShipAddressvarchar60货主地址
ShipCityvarchar15所在城市
ShipRegionvarchar15区域
ShipPostalCodevarchar10邮政编码
ShipCountryvarchar15所在国家

表样例

请在这里给出上述表结构对应的表样例。例如

orders表:

OrderIDCustomerIDEmployeeIDOrderDateRequiredDateShippedDateShipViaFreightShipNameShipAddressShipCityShipRegionShipPostalCodeShipCountry
10248VINET51996/7/4 00:00:001996/8/1 00:00:001996/7/16 00:00:00332.3800Vins et alcools Chevalier59 rue de l-AbbayeReims51100France
10249TOMSP61996/7/5 00:00:001996/8/16 00:00:001996/7/10 00:00:00111.6100Toms SpezialittenLuisenstr. 48Mnster44087Germany
10250HANAR41996/7/8 00:00:001996/8/5 00:00:001996/7/12 00:00:00265.8300Hanari CarnesRua do Pao, 67Rio de JaneiroRJ05454-876Brazil
10251VICTE31996/7/8 00:00:001996/8/5 00:00:001996/7/15 00:00:00141.3400Victuailles en stock2, rue du CommerceLyon69004France
10252SUPRD41996/7/9 00:00:001996/8/6 00:00:001996/7/11 00:00:00251.3000Suprmes dlicesBoulevard Tirou, 255CharleroiB-6000Belgium
10253HANAR31996/7/10 00:00:001996/7/24 00:00:001996/7/16 00:00:00258.1700Hanari CarnesRua do Pao, 67Rio de JaneiroRJ05454-876Brazil
10254CHOPS51996/7/11 00:00:001996/8/8 00:00:001996/7/23 00:00:00222.9800Chop-suey ChineseHauptstr. 31Bern3012Switzerland
10255RICSU91996/7/12 00:00:001996/8/9 00:00:001996/7/15 00:00:003148.3300Richter SupermarktStarenweg 5Genve1204Switzerland
10256WELLI31996/7/15 00:00:001996/8/12 00:00:001996/7/17 00:00:00213.9700Wellington ImportadoraRua do Mercado, 12ResendeSP08737-363Brazil
10257HILAA41996/7/16 00:00:001996/8/13 00:00:001996/7/22 00:00:00381.9100HILARION-AbastosCarrera 22 con Ave. Carlos Soublette #8-35San CristbalTchira5022Venezuela

输出样例:

CustomerIDavgFreight
CHOPS22.98000000
HANAR62.00000000
HILAA81.91000000
RICSU148.33000000
SUPRD51.30000000
TOMSP11.61000000
VICTE41.34000000
VINET32.38000000
WELLI13.97000000

select CustomerID,avg(Freight) avgFreight from orders group by CustomerID	

10-126 A4-2统计顾客表中每个国家的顾客数量

查询出顾客表(customers)中查询每个国家的客户数量,显示为国家(Country)客户数量(custCount)

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
CustomerIDvarchar5顾客编号
CompanyNamevarchar40公司名称
ContactNamevarchar30联系姓名
ContactTitlevarchar30联系头衔
Addressvarchar60地址
Cityvarchar15城市
Regionvarchar15区域
PostalCodevarchar10邮政编码
Countryvarchar15国家
Phonevarchar24电话
Faxvarchar24传真

表样例

customers表:

CustomerIDCompanyNameContactNameContactTitleAddressCityRegionPostalCodeCountryPhoneFax
ALFKIAlfreds FutterkisteMaria AndersSales RepresentativeObere Str. 57Berlin12209Germany030-0074321030-0076545
ANATRAna Trujillo Emparedados y heladosAna TrujilloOwnerAvda. de la Constitucin 2222Mxico D.F.05021Mexico(5) 555-4729(5) 555-3745
ANTONAntonio Moreno TaqueraAntonio MorenoOwnerMataderos 2312Mxico D.F.05023Mexico(5) 555-3932
AROUTAround the HornThomas HardySales Representative120 Hanover Sq.LondonWA1 1DPUK(171) 555-7788(171) 555-6750
BERGSBerglunds snabbkpChristina BerglundOrder AdministratorBerguvsvgen 8LuleS-958 22Sweden0921-12 34 650921-12 34 67
BLAUSBlauer See DelikatessenHanna MoosSales RepresentativeForsterstr. 57Mannheim68306Germany0621-084600621-08924
BLONPBlondesddsl pre et filsFrdrique CiteauxMarketing Manager24, place KlberStrasbourg67000France88.60.15.3188.60.15.32
BOLIDBlido Comidas preparadasMartn SommerOwnerC/ Araquil, 67Madrid28023Spain(91) 555 22 82(91) 555 91 99
BONAPBon app'Laurence LebihanOwner12, rue des BouchersMarseille13008France91.24.45.4091.24.45.41
BOTTMBottom-Dollar MarketsElizabeth LincolnAccounting Manager23 Tsawassen Blvd.TsawassenBCT2F 8M4Canada(604) 555-4729(604) 555-3745

输出样例:

CountrycustCount
Canada1
France2
Germany2
Mexico2
Spain1
Sweden1
UK1

select Country,count(*) custCount from customers group by Country

10-127 A4-3在订单表中查找特定国家且平均运费不小于10的信息

查询订单表(orders)中查找所在国家(ShipCountry)BelgiumSwitzerland,且平均运费不小于10的的顾客及运费信息,结果显示为顾客编号(CustomerID)平均运费(avgFreight)

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
OrderIDint11订单编号
CustomerIDvarchar5顾客编号
EmployeeIDint11员工编号
OrderDatedatetime,订购日期
RequiredDatedatetime,预计到达日期
ShippedDatedatetime,发货日期
ShipViaint11运货商
Freightdecimal10,4运费
ShipNamevarchar40货主姓名
ShipAddressvarchar60货主地址
ShipCityvarchar15所在城市
ShipRegionvarchar15区域
ShipPostalCodevarchar10邮政编码
ShipCountryvarchar15所在国家

表样例

请在这里给出上述表结构对应的表样例。例如

orders表:

OrderIDCustomerIDEmployeeIDOrderDateRequiredDateShippedDateShipViaFreightShipNameShipAddressShipCityShipRegionShipPostalCodeShipCountry
10248VINET51996/7/4 00:00:001996/8/1 00:00:001996/7/16 00:00:00332.3800Vins et alcools Chevalier59 rue de l-AbbayeReims51100France
10249TOMSP61996/7/5 00:00:001996/8/16 00:00:001996/7/10 00:00:00111.6100Toms SpezialittenLuisenstr. 48Mnster44087Germany
10250HANAR41996/7/8 00:00:001996/8/5 00:00:001996/7/12 00:00:00265.8300Hanari CarnesRua do Pao, 67Rio de JaneiroRJ05454-876Brazil
10251VICTE31996/7/8 00:00:001996/8/5 00:00:001996/7/15 00:00:00141.3400Victuailles en stock2, rue du CommerceLyon69004France
10252SUPRD41996/7/9 00:00:001996/8/6 00:00:001996/7/11 00:00:00251.3000Suprmes dlicesBoulevard Tirou, 255CharleroiB-6000Belgium
10253HANAR31996/7/10 00:00:001996/7/24 00:00:001996/7/16 00:00:00258.1700Hanari CarnesRua do Pao, 67Rio de JaneiroRJ05454-876Brazil
10254CHOPS51996/7/11 00:00:001996/8/8 00:00:001996/7/23 00:00:00222.9800Chop-suey ChineseHauptstr. 31Bern3012Switzerland
10255RICSU91996/7/12 00:00:001996/8/9 00:00:001996/7/15 00:00:003148.3300Richter SupermarktStarenweg 5Genve1204Switzerland
10256WELLI31996/7/15 00:00:001996/8/12 00:00:001996/7/17 00:00:00213.9700Wellington ImportadoraRua do Mercado, 12ResendeSP08737-363Brazil
10257HILAA41996/7/16 00:00:001996/8/13 00:00:001996/7/22 00:00:00381.9100HILARION-AbastosCarrera 22 con Ave. Carlos Soublette #8-35San CristbalTchira5022Venezuela

输出样例:

|CustomerID|avgFreight|
|CHOPS|22.98000000|
|RICSU|148.33000000|
|SUPRD|51.30000000|

select CustomerID,avg(Freight) as avgFreight from orders
where ShipCountry in('Belgium','Switzerland')
group by CustomerID
having avg(Freight)>=10;

10-128 A4-4查找产品表中平均订购数大于特定值的产品信息

产品表(products)中查找平均订购数量大于15的产品信息,显示为:产品编号(ProductID),和总订货数量(重命名为sumUnitsOnOrder

提示:请使用SELECT语句作答。

表结构:

列名数据类型长度主码说明
ProductIDint11产品编号
ProductNamevarchar40产品名称
SupplierIDint11供应商编号
CategoryIDint11种类编号
QuantityPerUnitvarchar20数量
UnitPricedecimal10,4单价
UnitsInStocksmallint2库存数量
UnitsOnOrdersmallint2订购数量
ReorderLevelsmallint2再次订购量
Discontinuedbit1中止

表样例

products表:

ProductIDProductNameSupplierIDCategoryIDQuantityPerUnitUnitPriceUnitsInStockUnitsOnOrderReorderLevelDiscontinued
1Chai1110 boxes x 20 bags1839010
2Chang1124 - 12 oz bottles19174025
3Aniseed Syrup1212 - 550 ml bottles10137025
4Chef Anton's Cajun Seasoning2248 - 6 oz jars225300
5Chef Anton's Gumbo Mix2236 boxes21.35000
6Grandma's Boysenberry Spread3212 - 8 oz jars25120025
7Uncle Bob's Organic Dried Pears3712 - 1 lb pkgs.3015010
8Northwoods Cranberry Sauce3212 - 12 oz jars40600
9Mishi Kobe Niku4618 - 500 g pkgs.972900
10Ikura4812 - 200 ml jars313100

输出样例:

ProductIDsumUnitsOnOrder
240
370
select 
    ProductID,SUM(UnitsOnOrder) AS sumUnitsOnOrder
from 
    products
group by 
    ProductID
having 
    AVG(UnitsOnOrder) > 15;

10-129 4-1 查询速度至少为160MHz的PC的制造商

本题目要求编写SQL语句,
查询速度至少为160MHz的PC的制造商。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE product
( maker CHAR(20) ,          --制造商
  model CHAR(20) NOT NULL,  --产品型号
  type CHAR(20),            --产品类型
  PRIMARY KEY(model)
);
CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);

表样例

product表:

makermodeltype
D2003便携式电脑
D3001打印机
B3002打印机
A1001个人电脑
B1006个人电脑
D1008个人电脑
D1009个人电脑

pc表:

modelspeedramhdcdprice
1001133.00161.606X1595
1006200.00323.108X2099
1008180.00322.008X3699
1009200.00322.508X2599

输出样例:

maker
B
D

select distinct maker from product join pc on product.model=pc.model where pc.speed>=160

10-130 4-2 查询价格最高的打印机型号

本题目要求编写SQL语句,
查询价格最高的打印机型号。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE printer
( model CHAR(20) NOT NULL,    --型号
  color BIT,                  --是否彩色
  type CHAR(10),              --类型
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);

表样例

printer表:

modelcolortypeprice
30011喷墨275
30021喷墨269
30030激光829
30040激光879

输出样例:

model
3004
select model from printer where price in (select max(price) from printer)

10-131 4-3 查询速度低于任何PC的便携式电脑

本题目要求编写SQL语句,
查询速度低于任何PC的便携式电脑。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model) 
);
CREATE TABLE laptop
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度 
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  screen DECIMAL(6,2),        --屏幕大小
  price INT,                  --价钱
  PRIMARY KEY(model)
);

表样例

pc表:

modelspeedramhdcdprice
1001133.00161.606X1595
1002120.00161.606X1399
1003166.00242.506X1899
1004166.00322.508X1999
1008180.00322.008X3699
1009200.00322.508X2599

laptop表:

modelspeedramhdscreenprice
2001100.00201.109.501999
2002117.00120.7511.302499
2004133.00161.1011.203499

输出样例:

model
2001
2002
select model from laptop where speed<(select min(speed) from pc)

10-132 4-4 查询具有最高价格的机器的型号,机器包括PC、Laptop、Printer

本题目要求编写SQL语句,查询具有最高价格的机器的型号,机器包括PC、Laptop、Printer。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model) 
);
CREATE TABLE laptop
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度 
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  screen DECIMAL(6,2),        --屏幕大小
  price INT,                  --价钱
  PRIMARY KEY(model)
);
CREATE TABLE printer
( model CHAR(20) NOT NULL,    --型号
  color BIT,                  --是否彩色
  type CHAR(10),              --类型
  price INT,                  --价钱
  PRIMARY KEY(model)
);

表样例

pc表:

modelspeedramhdcdprice
1001133.00161.606X1595
1002120.00161.606X1399
1003166.00242.506X1899
1004166.00322.508X1999
1008180.00322.008X3699
1009200.00322.508X2599

laptop表:

modelspeedramhdscreenprice
2001100.00201.109.501999
2002117.00120.7511.302499
2003117.00321.0011.203599

printer表:

modelcolortypeprice
30011喷墨275
30021喷墨269
30030激光829
30040激光879

输出样例:

model
1008
select a.model
from (select model,price price from pc 
     union
     select model,price from laptop
     union
     select model,price from printer) a
     where a.price=(select max(price) from (select model,price from pc 
     union
     select model,price from laptop
     union
     select model,price from printer) b)
    

10-133 4-5 查询具有最低价格的的彩色打印机的制造商

本题目要求编写SQL语句,
查询具有最低价格的的彩色打印机的制造商。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE product
( maker CHAR(20) ,          --制造商
  model CHAR(20) NOT NULL,  --产品型号
  type CHAR(20),            --产品类型
  PRIMARY KEY(model)
);
CREATE TABLE printer
( model CHAR(20) NOT NULL,    --型号
  color BIT,                  --是否彩色
  type CHAR(10),              --类型
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);

表样例

product表:

makermodeltype
D2003便携式电脑
B1006个人电脑
D3001打印机
B3002打印机
D3003打印机
B3004打印机

printer表:

modelcolortypeprice
30011喷墨275
30021喷墨369
30030激光829
30040激光879

输出样例:

maker
D


select a.maker from (select maker,price from product
join printer on printer.model=product.model
where printer.color=1
) a
where a.price =(select min(price) from
(select maker,price from product
join printer on printer.model=product.model
where printer.color=1
) a)

10-134 4-6 查询在具有最小内存容量的所有PC中具有最快处理器的PC制造商

本题目要求编写SQL语句,
查询在具有最小内存容量的所有PC中具有最快处理器的PC制造商。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE product
( maker CHAR(20) ,          --制造商
  model CHAR(20) NOT NULL,  --产品型号
  type CHAR(20),            --产品类型
  PRIMARY KEY(model)
);
CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);

表样例

product表:

makermodeltype
D2003便携式电脑
D3001打印机
A1001个人电脑
A1002个人电脑
A1003个人电脑
B1004个人电脑

pc表:

modelspeedramhdcdprice
1001133.00161.606X1595
1002120.00161.606X1399
1003166.00242.506X1899
1004166.00322.508X1999

输出样例:

maker
A

select maker
    from pc,product
    where pc.model = product.model
    and ram = (select min(ram) from pc)
    and speed = (select max(speed) from pc where ram = (select min(ram) from pc));


10-135 5-1 查询销售便携式电脑但不销售PC的厂商

本题目要求编写SQL语句,
查询销售便携式电脑但不销售PC的厂商。

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE product
( maker CHAR(20) ,          --制造商
  model CHAR(20) NOT NULL,  --产品型号
  type CHAR(20),            --产品类型
  PRIMARY KEY(model)
);
CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);
CREATE TABLE laptop
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度 
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  screen DECIMAL(6,2),        --屏幕大小
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model) 
);

表样例

product表:

makermodeltype
D2001便携式电脑
D2002便携式电脑
E2004便携式电脑
D3001打印机
B3002打印机
A1001个人电脑
B1004个人电脑
D1008个人电脑

pc表:

modelspeedramhdcdprice
1001133.00161.606X1595
1004166.00322.508X1999
1008180.00322.008X3699

laptop表:

modelspeedramhdscreenprice
2001100.00201.109.501999
2002117.00120.7511.302499
2004133.00161.1011.203499

输出样例:

maker
E


select distinct maker
       from laptop,product
       where laptop.model = product.model
       and product.maker not in (select maker
                                        from pc,product
                                        where pc.model = product.model);

10-136 5-2 查询至少生产两种不同的计算机(PC或便携式电脑)且机器速度至少为133的厂商

本题目要求编写SQL语句,
查询至少生产两种不同型号的计算机(PC便携式电脑)且机器速度至少为133的厂商,输出结果按照制造商升序排列。

提示:

本题有三种情况:
(1) 至少生产两种不同PC的厂商
(2) 至少生产两种不同便携式电脑的厂商
(3) 至少生产一种PC且至少生产一种便携式电脑的厂商。

表结构:

CREATE TABLE product
( maker CHAR(20) ,          --制造商
  model CHAR(20) NOT NULL,  --产品型号
  type CHAR(20),            --产品类型
  PRIMARY KEY(model)
);
CREATE TABLE pc
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  cd CHAR(4),                 --光驱
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model)
);
CREATE TABLE laptop
( model CHAR(20) NOT NULL,    --型号
  speed  DECIMAL(6,2),        --速度 
  ram  INT,                   --内存
  hd DECIMAL(6,2),            --硬盘容量
  screen DECIMAL(6,2),        --屏幕大小
  price INT,                  --价钱
  PRIMARY KEY(model),
  FOREIGN KEY(model) REFERENCES product(model) 
);

表样例

product表:

makermodeltype
D2001便携式电脑
D2002便携式电脑
E2004便携式电脑
D3001打印机
B3002打印机
A1001个人电脑
A1002个人电脑
A1003个人电脑
D1008个人电脑

pc表:

modelspeedramhdcdprice
1001133.00161.606X1595
1002120.00161.606X1399
1003166.00322.508X1999
1008180.00322.008X3699

laptop表:

modelspeedramhdscreenprice
2001100.00201.109.501999
2002133.00120.7511.302499
2004133.00161.1011.203499

输出样例:

maker
A
D


select maker
from 
(
    select maker,speed
    from product join pc on product.model=pc.model
    union
    select maker,speed
    from product join laptop on product.model=laptop.model
) as A
where A.speed>=133
group by A.maker
having count(*)>=2;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值