以下代码是通过课上的实践整理的笔记,可以直接在sql查询窗口运行
1、select 1+1;----查询1+1=?
2、select (1+1) as sum;----查询1+1=?并启个别名
3、select select getdate(),@@version;----查询 当前日期日间,数据库版本
4、select * from students;----查询students表所有数据(包括了所有这表中的所有列)
5、select id,name from students;--查询students表中id,name这两列数据
6、select * from students where id >=1;--查询students表中id值大于等于1的所有数据
7、select * from students where 1 =1;--此条件永久成立,所以查询的是所有数据
8、select * from students where 1 <>1;--此条件永不成立,所以查询不出数据
9、select * from students where name like '%小%' ;--查询name列中有小字的所有数据
10、select * from students where name like '%小' ;--查询name列中以小字结尾的数据
11、select * from students where name like '小%' ;--查询name列中以小字开头的数据
12、select * from students where id between 1 and 5;-- 查询ID在1和5之间的所有数据
13、select * from students where id>1 and name like '%小' ;--查询ID大于1并且name列中以小字结尾的数据
14、select * from students where id>1 or name like '%小' ;--查询ID大于1或者name列中以小字结尾的数据
15、select * from students order by id desc;--查询students表所有数据以ID列倒序排列
16、select top 2 * from students order by id desc ;--查询students表所有数据中 以ID列倒序排列 的前两行
17、select * from students order by id asc;--查询students表所有数据以ID列顺序排列
18、select count( *) as total from students--查询表students所有行数
19、select count( *) as total, class from students group by class--以班级分组查询各个班级的人数
20、select sum(score) as total, class from students group by class--以班级分组查询各个班级的总分
21、select count( *) as total, class from students where score<60 group by class ;--以班级分组查询各个班级分数小于60分的人数
22、select max(score) as zg from students;--查询此表中的最高分
23、select min(score) as zg from students;----查询此表中的最低分
24、select distinct(class) as class from students;----查询此表中一共有几个班级
25、select * from students where id in (1,2,3);-- 查询ID为1、2、3的所有数据
26、select * from students where id not in (1,2,3);-- 查询ID不包括1、2、3的所有数据
27、select * from students where jianjie is null --查询表中简介为空的数据
28、select * from students where jianjie is not null --查询表中简介不为空的数据
29、select st.id,st.name,st.sex,cl.name as classname from students st ,classes cl where st.class=cl.id--多表关联查询
30、select st.id,st.name,st.sex,cl.name as classname
from students st ,
(select id ,name from classes) cl
where st.class=cl.id
--表简单嵌套查询
31、插入语句
insert into students( name,sex) values('小明','男');因为主键为自增列,所以不用插入主键值。
32、更新语句
update students set info='aa' where id=1;
update students set info='aa' ,name= 'aa' where id=1;
33、删除语句
delete FROM students;
delete FROM students where id=5;