--从客户表中查询出所有的列:
select * from 客户表
--从客户表中查询出部分列:
select customerid,公司名称,contactname from 客户表
--在结果集中给查询的列以别名:
select 公司名称,contactname as 联系人姓名,address as 地址,phone as 电话号码,fax as 传真
from 客户表
--从雇员表中查询“所有的员工来自于哪几个不同的城市?”
select distinct 城市from 雇员表
--在雇员表表中查询FirstName列为“Andrew”的记录:
select * from 雇员表where firstname='andrew'
--查询“国籍”值不等于“USA”的记录:
select * from 雇员表where 国籍<>'usa'
--查询单价大于等于18小于等于40的产品记录:
select * from 产品表
where 总价格>=18 and 总价格<=40
--查询单价大于等于18小于等于40的产品记录,使用BETWEEN…AND:
select * from 产品表
where 总价格between 18 and 40
--查询员工所在的城市为Seattle、London或者Redmond的记录:
select * from 雇员表where 城市='seattle' or 城市='london' or 城市='redmond'
--查询员工表中“地区”列的值为NULL的记录:
select * from 雇员表
where 地区is null
--查询员工表中“地区”列的值非空的记录,使用IS NOT NULL:
select * from 雇员表
where 地区is not null
--用LEN函数求得“城市”值为London的公司的名称字符串的长度:
select len(公司名称) as 公司名称长度from 客户表
where 城市='london'
--用UPPER函数和LOWER函数实现字符串的大小写转换。查询雇员表用大写显示FirstName列,用小写显示LastName列:
select upper(firstname) as firstname,lower(lastname) as lastname from 雇员表
--下面的语句用REVERSE函数把雇员表的FirstName字符串反转:
select reverse(firstname) as 反转的名字from 雇员表
--用ABS函数返回给定数字或者表达式的绝对值:(自己定义数字)
select abs(-111),abs(0.0),abs(55.55)
--用CEILING函数返回大于或等于所给数字表达式的最小整数:(自定义一个数字)
select ceiling(40.01)
--用FLOOR函数返回小于或等于所给数字表达式的最大整数:(自定义一个数字)
select floor(40.99)
--用函数PI()得到圆周率,ROUND函数四舍五入到指定的精度,SQUARE求平方,SQRT求平方根:(自定义一个数字)
select pi(),round(40.14548,4),square(2),sqrt(9)
--从服务器上得到当前的日期和时间用GETDATE函数:
select getdate() as 当前系统时间
--用YEAR、MONTH和DAY函数分别获得当前日期的年份、月份和日期:
select year(getdate()) as 年,month(getdate()) as 月,day(getdate()) as 日
--求各类图书的平均价格并按价格从大到小排列
select type as 类型,avg(price) as 平均价格from 图书表
group by type
order by avg(price) desc
--要返回平均价格在13到18之间的图书分类
select type as 类型,avg(price) as 平均价格from 图书表
group by type
having avg(price) between 13 and 18
--用COUNT函数统计记录数
select count(*) as 记录数from 图书表
--统计每一类图书的平均价格,可以用GROUP BY子句对type列分组,然后用聚合函数AVG()求各组的平均值。
select type as 类型,avg(price) as 平均价格from 图书表
group by type
-1.查询所有学员的姓名、班级、性别
select sname,sclass,ssex from stud
--2.查询分数表中所有的记录,并且用别名替换原来字段名
select sid as 学号,oid as 课号,tid as 教员, mark as 考分from mark
--3.查询分数表中有哪几门课(课程号)
select distinct oid from mark
--4.查询所有的男生有详情
select * from stud where ssex='男'
--5.查询所有女教员的详情
select * from tch where tsex='女'
--6.查询所有不是教授的教员详情
select * from tch where tgrade<>'教授'
--7.不姓周的同学有哪些?
select * from stud where sname not like '周%'
--8.姓黄且姓名只有两个字的教员有哪些?
select * from tch where tname like '黄_'
--9.查询课程编号为4的课程,考分在80~90分的学员学号
select sid,oid,mark from mark
where oid='4' and mark between 80 and 90
--10.显示缺考学员的学号和缺考课程号(分数为空为缺考)
select sid,oid from mark
where mark is null
--11.查询1982年后出生的女生的详情
select * from stud
where ssex='女' and sage>'1982-12-31'
--12.按出生年月从先到后排列学员信息,且将没有填写出生日期的人排除在外
select * from stud
where sage is not null
order by sage
--13.显示教师编号为2的教师教过哪几门课程(课程号)
select distinct oid from mark where tid='2'
--14.查询学号为5的同学的总分、平均分、最高分、最底分
select sum(mark),avg(mark),max(mark),min(mark) from mark
where sid='5'