文档库 最新最全的文档下载
当前位置:文档库 › select试题参考答案

select试题参考答案

select试题参考答案
select试题参考答案

----答案网上下载,未经检验,仅供参考。

--------简单查询

1 显示所有部门名

select dname

from dept;

2 显示存在雇员的所有部门号

select distinct deptno

from emp;

3 显示工资超过2850的雇员名和工资

select ename,sal

from emp

where sal > 2850;

4 显示工资不在1500到2850之间的所有雇员名及工资select ename,sal

from emp

where sal not between 1500 and 2850;

5 显示雇员代码为7566的雇员名及所在部门号

select ename,deptno

from emp

where empno = 7566;

6 显示部门代码为10和30中工资超过1500的雇员名及工资select ename,sal

from emp

where sal > 1500 and deptno in (10,30)

7 显示无管理者的雇员名及岗位

select ename,job

from emp

where mgr is null;

8 显示获得补助的所有雇员名、补助额以及所在部门号select deptno,ename,comm

from emp

where comm is not null;

9 显示部门代码为20的部门的所有雇员名、雇员工资及岗位select ename,sal,job

where deptno = 20;

10 显示所有雇员的姓名、部门编号、工资,并且列名要显示为中文

select ename as 姓名,

deptno as 部门编号,

sal as 工资

from emp;

11 显示工资大于1500的雇员名和工资,并且按照工资的降序排列

select ename,sal

from emp

where sal > 1500

order by sal desc;

12 显示雇员部门编号为10或20的信息(要求使用IN关键字)

select *

from emp

where deptno in (10,20);

13 显示雇员名的第二个字母为A的信息

select ename

from emp

where ename like '_A%';

14 显示没有发放补助的雇员信息

select *

from emp

where comm is null;

15 显示所有雇员名及其全年收入(工资+补助),并指定列别名"年收入"

select ename,nvl2(comm,sal+comm,sal) as 年收入

from emp;

--NVL2(x,v1,v2):如果x不为NULL,返回v1,否则返回v2。也可用if、case when实现。

--------分组函数

1 显示所有雇员的平均工资、总计工资、最高工资、最低工资

select avg(sal) as 平均工资,

sum(sal) as 总计工资

max(sal) as 最高工资

min(sal) as 最低工资

2 显示每种岗位的雇员总数、平均工资

select job,count(*),avg(sal)

from emp

group by job;

3 显示雇员总数,以及获得补助的雇员数

select count(*),count(comm)

from emp;

4 显示管理者的总人数

select count(distinct mgr)

from emp;

5 显示雇员工资的最大差额

select max(sal) - min(sal)

from emp;

6 显示每个部门每个岗位的平均工资、每个部门的平均工资、每个岗位的平均工资select deptno,job,avg(sal)

from emp

group by cube(deptno,job); --有疑问。

7 显示雇员表中记录总数

select count(*)

from emp;

--------表连接

1 显示所有雇员名、雇员工资及所在部门名

select d.dname,e.ename,e.sal

from dept d,emp e

where d.deptno = e.deptno;

2 显示部门代码为20的部门名,以及该部门的所有雇员名、雇员工资及岗位

select d.dname,e.ename,e.sal

from dept d,emp e

where d.deptno = e.deptno

and d.deptno = 20;

3 显示所有雇员名、雇员工资及工资级别

select e.ename,e.sal,s.grade

from emp e,salgrade s

where e.sal >= s.losal and e.sal <= s.hisal;

4 显示雇员"SCOTT"的管理者名

select m.ename

from emp e,emp m

where e.mgr = m.empno

and e.ename = 'SCOTT';

5 显示获得补助的所有雇员名、补助额以及所在部门名

select d.dname,e.ename,https://www.wendangku.net/doc/6910716548.html,m

from dept d,emp e

where d.deptno = e.deptno

and https://www.wendangku.net/doc/6910716548.html,m is not null;

6 查询EMP表和SALGRADE表,显示部门代码为20的雇员名、工资及其工资级别select e.ename,e.sal,s.grade

from emp e,salgrade s

where e.sal >= s.losal and e.sal <= s.hisal

and e.deptno = 20;

7 显示部门代码为10的所有雇员名、部门名,以及其他部门名

select e.ename,d.dname

from dept d,emp e

where d.deptno = e.deptno(+)

and e.deptno(+) = 10;

8 显示部门代码为10的所有雇员名、部门名,以及其他雇员名

select e.ename,d.dname

from dept d,emp e

where d.deptno(+) = e.deptno

and d.deptno(+) = 10;

9 显示部门代码为10的所有雇员名、部门名,以及其他部门名和雇员名

select e.ename,d.dname

from dept d full join emp e

on d.deptno = e.deptno

and d.deptno = 10;

10显示"BLAKE"同部门的所有雇员,但不显示"BLAKE"

select e.ename,e.deptno

from emp e,emp b

where e.deptno = b.deptno

and e.ename <> 'BLAKE'

and b.ename = 'BLAKE';

--------子查询等稍复杂题

1 按以下格式显示下面的信息,条件是工资大于1500的。

-- 部门名称姓名工资

select dname,ename,sal

from dept,emp

where dept.deptno = emp.deptno;

2 按以下格式显示下面信息,条件是此人工资在所有人中最高。

-- 部门姓名工资

select dname,ename,sal

from dept,emp

where dept.deptno = emp.deptno

and emp.sal = (select max(sal) from emp);

3 按以下格式显示下面信息

-- 某人为某人工作

select e.ename || '为' || m.ename || '工作' as 描述

from emp e,emp m

where e.mgr = m.empno;

4 为所有人长工资,标准是:10部门长10%;20部门长15%;

-- 30部门长20%其他部门长18%(要求用DECODE函数)

select ename,deptno,sal,sal*(1+nvl(decode(deptno,10,0.1,20,0.15,30,0.2),0.18)) as newsal from emp;

5

--根据工作年限长工资,标准是:为公司工作了几个月就长几个百分点。

select ename,hiredate,sal,sal*(1+round(months_between(sysdate,hiredate)/100.0)) as newsal from emp;

6 查询出king所在部门的部门号\部门名称\部门人数

--ex1

select d.deptno,d.dname,count(*)

from dept d,emp e,emp m

where d.deptno = e.deptno

and e.deptno = m.deptno

and m.ename = 'KING'

group by d.deptno,d.dname;

--ex2

select d.deptno,d.dname,count(*)

from dept d,emp e

where d.deptno = e.deptno

and e.deptno = (select deptno from emp where ename = 'KING')

group by d.deptno,d.dname;

7 查询出king所在部门的工作年限最大的员工名字

select ename,hiredate

from emp

where (deptno,hiredate) in (select deptno ,min(hiredate)

from emp

where deptno in (select deptno

from emp

where ename = 'KING')

group by deptno);

8 查询出管理员工人数最多的人的名字和他管理的人的名字

with m as

( select empno,ename

from emp

where empno in ( select mgr

from emp

group by mgr

having count(*)>= all ( select count(*)

from emp

group by mgr)

)

)

select ename,'manager' as type from m

union

select e.ename, 'emp' as type from m,emp e where e.mgr = m.empno;

9 查询出工资成本最高的部门的部门号和部门名称

select d.deptno,d.dname

from dept d,emp e

where d.deptno = e.deptno

group by d.deptno,d.dname

having sum(e.sal) >= all (select sum(sal)

from emp

group by deptno);

10 查询出工资不超过2500的人数最多的部门名称

select d.deptno,d.dname

where d.deptno = e.deptno

and e.sal <= 2500

group by d.deptno,d.dname

having count(*) >= all (select count(*)

from emp

where sal <= 2500

group by deptno);

11 查询出没有下属员工的人的名字和他的职位

select ename,job

from emp

where empno not in ( select distinct nvl(mgr,0)

from emp);

12 查询出人数最多的那个部门的部门编号和部门名称

select d.deptno,d.dname

from dept d,emp e

where d.deptno = e.deptno

group by d.deptno,d.dname

having count(*) >= all (select count(*)

from emp

group by deptno);

13 查询出没有员工的那个部门的部门编号和部门名称(要求用两种方法,其中一种要用集合运算)

--ex1

select deptno,dname

from dept

where deptno not in (select deptno from emp);

--ex2

select deptno,dname

from dept

where not exists

( select deptno deptno

from emp

where dept.deptno = emp.deptno );

--ex3

select deptno,dname

from dept

minus

select d.deptno,d.dname

where d.deptno = e.deptno;

14 查询出员工名字以A打头的人数最多的部门名称和员工名字

select d.dname,e.ename

from dept d,emp e

where d.deptno = e.deptno

and e.deptno in ( select deptno

from emp

where ename like 'A%'

group by deptno

having count(*) >= all (select count(*)

from emp

where ename like 'A%'

group by deptno)

);

15 现在公司要给员工增加工龄工资,规则是:30*工作年限,请按以下格式显示下面结果:

-- 部门名称员工姓名原工资增加额度新工资

select d.dname as 部门名称,e.ename as 员工姓名,e.sal as 原工资,

trunc(months_between(sysdate,hiredate)/12,0) * 30 as 增加额度,

e.sal + trunc(months_between(sysdate,hiredate)/12,0) * 30 as 新工资

from dept d,emp e

where d.deptno = e.deptno;

16 针对DEPT和EMP表,查询出下面格式的结果并要求按部门编号和工资降序排列。

-- 部门名称员工姓名工资

select d.dname as 部门名称,e.ename as 员工姓名,

e.sal as 工资

from dept d,emp e

where d.deptno = e.deptno

order by d.deptno ,e.sal desc;

17 针对DEPT和EMP表,查询出下面格式的结果。

-- 部门编号部门名称部门工资最小值部门工资最大值部门工资平均值部门工资合计值

select d.deptno as 部门编号,d.dname as 部门名称,

min(e.sal) as 部门工资最小值,

max(e.sal) as 部门工资最大值,

avg(e.sal) as 部门工资平均值,

sum(e.sal) as 部门工资合计值

from dept d,emp e

where d.deptno = e.deptno

group by d.deptno,d.dname;

18 针对DEPT和EMP表,查询出SMITH所在部门的部门名称、部门工资平均值。(要求使用子查询)

select d.deptno,d.dname,avg(e.sal)

from dept d,emp e

where d.deptno = e.deptno

and e.deptno in (select deptno from emp where ename = 'SMITH')

group by d.deptno,d.dname;

19 针对DEPT和EMP表,查询出下面格式的结果。(要求使用外连接,没有员工的部门名也要显示。

-- 员工姓名如果是空值,要求用"不存在"代替;如果工资是空值,要求用0代替。)

-- 部门名称员工姓名工资

select d.deptno,nvl(e.ename,'不存在'),nvl(e.sal,0)

from dept d ,emp e

where d.deptno = e.deptno(+);

20 针对DEPT和EMP表,查询出没有员工的部门号和部门名称(要求用两种方法)

--ex1

select deptno,dname

from dept

where deptno not in (select deptno from emp);

--ex2

select deptno,dname

from dept

where not exists

( select deptno deptno

from emp

where dept.deptno = emp.deptno );

--ex3

select deptno,dname

from dept

minus

select d.deptno,d.dname

from dept d ,emp e

where d.deptno = e.deptno;

21 查询出平均工资最高的部门编号、部门名称和平均工资。

select d.deptno,d.dname,avg(e.sal)

from dept d,emp e

where d.deptno = e.deptno

group by d.deptno,d.dname

having avg(e.sal) >= all ( select avg(sal) from emp group by deptno) ;

22 查询出工资高于全体平均工资人数最多的部门编号、部门名称和员工姓名、工资。select d.deptno,d.dname,e.ename,e.sal

from dept d,emp e

where d.deptno = e.deptno

and d.deptno in ( select deptno

from emp

where sal > (select avg(sal) from emp)

group by deptno

having count(*) >= all ( select count(*)

from emp e

where e.sal > (select avg(sal) from emp)

group by e.deptno

)

);

lab1 SQLPlus使用及简单Select语句

实验1 SQL*Plus使用及简单Select语句 实验人:_________ 学号_____ 班级____________ 实验目的: 1.掌握SQL*Plus常用功能的使用。 2.掌握简单查询的语法。 实验平台: 1.Windows 2000/XP。 2.Oracle 10g 实验过程记录及分析: 1.SQL*Plus的使用: 1) 2) 3)如果某个用户连接数据库时,发生了“协议适配器错误”,分析其原因,并给出解决错 4)

5) 6) 7) 2. SQL 1) 2)3

4)查询emp表中,工资额大于2000的员工的姓名及其工资额。 5) 6) 7)查询emp表中,ename列含有字母A的员工的姓名。

8) 9) 10

11)使用to_date函数查询1981年入职的员工姓名。 SQL> select * from emp 2 where to_char(hiredate,'yyyy')='1981'; EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- -------------- ---------- ---------- DEPTNO ---------- 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7566 JONES MANAGER 7839 02-4月 -81 2975 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- -------------- ---------- ---------- DEPTNO ---------- 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- -------------- ---------- ----------

SQL语句大全实例

SQL语句实例 表操作 例 1 对于表的教学管理数据库中的表STUDENTS ,可以定义如下:CREATE TABLE STUDENTS (SNO NUMERIC (6, 0) NOT NULL SNAME CHAR (8) NOT NULL AGE NUMERIC(3,0) SEX CHAR(2) BPLACE CHAR(20) PRIMARY KEY(SNO)) 例 2 对于表的教学管理数据库中的表ENROLLS ,可以定义如下: CREATE TABLE ENROLLS (SNO NUMERIC(6,0) NOT NULL CNO CHAR(4) NOT NULL GRADE INT PRIMARY KEY(SNO,CNO) FOREIGN KEY(SNO) REFERENCES STUDENTS(SNO) FOREIGN KEY(CNO) REFERENCES COURSES(CNO) CHECK ((GRADE IS NULL) OR (GRADE BETWEEN 0 AND 100))) 例 3 根据表的STUDENTS 表,建立一个只包含学号、姓名、年龄的女学生表。 CREATE TABLE GIRL

AS SELECT SNO, SNAME, AGE FROM STUDENTS WHERE SEX=' 女'; 例 4 删除教师表TEACHER 。 DROP TABLE TEACHER 例 5 在教师表中增加住址列。 ALTER TABLE TEACHERS ADD (ADDR CHAR(50)) 例 6 把STUDENTS 表中的BPLACE 列删除,并且把引用BPLACE 列的所有视图和约束也一起删除。 ALTER TABLE STUDENTS DROP BPLACE CASCADE 例7 补充定义ENROLLS 表的主关键字。 ALTER TABLE ENROLLS ADD PRIMARY KEY (SNO,CNO) ; 视图操作(虚表) 例9 建立一个只包括教师号、姓名和年龄的视图FACULTY 。( 在视图定义中不能包含ORDER BY 子句) CREATE VIEW FACULTY AS SELECT TNO, TNAME, AGE FROM TEACHERS 例10 从学生表、课程表和选课表中产生一个视图GRADE_TABLE ,它包括学生姓名、课程名和成绩。 CREATE VIEW GRADE_TABLE AS SELECT SNAME,CNAME,GRADE FROM STUDENTS,COURSES,ENROLLS WHERE STUDENTS.SNO =ENROLLS.SNO AND https://www.wendangku.net/doc/6910716548.html,O=https://www.wendangku.net/doc/6910716548.html,O 例11 删除视图GRADE_TABLE DROP VIEW GRADE_TABLE RESTRICT 索引操作 例12 在学生表中按学号建立索引。 CREATE UNIQUE INDEX ST ON STUDENTS (SNO,ASC) 例13 删除按学号所建立的索引。 DROP INDEX ST 数据库模式操作

实验4--SQL语言--SELECT查询操作

实验4--SQL语言--SELECT查询操作 1、基于?教学管理?数据库jxgl,试用SQL的查询语句表达下列查询。(1)--检索年龄大于23的男学生的学号和姓名-- select sno,sn from s where sex='男'and age > 23 (2)--检索至少选修一门课程的女学生姓名-- select sn from S,SC where sex='女' AND S.Sno=SC.Sno group by S.Sn having count(*)>=1; (3)--检索王同学没有选修的课程的课程号-- select cno from c where https://www.wendangku.net/doc/6910716548.html,o not in (select cno from sc,s where sc.sno=s.sno and sn like'王%') (4)--检索至少选修两门课程的学生学号-- select distinct s.sno from s,sc where sc.sno=s.sno group by s.sno having count(*)>=2; (5)--检索全部学生都选修的课程的课程号与课程名-- select cno,cn from c where not exists (select*from s where not exists (select*from sc where s.sno=sc.sno and https://www.wendangku.net/doc/6910716548.html,o=https://www.wendangku.net/doc/6910716548.html,o)) (6)--检索选修了所有3学分课程的学生学号和姓名-- select distinct s.sno,s.sn from s,sc where exists (select*from c where ct='3'and s.sno=sc.sno and https://www.wendangku.net/doc/6910716548.html,o=https://www.wendangku.net/doc/6910716548.html,o) 2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。 (1)--统计有学生选修的课程门数-- select count(distinct https://www.wendangku.net/doc/6910716548.html,o)from sc; (2)--查询选修4号课程的学生的平均年龄-- select avg(s.age) from s,sc where s.sno=sc.sno and cno='4';

SQL常用语句+举例

SQL 常用语句+举例 相关表: 1. distinct: 剔除重复记录 例:select distinct stroe_name from Store_information 结果: 2. And / or: 并且/或 例:在表中选出所有sales 高于$1000或是sales 在$275及$500之间的记录 Select store_name ,sales from Store_information Where sales>1000 Or (sales>275 and sales <500) 3. 例:在表中查找store_name 包含 Los Angeles 或San Diego 的记录 Select * from Store_information where store_name in (‘Los Angeles ’,’San Diego ’) 结果: 4. Between : 可以运用一个范围抓出表中的值

与in 的区别:in 依照一个或数个不连续的值的限制抓出表中的值 例:查找表中介于Jan-06-1999 及Jan-10-1999 中的记录 Select * from Store_information where date between ‘Jan-06-1999’ and ‘Jan-10-1999’ 结果: 5. Like : 让我们依据一个套式来找出我们要的记录 套式通常包含: ’A_Z ’: 所有以A 开头,中间包含一个字符,以Z 结尾的字串 ’ABC%’: 所有以ABC 起头的字串 ’%XYZ ’: 所有以XYZ 结尾的字串 ’%AN%’: 所有包含AN 的字串 例:Select * from Store_information where store_name like ‘%An%’ 结果: 6. Order by: 排序,通常与ASC (从小到大,升序)、DESC (从大到小,降序)结合使用 当排序字段不止一个时,先依据字段1排序,当字段1有几个值相同时,再依据字段2排序 例:表中sales 由大到小列出Store_information 的所有记录 Select Store_name, sales,date from Store_information order by sales desc 结果: 7. 函数:AVG (平均值)、COUNT (计数)、MAX (最大值)、MIN (最小值)、SUM(求和) 语句:select 函数名(字段名) from 表名 例:求出sales 的总和 Select sum(sales) from Store_information 结果 8. COUNT (计数) 例:找出Store_information 表中 有几个store_name 值不是空的记录

photoShop操作实例大全

】【共享】[4-17]photoShop基本制作技能大集合 我感觉很精典的教程: 下载地址:https://www.wendangku.net/doc/6910716548.html,/softdown ... /200510/IT_yyjz.rar 1 Photoshop只是一个工具而已,不要把它看成神物。 2 看5本书不如自己动脑筋分析一个例子。 3 学Photoshop并不难,难的是学会怎么用。 4 不要试图掌握photoshop的每一个功能,熟悉和你工作最相关的部分就可以了。 5 不要看不起最基本的元素,往往看起来比较复杂的图像就是这些基本元素构成的。 6 不要问:有没有XXX教程--耐心的人会自己打开photoshop尝试。 7 不要说:不要让我用英文的photoshop,不要让我看英文的网站,我看不懂--谁都是从不懂到懂的8 不要担心:我没有学过美术,我一定用不好photoshop。 9 不要只问不学 10 学photoshop要坚持,要有耐心 11 看到某个图像的教程请试着用同样方法作出其他的图像 12 时常总结、吸收自己和其他人的小敲门、技巧 13 有了问题先自己想,察看帮助,1个小时后没有结果再问别人 14 学会用搜索引擎,很多知识在网上可以轻松得到 15 花3个小时做10张图,不如花10个小时做3张图。 16 不要总想在图片中赋予什么意义,好看就行 17 学photoshop首先掌握功能,然后掌握方法 18 先会走再会跑 19 明白了以上18条,你会觉得photoshop不过如此 1.如何制作晕映效果? 如何制作晕映效果?

晕映(Vignettss)效果是指图像具有柔软渐变的边缘效果。使用Photoshop制作晕映效果非常容易,下面介绍如何使用Photoshop5.5制作图像的两种晕映效果。 1.椭圆晕映效果 1)使用Photoshop5.5打开一幅图; 2)在工具栏中选择椭圆套索工具; 3)用椭圆套索工具在图像中选取所需的部分;

mysql中select简单查询

1.简单查询select语句(1) (1) create database chapter04; use chapter04; create table exam ( id int not null primary key default null auto_increment, name varchar(20) not null, Chinese double, math double, english double ); insert into exam values(null,'关羽',85,76,70); insert into exam values(null,'张飞',70,75,70); insert into exam values(null,'赵云',90,65,95); (2) select name,english from exam; (3) select distinct english from exam; 2.简单查询select语句(2) (1)select Chinese+10,math+10,english+10 from exam; (2)select sum(Chinese+math+english) from exam where name='关羽’;

select sum(Chinese+math+english) from exam where name='张飞’; select sum(Chinese+math+english) from exam where name='赵云’; (3)select sum(Chinese+math+english) as score from exam; 3.简单查询select语句(3) (1)select name,score from exam where name='张飞'; (2)select name,english from exam where english>90; (3)select name,score from exam where score>230; 4.简单查询select语句(4) (1)select name,english from exam where english between 80 and 100; (2)select name,math from exam where math in (75,76,77); (3)insert into exam values(null,'张三',80,70,78); select score,name from exam where name like'张%'; (4)select name,score from exam where math>70 and Chinese>80; 5.聚合函数-count (1)select count (*) from exam; (2)select count (*) from exam where math>70;

50个常用sql语句实例(学生表 课程表 成绩表 教师表)

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)) 前面加一列序号: if exists(select table_name from information_schema.tables where table_name='Temp_Table') drop table Temp_Table go select 排名=identity(int,1,1),* INTO Temp_Table from Student go select * from Temp_Table go drop database [ ] --删除空的没有名字的数据库 问题: 1、查询“”课程比“”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student

SQL语句 SELECT LIKE like用法详解

SQL语句 SELECT LIKE like用法详解 2009-12-16 13:44 LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用是在一个字符型字段列中检索包含对应子串的。 假设有一个数据库中有个表table1,在table1中有两个字段,分别是name 和sex二者全是字符型数据。现在我们要在姓名字段中查询以“张”字开头的记录,语句如下: select * from table1 where name like "张*" 如果要查询以“张”结尾的记录,则语句如下: select * from table1 where name like "*张" 这里用到了通配符“*”,可以说,like语句是和通配符分不开的。下面我们就详细介绍一下通配符。 多个字符 * c*c代表cc,cBc,cbc,cabdfec等 它同于DOS命令中的通配符,代表多个字符。 多个字符 % %c%代表agdcagd等 这种方法在很多程序中要用到,主要是查询包含子串的。 特殊字符 a a代表a*a 代替* 单字符 ? b?b代表brb,bFb等 同于DOS命令中的?通配符,代表单个字符 单数字 # k#k代表k1k,k8k,k0k 大致同上,不同的是代只能代表单个数字。 字符范围 - [a-z]代表a到z的26个字母中任意一个 指定一个范围中任意一个 续上

排除 [!字符] [!a-z]代表9,0,%,*等 它只代表单个字符 数字排除 [!数字] [!0-9]代表A,b,C,d等 同上 组合类型 字符[范围类型]字符 cc[!a-d]#代表ccF#等 可以和其它几种方式组合使用 例:假设表table1中有以下记录: name sex 张小明男 李明天男 李a天女 王5五男 王清五男 下面我们来举例说明一下: 查询name字段中包含有“明”字的。 select * from table1 where name like '%明%' 查询name字段中以“李”字开头。 select * from table1 where name like '李*' 查询name字段中含有数字的。 select * from table1 where name like '%[0-9]%' 查询name字段中含有小写字母的。 select * from table1 where name like '%[a-z]%' 查询name字段中不含有数字的。 select * from table1 where name like '%[!0-9]%' 我们着重要说明的是通配符“*”与“%”的区别。 select * from table1 where name like '*明*' select * from table1 where name like '%明%' 大家会看到,前一条语句列出来的是所有的记录,而后一条记录列出来的是name字段中含有“明”的记录,所以说,当我们作字符型字段包含一个子串的查询时最好采用“%”而不用“*”,用“*”的时候只在开头或者只在结尾时,而不能两端全由“*”代替任意字符的情况下。 大家在写sql 语句的时候,如果是 select .. where 类型的语句,有注意到条件的前后顺序吗?我今天做个小实验。 比如查询地址里包含“海口市”及“振兴路” 两个关键字的数据,一般时候可能会用

常用SELECT语句汇总

常用SELECT语句汇总 一、单表查询 (一)按照条件查询相关记录 Select 字段1,字段2……字段N from 表 where 条件含义:从表中根据where 条件查询记录,每条记录显示的字段按照字段1、字段2….字段N的设置显示 注:select语句中的标点符号及运算符必须使用英文半角字符。 例1:从凭证库中查询2004年1月31日的凭证,每条凭证只显示凭证日期、凭证号、科目名称、借方金额、贷方金额、会计月份 6个字段 Select 凭证日期,凭证号,科目名称,借方金额,贷方金额,会计月份 From 凭证库 where 凭证日期=’2004-1-31’ 例2:根据业务_个人基本情况表,找出缴存状态为”正常”的记录,查出的记录只显示姓名、身份证号、单位账号及个人账号 4个字段 Select 个人姓名,身份证号,单位账号,个人账号 from 业务_个人基本情况表 where 账户状态=’1’ 例3:从科目余额表中查询出2010年借方金额大于50万或2010年借方金额小于10万的记录,每条记录只显示摘要、科目编码、借方金额、贷方金额、年度5个字段 Select摘要,科目编码,借方金额,贷方金额,年度 From 科目余额 where(借方金额>500000 and 年度=2010) or (借方金额<100000 and 年度=2010) Select top 100 字段1,字段2……字段N from 表 where 条件含义:从表中根据where 条件查询记录,显示前100条记录,每条记录按照字段1、字段2….字段N的设置显示 例1:从凭证库中查询2004年1月31日的前100条凭证,每条 2 凭证只显示凭证日期、凭证号、科目名称、借方金额、贷方金额、会计月份 6个字段Select top 100凭证日期,凭证号,科目名称,借方金额,贷方金额,会计月份 From 凭证库where 凭证日期=’2004-1-31’ 例2:根据业务_个人基本情况表,找出缴存状态为”正常”的前100条记录 Select top 100个人姓名,身份证号,单位账号,个人账号 from 业务_个人基本情况表where 账户状态=’1’ (二)通配符的使用 *表示将全部的字段内容都显示出来 例1:从业务_电子警察表中筛选出无车号或者车牌号小于3位的记录 Select * from 业务_电子警察 where 车号=’’ or Len(车号)<3 例2:从科目余额表中查询出2002年收入大于50万的记录 Select * from 科目余额 where 借方金额>500000 and 年度=2002 %表示零或多个字符 例1:从凭证库中查询2003年各月的房租收入情况 Select month(凭证日期) as 月份, sum(贷方金额) as 房租金额 from 凭证 where 摘要 like ‘%房租%’ and 年度=2003 例2:从凭证库中查询 2008年包含税的记录 Select * from 凭证库 where摘要 like ‘%税%’ and 年度=2008 _表示任何一个字符 例1:根据科目余额表查询出目编码为10开头的一级科目记录 Select * from 科目余额

[MSSQL] - SELECT语句使用大全

SELECT语句使用大全 虽然 SELECT 语句的完整语法比较复杂,但是大多数 SELECT 语句都描述结果集的四个主要属性 1、结果集中的列的数量和属性。 2、从中检索结果集数据的表,以及这些表之间的所有逻辑关系。 3、为了符合 SELECT 语句的要求,源表中的行所必须达到的条件。不符合条件的行会被忽略。 4、结果集的行的排列顺序。 它的主要子句可归纳如下: SELECT select_list --描述结果集的列 INTO new_table_name --指定使用结果集来创建新表 FROM table_list --包含从中检索到结果集数据的表的列表[返回结果集的对象]。 [ WHERE search_conditions ] --WHERE 子句是一个筛选,它定义了源表中的行要满足 SELECT 语句的要求所必须达到的条件 [ GROUP BY group_by_list ] --根据 group_by_list 列中的值将结果集分成组[ HAVING search_conditions ] --结果集的附加筛选 [ ORDER BY order_list [ ASC | DESC ] ] --结果集的附加筛选 一、使用选择列表 1、使用 *号来选择所有列;使用“[表名|别名]。[字段]”选取特定的列。 2、AS 子句可用来更改结果集列的名称或为派生列分配名称,也可以使用空格代替 如: SELECT Name AS Name1,Name Name2 FROM Product ORDER BY Name ASC 3、使用 DISTINCT 消除重复项 如:select distinct [Year] from A 4、使用 TOP 和 PERCENT 限制结果集数量 TOP ( expression ) [ PERCENT ] [ WITH TIES ] --expression 数量、PERCENT按百分比返回数据、WITH TIES返回排序与最后一行并列的行。 如:获取成绩前三名的同学 select top 3 * from Score order by Num desc --不考虑成绩并列 select top 3 WITH TIES * from Score order by Num desc --可解决成绩并列的问题 5、选择列表中的计算值 选择的列不但可以包括数据表列,还可以包括计算值,这些结果集列被称为派生列。 计算并且包括以下运算: 对数值列或常量使用算术运算符或函数进行的计算和运算。如SUM(),

Sql练习答案,sql常用实例一写就会

------1.列出至少有一个员工的所有部门。 select count(*),deptno from emp group by deptno having count(*)>1; ------2.列出薪金比“SMITH”多的所有员工。 select * from emp where sal>(select sal from emp where ename='SMITH'); ------3.列出所有员工的姓名及其直接上级的姓名。 select ename,(select ename from emp where empno=a.mgr) from emp a; select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+); ------4.列出受雇日期晚于其直接上级的所有员工。 select ename from emp a where hiredate>(select hiredate from emp where empno=a.mgr); 列出受雇日期早于其直接上级的所有员工。 select ename from emp a where hiredate<(select hiredate from emp where empno=a.mgr); ------5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。 select dname,ename from dept left join emp on dept.deptno=emp.deptno; select dname,ename from dept a,emp b where a.deptno = b.deptno(+); ------6.列出所有“CLERK”(办事员)的姓名及其部门名称。 select dname,ename from dept a,emp b where a.deptno=b.deptno and job='CLERK'; select (select dname from dept where deptno=a.deptno) as dname ,ename from emp a where job='CLERK'; ------7.列出最低薪金大于1500的各种工作。 select job,min(sal) msal from emp group by job having min(sal)>1500; ------8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。select ename from emp where deptno=(select deptno from dept where dname='SALES');

SQL函数用法大全

SQL 是用于访问和处理数据库的标准的计算机语言。 什么是SQL? ?SQL 指结构化查询语言 ?SQL 使我们有能力访问数据库 ?SQL 是一种ANSI 的标准计算机语言 编者注:ANSI,美国国家标准化组织 SQL 能做什么? ?SQL 面向数据库执行查询 ?SQL 可从数据库取回数据 ?SQL 可在数据库中插入新的纪录 ?SQL 可更新数据库中的数据 ?SQL 可从数据库删除记录 ?SQL 可创建新数据库 ?SQL 可在数据库中创建新表 ?SQL 可在数据库中创建存储过程 ?SQL 可在数据库中创建视图 ?SQL 可以设置表、存储过程和视图的权限 SQL 是一种标准- 但是... SQL 是一门ANSI 的标准计算机语言,用来访问和操作数据库系统。SQL 语句用于取回和更新数据库中的数据。SQL 可与数据库程序协同工作,比如MS Access、DB2、Informix、MS SQL Server、Oracle、Sybase 以及其他数据库系统。 不幸地是,存在着很多不同版本的SQL 语言,但是为了与ANSI 标准相兼容,它们必须以相似的方式共同地来支持一些主要的关键词(比如SELECT、UPDATE、DELETE、INSERT、WHERE 等等)。 注释:除了SQL 标准之外,大部分SQL 数据库程序都拥有它们自己的私有扩展! 在您的网站中使用SQL 要创建发布数据库中数据的网站,您需要以下要素: ?RDBMS 数据库程序(比如MS Access, SQL Server, MySQL) ?服务器端脚本语言(比如PHP 或ASP) ?SQL ?HTML / CSS RDBMS

第1节-基本的SQL-SELECT语句

基本的SQL-SELECT语句 1.查看表结构 sql>desc 表名称; 2.查询所有列 select * from 表名称; 3. 查询指定列 select 列名称1 ,列名称2 ,列名称3 from 表名称; 4.如何取消重复行 select distinct 列名称1 ,列名称2 from 表名称; 5. 使用算数表达式 ?显示每个雇员的年工资 select sal*13+nvl(comm,0)*13 "年工资",ename,comm from emp; 6. 使用列的别名 select ename "姓名",sal*12 as "年收入" from emp; 7.如何处理null值(空值不是空格或者0) 使用nvl函数来处理 8. 如何连接字符串(||) selectename || 'is a ' || job from emp; SQL语句分为以下三种类型: ●DML: Data Manipulation Language 数据操纵语言 ●DDL: Data Definition Language 数据定义语言 ●DCL: Data Control Language 数据控制语言

DML用于查询与修改数据记录,包括如下SQL语句: ●INSERT:添加数据到数据库中 ●UPDATE:修改数据库中的数据 ●DELETE:删除数据库中的数据 ●SELECT:选择(查询)数据 SELECT是SQL语言的基础,最为重要。 DDL用于定义数据库的结构,比如创建、修改或删除数据库对象,包括如下SQL语句: ●CREATE TABLE:创建数据库表 ●ALTER TABLE:更改表结构、添加、删除、修改列长度 ●DROP TABLE:删除表 ●CREATE INDEX:在表上建立索引 ●DROP INDEX:删除索引 DCL用来控制数据库的访问,包括如下SQL语句: ●GRANT:授予访问权限 ●REVOKE:撤销访问权限 ●COMMIT:提交事务处理 ●ROLLBACK:事务处理回退 ●SAVEPOINT:设置保存点 ●LOCK:对数据库的特定部分进行锁定

Select 查询语句

Select 查询语句 语法:SELECT [ALL|DISTINCT] <目标列表达式> [AS 列名] [,<目标列表达式> [AS 列名] ...] FROM <表名> [,<表名>…] [WHERE <条件表达式> [AND|OR <条件表达式>...] [GROUP BY 列名[HA VING <条件表达式>> [ORDER BY 列名[ASC | DESC> 解释:[ALL|DISTINCT] ALL:全部;DISTINCT:不包括重复行 <目标列表达式> 对字段可使用A VG、COUNT、SUM、MIN、MAX、运算符等 <条件表达式> 查询条件谓词 比较=、>,<,>=,<=,!=,<>, 确定范围BETWEEN AND、NOT BETWEEN AND 确定集合IN、NOT IN 字符匹配LIKE(“%”匹配任何长度,“_”匹配一个字符)、NOT LIKE 空值IS NULL、IS NOT NULL 子查询ANY、ALL、EXISTS 集合查询UNION(并)、INTERSECT(交)、MINUS(差) 多重条件AND、OR、NOT 对查询结果分组 [HA VING <条件表达式>] 分组筛选条件 [ORDER BY 列名[ASC | DESC> 对查询结果排序;ASC:升序DESC:降序 例1:select student.sno as 学号, https://www.wendangku.net/doc/6910716548.html, as 姓名, course as 课程名, score as 成绩from score,student where student.sid=score.sid and score.sid=:sid 例2:select student.sno as 学号, https://www.wendangku.net/doc/6910716548.html, as 姓名,A VG(score) as 平均分from score,student where student.sid=score.sid and student.class=:class and (term=5 or term=6) group by student.sno, https://www.wendangku.net/doc/6910716548.html, having count(*)>0 order by 平均分DESC 例3:select * from score where sid like '9634' 例4:select * from student where class in (select class from student where name='陈小小')

SQL查询语句例子

数据表的查询(select) select 字段列表[as 别名], * from 数据表名 [where 条件语句] [group by 分组字段] [order by 排序字段列表desc] [LIMIT startrow,rownumber] 1、Select 字段列表From 数据表 例:①、select id,gsmc,add,tel from haf (* 表示数据表中所有字段) ②、select 单价,数量,单价*数量as 合计金额from haf (As 设置字段的别名) 2、Select …from …Where 筛选条件式 筛选条件式:①、字符串数据:select * from 成绩单Where 姓名='李明' ②、万用字符:select * from 成绩单Where 姓名like '李%' select * from 成绩单Where 姓名like '%李%' select * from 成绩单Where 姓名like '%李_' ③、特殊的条件式: ⑴= / > / < / <> / >= / <= ⑵AND(逻辑与) OR(逻辑或) NOT(逻辑非) ⑶Where 字段名称in(值一,值二) ⑷Where 字段名称Is Null / Where 字段名称Is Not Null 3、Select …from …group by 字段 SQL函数: SELECT sex,count(id) as women from `user` group by 'sex'; 函数名描述函数名描述 AVG平均值Count计数 MAX最大值MIN最小值 Sum求和

实例学习SQL的Select命令

实例学习SQL的Select命令 1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值, --显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。 select emp_no ,emp_name ,dept , isnull(convert(char(10),birthday,120),'日期不详') birthday from employee order by dept --2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称 select emp_no,emp_name,dept,title from employee where emp_name<>'喻自强' and dept in (select dept from employee where emp_name='喻自强') --3、按部门进行汇总,统计每个部门的总工资 select dept,sum(salary) from employee group by dept --4、查找商品名称为14寸显示器商品的销售情况, --显示该商品的编号、销售数量、单价和金额 select a.prod_id,qty,unit_price,unit_price*qty totprice from sale_item a,product b where a.prod_id=b.prod_id and prod_name='14寸显示器' --5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice from sale_item group by prod_id --6、使用convert函数按客户编号统计每个客户1996年的订单总金额select cust_id,sum(tot_amt) totprice from sales where convert(char(4),order_date,120)='1996' group by cust_id --7、查找有销售记录的客户编号、名称和订单总额 select a.cust_id,cust_name,sum(tot_amt) totprice from customer a,sales b where a.cust_id=b.cust_id group by a.cust_id,cust_name

SELECT语句练习题答案

SELECT 语句练习题 有下列四个表: 1、学生表 S_NO S_NAME S_SEX S_BIRTHDAY CLASS 108曾华男1905-5-2295033 105匡明男1905-5-1895031 107王丽女1905-5-795033 101李军男1905-5-995033 109王芳女1905-5-1895031 103陆君男1905-5-2095031 2、教师表 T_NO T_NAME TSEX T_BIRTHDAY PROF DEPART 804李诚男1958-12-2副教授计算机系856张旭男1969-3-12讲师电子工程系825王萍女1972-5-5助教计算机系831刘冰女1977-8-14助教电子工程系 3、课程表 C_NO C_NAME T_NO 3-105计算机导论825 3-245操作系统804 6-166数据电路856 9-888高等数学100 4、成绩表 S_NO C_NO DEGREE 1033-24586 1053-24575 1093-24568 1033-10592 1053-10588 1093-10576 1013-10564 1073-10591 1083-10578 1016-16685 1076-10679 1086-16681 Select 语句的最基本结构:Select …. From ….where order by:排序子句(ASC:升序,DESC:降序)

like:模式匹配(通配符:% 可以匹配任意类型和长度的字符; _ 任意单个字符。 聚合函数(SUM A VG、COUNT、COUNT(*)、MAX、MIN) GROUP BY:分组 1、查询Student表中的所有记录的S_NAME、S_SEX和Class列。 2、查询教师所有的单位即不重复的Depart列。 3、查询Student表的所有记录。 4、查询Score表中成绩在60到80之间的所有记录。 5、查询Score表中成绩为85,86或88的记录。 6、查询Student表中“95031”班或性别为“女”的同学记录。 7、以Class降序查询Student表的所有记录。 8、以C_NO升序、Degree降序查询Score表的所有记录。 9、查询“95031”班的学生人数。 10、查询Score表中的最高分的学生学号和课程号。 11、查询‘3-105’号课程的平均分。 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 13、查询最低分大于70,最高分小于90的S_NO列。 14、查询所有学生的S_NAME、C_NO和Degree列。 15、查询所有学生的S_NO、C_NAME和Degree列。 16、查询所有学生的S_NAME、C_NAME和Degree列。 17、查询“95033”班所选课程的平均分。 SQL语句练习题参考答案 1、select S_NAME,S_SEX,Class from Student; 2、select distinct depart from teacher; 3、select S_NO as '学号',S_NAME as '姓名',S_SEX as '性别',S_BIRTHDAY as'出生日期',Class as'班号'from student; 或 select S_NO as 学号,S_NAME as 姓名,S_SEX as 性别,S_BIRTHDAY as 出生日期,Class as 班号from student; 4、select * from score where degree between 60 and 80; 或select * from score where degree>=60 and degree<=80; 5、select * from score where degree in (85,86,88); 6、select * from student where class='95031'or S_SEX='女'; 7、select * from student order by class desc; 8、select * from score order by C_NO asc ,degree desc; 或select * from score order by C_NO ,degree desc; 9、select count(*) as CNT from student where class='95031'; 10、select S_NO as '学号',C_NO as '课程号', degree as '最高分' from score where degree=(select max(degree) from score) 11、select avg(degree)as 课程平均分from score where C_NO='3-105'; 12、select C_NO,avg(degree) from score where C_NO like'3%'group by C_NO having count(*) >5; 13、select S_NO from score group by S_NO having min(degree)>70 and max(degree)<90; 14、select student.S_NAME,score.C_NO,score.degree from student,score where student.S_NO=score.S_NO; 15、select x.S_NO,y.C_NAME,x.degree from score x,course y where x.C_NO=y.C_NO; 16、select x.S_NAME,y.C_NAME,z.degree from student x,course y,score z where x.S_NO=z.S_NO and z.C_NO=y.C_NO;

相关文档
相关文档 最新文档