您的位置:首页 > 数据库

SQL语句中的查询操作

2016-04-01 22:03 323 查看
单表查询

1.查询全体学生地 学号和姓名

select Sno,Sname
from Student;


2.查询全体学生的详细记录

select *
from Student;


3.查询经过计算的值(查询全体学生的姓名及出生年份)

select Sname, 'Year of Birth;',2004-Sage,LOWER(Sdept)
from Student;


选择表中的若干元祖

1.消除取消重复的行

select distinct Sno
from SC;


2.查询满足条件的元祖

比较运算符  =,>, <, >=, <=, !=, <>, !>, !<, NOT +

确定范围 between and, not between and

确定集合 in,not in

字符匹配  like, not like

空值 is null, is not null

多重运算符(逻辑运算符) and ,or, not

比较大小

查询计算机科学系全体学生的名单

select Sname
from Student
where Sdept='CS'


注意该查询的运算过程 :对Student表进行全表扫描,取出一个元组,检查该元组在Sdept列的值是否等于‘CS’。如果相等,取出Sname列的值形成一个新的元组输出,否则跳出该元组,取下一个元组。(from->where->select)面试可能会问。。。。

查询所有年龄在20岁以下的学生姓名及年龄

select Sname,Sage
from Student
where Sage<20;


查询考试成绩不及格的学生的学号

select distinct Sno
from SC
where Grade<60;


确定范围

查询年龄在20-23岁之间的学生姓名、系别和年龄

select Sname,Sdept,Sage
from Student
where Sage between 20 and 23;


查询年龄不在20-23岁之间的学生姓名、系别和年龄

select Sname,Sdept,Sage
from Student
where Sage not between 20 and 23;


确定集合

查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别

select Sname,Ssex
from Student
where Sdept in ('CS','MA','IS');


查询既不是计算机系,数学系,也不是信息系的学生姓名和性别

select Sname,Ssex
from Student
where Sdept not in ('CS','MA','IS');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql