名称、地区、城市和电话字段的值。
Select SupplierID,CompanyName,Region,City,Phone from [Northwind].[dbo].[Suppliers] where Region in ('东北','华南') <找不到符合条件的值>
请你练习
1. 在NorthWind数据库中查询所有产品的信息。
SQl语句为:select * from [Northwind].[dbo].[Products]
部分截图:
2. 查询NorthWind中雇员姓名、所在城市及其家庭电话,姓名以格式“姓.名”显示。
查询语句为:
Select LastName,FirstName,City,HomePhone from [Northwind].[dbo].[Employees]
3. 在NorthWind中查询出单价最贵的前10种产品的信息。
查询语句为:
select Top 10 * from [Northwind].[dbo].[Products] order by UnitPrice desc
4. 在NorthWind中查询出所有单价超过50美元的货物名称、货物代号及其单位重量。
查询语句为:
select ProductName,ProductID,QuantityPerUnit
from [Northwind].[dbo].[Products] where UnitPrice > 50
5.在NorthWind中查询出职务为销售代表,称呼为小姐的所有职员姓名和生日。 Select LastName,FirstName, BirthDate from [Northwind].[dbo].[Employees]
where Title='Sales Representative' and TitleOfCourtesy='Ms.'
分析与讨论:
1、SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。
实验九 稍复杂的查询(多表查询、子查询)
实验目的
1. 掌握SSMS查询编辑器的使用方法,加深对SQL语言的嵌套查询语句的理解。
2. 熟悉基本的连接查询,掌握内连接与外连接查询。 3. 掌握相关子查询的使用方法。
4. 学会嵌套子查询的使用方法、集合查询。
实验要求
1. 掌握在实际的数据关系中设计有意义的查询。 2. 掌握较复杂的数据查询方法。
实验内容
数据查询联系。 实验步骤
(1)查询唐强的工作所在部门名称和部门所在地。 select DNAME,LOCATION from [T].[dbo].[DEPT] where DEPTNO = ( select DEPTNO from [Test].[dbo].[EMP]
where ENAME='唐强')
(2)在沈阳工作的雇员信息。 select * from [Test].[dbo].[EMP] where DEPTNO = ( select DEPTNO from [Test].[dbo].[DEPT]
where LOCATION='沈阳')
3)找出在沈阳工作的雇员信息及其所在部门信息。
select * from [Test].[dbo].[EMP],[Test].[dbo].[DEPT]
where DEPT.LOCATION='沈阳' and DEPT.DEPTNO=EMP.DEPTNO
(4)找出工资超过经理的职工,列出姓名、工资及其经理的姓名、工资。 select first.ENAME 职工,second.ENAME 经理
from [Test].[dbo].[EMP] first,[Test].[dbo].[EMP] second where first.JOB not like'%经理'
and second.JOB in ('总经理','部门经理') and first.SAL>second.SAL
(5)下面使用Northwind数据库进行练习。查询Boise和Albuquerque的客户在1996年订购的所有订单的订单ID、所订购的产品名称和数量
use Northwind
select orders.orderid,productname,quantity,city from [order details] join products
on [order details].productid=products.productid join orders
on [order details].orderid=orders.orderid join customers
on orders.customerid=customers.customerid where city in('Boise' ,'Albuquerque') and
OrderDate between '1996-1-1' and '1996-12-31'
(6)查询客户的每份订单的订单ID、产品名称和销售金额 use Northwind
select orders.orderid,productname,[order details].unitprice*quantity as 销售金额 from [order details] join products
on [order details].productid=products.productid join orders
on [order details].orderid=orders.orderid join customers
on orders.customerid=customers.customerid
(7)按运货商公司名称,统计1997年由各个运货商承运的订单的总数量 use Northwind
select companyname,count(*) from shippers join orders
on shippers.shipperid=orders.shipvia where year(orderdate)=1997 group by companyname