use pubs
--1. 对pubs数据库中的authors表,查询所有作者的信息。
select *from authors
--2. 查询作者所在的城市。
select distinct city from authors
--3. 查询作者编号、姓氏、电话号码及所在城市,查询结果按照作者编号的降序排列。
select au_id,au_fname+' '+au_lname 姓名,phone,city
from authors
order by au_id desc
--4. 查询作者编号、姓氏、电话号码及所在城市,查询结果按照城市降序排列,同一城市的按作者编号的升序排列。
select au_id,au_fname+' '+au_lname 姓名,phone,city
from authors
order by city,au_id
--5. 查询居住在Oakland的作者姓名和电话号码。
select au_fname+' '+au_lname 姓名,phone
from authors
where city in ('Oakland')
select au_fname+' '+au_lname 姓名,phone
from authors
where city='Oakland'
--6. 查询居住在Oakland或Berkeley的作者姓名和电话号码。查询结果按照城市升序排列。
select au_fname+' '+au_lname 姓名,phone
from authors
where city in ('Oakland','Berkeley')
order by city
--7. 查询所有作者全名,结果先按姓氏升序排列,如有雷同,再按照名字排列。
select au_fname+' '+au_lname 姓名
from authors
order by au_lname,au_fname
--8. pubs数据库,employee表,查询工资水平在100至172之间的员工编号及工资水平。
select * from employee
select emp_id,job_lvl
from employee
where job_lvl between 100 and 172
--9. 查询authors表中的姓氏,并返回作者名字的首字母
--答案
select au_lname,substring(au_fname,1,1)--从以一个开始截取第一个
from authors
order by au_lname
--函数用法
SUBSTR(str,pos): 由<str>中,选出所有从第<pos>位置开始的字元。请注意,这个语法不适用于SQL Server上。
SUBSTR(str,pos,len): 由<str>中的第<pos>位置开始,选出接下去的<len>个字元
SELECT SUBSTR(store_name, 3)
FROM Geography
WHERE store_name = 'Los Angeles';
结果:
's Angeles'
例2:
SELECT SUBSTR(store_name,2,4)
FROM Geography
WHERE store_name = 'San Diego';
结果:
'an D'
--10. 查询authors表中作者姓氏的第2个字母是‘r’的作者信息
select *from authors
where substring(au_lname,2,1)='r'
--11. 查询authors表中作者姓氏的最后一个字母是‘e’的作者信息.
select *from authors
where au_lname like '%e'
select *
from authors
where right(au_lname,1)='e'--从右数最后一个字母是
--12. 对上面两题,如果执行查询时不知道表中记录的大小写,该怎么办?
select *from authors
where substring(au_lname,2,1) in ('r','R')
select *from authors
where
au_lname like '%e_'
and
au_lname like '%E_'
--13. 查询pubs数据库employee中每个雇员的工龄。
select * from employee
--错误:select fname+' '+lname 姓名,getdate()-hire_date 工龄 from employee
--改正
select datediff(year,hire_date,getdate())--后面的减去前面的
from employee
图书--数据库查询
最新推荐文章于 2022-05-09 15:15:27 发布