Select
SELECT [EmployeeID]
FROM [dbo].[Employees]
- 这句话表示从表 Employees 中查询 EmployeeID ,查询结果如下:
如果我们要查询多个属性,只需要添加逗号即可:
SELECT [EmployeeID]
,[LastName]
,[FirstName]
,[Title]
,[TitleOfCourtesy]
,[BirthDate]
,[HireDate]
,[Address]
,[City]
,[Region]
,[PostalCode]
,[Country]
,[HomePhone]
,[Extension]
,[Photo]
,[Notes]
,[ReportsTo]
,[PhotoPath]
FROM [dbo].[Employees]
- 查询结果如下:
Distinct
如果我们不想查询重复的数值,而是想每个值只返回一次,那么我们可以使用Distinct关键字:
未使用distinct查询:
- 使用distinct查询:
SELECT DISTINCT [City]
FROM [dbo].[Employees]
查询结果
Top
Top关键字可以限制查询的条数,比如我只需要前面五行数据:
SELECT TOP 5 [Title]
FROM [dbo].[Employees]
Order
order关键字可以使查询结果根据一个或多个属性自动排序。
SELECT [LastName],[FirstName],[Title],[TitleOfCourtesy]
FROM [dbo].[Employees]
ORDER BY [LastName]
按照多个属性排序
SELECT [LastName],[FirstName],[Title],[TitleOfCourtesy]
FROM [dbo].[Employees]
ORDER BY [LastName],[FirstName]
查询结果
Desc
反向排序
SELECT [LastName],[FirstName],[Title],[TitleOfCourtesy]
FROM [dbo].[Employees]
ORDER BY [LastName],[FirstName] DESC
查询结果