简单的sql server查询

本文整理了在SQL Server中进行基本查询的多种方法,包括选择特定列、使用WHERE子句、LIKE操作符、聚合函数、ORDER BY排序、GROUP BY分组、JOIN操作以及INSERT、UPDATE、DELETE语句。这些例子旨在帮助初学者掌握SQL Server查询的基本技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下代码是通过课上的实践整理的笔记,可以直接在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;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值