【例6.8】从样例数据库pubs的employee表中查询所有列。
USE pubs
SELECT * FROM employee
运行结果:
emp_id fname minit lname job_id job_lvl pub_id hire_date
--------- -------------------- ----- ------------------------------ ------
PMA42628M Paolo M Accorti 13 35 0877 1992-08-27 00:00:00.000
PSA89086M Pedro S Afonso 14 89 1389 1990-12-24 00:00:00.000
…
…
HAS54740M Howard A Snyder 12 100 0736 1988-11-19 00:00:00.000
MFS52347M Martin F Sommer 10 165 0736 1990-04-13 00:00:00.000
GHT50241M Gary H Thomas 9 170 0736 1988-08-09 00:00:00.000
DBT39435M Daniel B Tonini 11 75 0877 1990-01-01 00:00:00.000
(所影响的行数为 43 行)
该查询语句执行的结果是显示表employee的所有列的信息。
【例6.9】从样例数据库pubs的Employees表中查询lname、job_id、hire_date列的信息。
USE pubs
SELECT lname,job_id,hire_date FROM employee
运行结果:
lname job_id hire_date
------------------------------ ------ ------------------------------------
Accorti 13 1992-08-27 00:00:00.000
Afonso 14 1990-12-24 00:00:00.000
Ashworth 6 1990-09-13 00:00:00.000
…
…
Thomas 9 1988-08-09 00:00:00.000
Tonini 11 1990-01-01 00:00:00.000
(所影响的行数为 43 行)
该查询语句执行的结果是显示表employe
【例6.10】从样例数据库pubs的employee表中查询lname、job_id、hire_date列的信息,为这些列取别名,并只返回前2条记录。
USE pubs
SELECT TOP 2 lname AS 'Last Name',job_id AS 'Job ID',hire_date AS 'Hire Date'
FROM employee
运行结果:
Last Name Job ID Hire Date
------------------------------ ------ ------------------------------------
Accorti 13 1992-08-27 00:00:00.000
Afonso 14 1990-12-24 00:00:00.000
(所影响的行数为 2 行)
该查询语句执行的结果是表employee的lname、job_id、hire_date列分别显示为Last Name、Job ID、Hire Date,并且只返回前2条记录。
【例6.11】从样例数据库pubs的titles表中查询书名(title列)及书价(price列)在9折后的新价格。
USE pubs
SELECT title,price*0.9 AS 'New Price'
FROM titles
运行结果:
title New Price
----------------------------------------------------------------------
The Busy Executive's Database Guide 17.99100
Cooking with Computers,Surreptitious Balance Sheets 10.75500
You Can Combat Computer Stress! 2.69100
...
...
Fifty Years in Buckingham Palace Kitchens 0.75500
Sushi,Anyone? 13.49100
(所影响的行数为 18 行)
该查询语句执行的结果是显示表titles书名及9折后的书价。但新价格精确到小数点3三位。如果要求只精确小数点后2位该怎么办?
【例6.12】从样例数据库pubs的titles表中查询书名(title列)及书价(price列)在9折后的新价格,新价格精确到小数点后2位。
USE pubs
SELECT title,round(price*0.9,2) AS 'New Price'
FROM titles
运行结果:
title New Price
-----------------------------------------------------------------------
The Busy Executive's Database Guide 17.99000
Cooking with Computers,Surreptitious Balance Sheets 10.76000
...
...
Fifty Years in Buckingham Palace Kitchens 10.76000
Sushi,Anyone? 13.49000
(所影响的行数为 18 行)
该查询语句中使用了数学函数round,执行的结果是显示表titles书名及9折后的书价,而且新价格精确到小数点后2位。
USE pubs
SELECT * FROM employee
运行结果:
emp_id fname minit lname job_id job_lvl pub_id hire_date
--------- -------------------- ----- ------------------------------ ------
PMA42628M Paolo M Accorti 13 35 0877 1992-08-27 00:00:00.000
PSA89086M Pedro S Afonso 14 89 1389 1990-12-24 00:00:00.000
…
…
HAS54740M Howard A Snyder 12 100 0736 1988-11-19 00:00:00.000
MFS52347M Martin F Sommer 10 165 0736 1990-04-13 00:00:00.000
GHT50241M Gary H Thomas 9 170 0736 1988-08-09 00:00:00.000
DBT39435M Daniel B Tonini 11 75 0877 1990-01-01 00:00:00.000
(所影响的行数为 43 行)
该查询语句执行的结果是显示表employee的所有列的信息。
【例6.9】从样例数据库pubs的Employees表中查询lname、job_id、hire_date列的信息。
USE pubs
SELECT lname,job_id,hire_date FROM employee
运行结果:
lname job_id hire_date
------------------------------ ------ ------------------------------------
Accorti 13 1992-08-27 00:00:00.000
Afonso 14 1990-12-24 00:00:00.000
Ashworth 6 1990-09-13 00:00:00.000
…
…
Thomas 9 1988-08-09 00:00:00.000
Tonini 11 1990-01-01 00:00:00.000
(所影响的行数为 43 行)
该查询语句执行的结果是显示表employe
【例6.10】从样例数据库pubs的employee表中查询lname、job_id、hire_date列的信息,为这些列取别名,并只返回前2条记录。
USE pubs
SELECT TOP 2 lname AS 'Last Name',job_id AS 'Job ID',hire_date AS 'Hire Date'
FROM employee
运行结果:
Last Name Job ID Hire Date
------------------------------ ------ ------------------------------------
Accorti 13 1992-08-27 00:00:00.000
Afonso 14 1990-12-24 00:00:00.000
(所影响的行数为 2 行)
该查询语句执行的结果是表employee的lname、job_id、hire_date列分别显示为Last Name、Job ID、Hire Date,并且只返回前2条记录。
【例6.11】从样例数据库pubs的titles表中查询书名(title列)及书价(price列)在9折后的新价格。
USE pubs
SELECT title,price*0.9 AS 'New Price'
FROM titles
运行结果:
title New Price
----------------------------------------------------------------------
The Busy Executive's Database Guide 17.99100
Cooking with Computers,Surreptitious Balance Sheets 10.75500
You Can Combat Computer Stress! 2.69100
...
...
Fifty Years in Buckingham Palace Kitchens 0.75500
Sushi,Anyone? 13.49100
(所影响的行数为 18 行)
该查询语句执行的结果是显示表titles书名及9折后的书价。但新价格精确到小数点3三位。如果要求只精确小数点后2位该怎么办?
【例6.12】从样例数据库pubs的titles表中查询书名(title列)及书价(price列)在9折后的新价格,新价格精确到小数点后2位。
USE pubs
SELECT title,round(price*0.9,2) AS 'New Price'
FROM titles
运行结果:
title New Price
-----------------------------------------------------------------------
The Busy Executive's Database Guide 17.99000
Cooking with Computers,Surreptitious Balance Sheets 10.76000
...
...
Fifty Years in Buckingham Palace Kitchens 10.76000
Sushi,Anyone? 13.49000
(所影响的行数为 18 行)
该查询语句中使用了数学函数round,执行的结果是显示表titles书名及9折后的书价,而且新价格精确到小数点后2位。