有没有SQL数据库的高手,过来帮
SQL基本查询代码示例2006年08月18日 星期五 14:30--查询pubs库中jobs表中的所有记录
use pubs
select * from jobs
--查询所有的作者编号和姓名
--写法1
select au_id as 作者编号,au_fname+'·'+au_lname as 作者姓名 from authors
--写法2
select au_id ad,au_fname+'·'+au_lname 作者姓名 from authors
--写法3
select 作者编号=au_id,作者姓名=au_fname+'·'+au_lname from authors
--小结...全部
SQL基本查询代码示例2006年08月18日 星期五 14:30--查询pubs库中jobs表中的所有记录
use pubs
select * from jobs
--查询所有的作者编号和姓名
--写法1
select au_id as 作者编号,au_fname+'·'+au_lname as 作者姓名 from authors
--写法2
select au_id ad,au_fname+'·'+au_lname 作者姓名 from authors
--写法3
select 作者编号=au_id,作者姓名=au_fname+'·'+au_lname from authors
--小结一:更改的列名不论是什么字符,都可以不用引号引起来
--查询jobs表中前10行记录
select top 10 * from jobs
--小结二:限制结果集后,即指定显示的记录数目后,字段名或列名不可少,查询所有字段即用*表示
--查询jobs表中前10%行的记录
select top 10 percent * from jobs
--显示所有作者的姓名信息和作者编号,同上
select au_fname+'·'+au_lname as 作者姓名,au_id as 作者编号 from authors
--查询在CA州的作者姓名和城市
select au_fname+'·'+au_lname as 作者姓名,city as 城市 from authors
where state='ca'
--查询出版社编号为0877,而且价格大于16美元的书的信息
select * from titles
where pub_id='0877' and price>16
--查询出版日期在1991-1-1到1991-12-31之间的书名和出版日期
--方法1
select title as 书名,pubdate as 出版日期 from titles
where pubdate between '1991-1-1' and '1991-12-31'
--方法2
select title as 书名,pubdate as 出版日期 from titles
where pubdate > '1991-1-1' and pubdate ''
--方法2
select * from titles where not royalty=''
--方法3
select * from titles where royalty is not null
--2、查询Titles表,返回advance列值大于7000的行数;
select count(*) as 预付款大于7000的行数 from titles where advance > 7000
--3、查询Titles表,按照type列进行分组,显示每一组中type值、Price的平均值;
select type as 类别,avg(price) as 平均价格 from titles
group by type
--4、查询TitleAuthor表中,按照Title_ID进行分组查询,并显示每一组中royaltyper
-- 的最大值,查询限制条件的要求是:au_id的值必须不以”8”开头,
-- 但必须包含字符”8”;
select au_id ,title_id as 书号,max(royaltyper) as 最高版权费
from titleauthor
where au_id like '[^8]%' and au_id like '%8%'
group by title_id
--5、查询Sales表,要求返回ord_date在1993年到1994年之间,
-- 查询结果按照title_id降序的方式进行显示;
select * from sales where year(ord_date) between '1993' and '1994'
order by title_id desc
--小结十:日期型条件不能用通配符%
--问题一:why?
--6、查询Sales表,只返回40%的行;
select top 40 percent * from sales
--7、编写一个查询,找出现有图书的各个类别(不能有重复值)
--方法1:
select distinct type as 类别 from titles
--方法2:
select distinct type as 类别 from titles
group by type
--问题二:有没有必要分组,以上效果一样!
--8、编写一个查询,找出各个作者所著图书的数量
select au_id as 作者编号,count(title_id) as 著书数量 from titleauthor
group by au_id
--小结十一:as 跟字段名之前必须用空格隔开
--小结十二:如果字段列表中有一个字段使用了聚合函数,则其他字段则必须出现在
-- 分组语句group by中
--9、编写一个查询,显示各个作者的版权费(royaltyper)的总和
select au_id as 作者编号,sum(royaltyper) as 版权费总和 from titleauthor
group by au_id
--10、编写一个查询,显示sales表中各个Title的数量的平均值
select title_id as 类别,avg(qty) as 平均数量 from sales
group by title_id
-- 作业
--在示例数据库Northwind中,执行数据查询,要求如下:
use northwind
--1、在”Suppliers”表中查询,得到不同的地区有多少家供应商的信息;
select region as 地区,count(supplierID) as 供应商数目 from suppliers
group by region
--2、在”Suppliers”表中查询,显示哪些供应商有传真;
select supplierID as 供应商编号,fax as 传真 from suppliers
where fax is not null
--3、在“order details”表中查询,查询不同产品所订购的总价;
select productid as 产品编号,sum(unitprice*quantity*(1-discount)) as 总价
from "order details"
group by productid
order by productid
--4、在”Orders”表中查询,查询运货费排名前十位的所有信息;
select top 10 * from orders
order by freight desc
。
收起