文档库 最新最全的文档下载
当前位置:文档库 › SELECT语句高级用法

SELECT语句高级用法

SELECT语句高级用法
SELECT语句高级用法

SELECT语句高级用法1,

使用group by 子句

group by 子句将表分为几组,此子句通常与为每个这样的组生产总结值的聚集函数组合。使用不带聚集的group by 子句与在select 子句中使用的distinct(或unique)关键字很相似。select distinct customer_num from orders;

selecct customer_num from orders group by customer_num;

group by 子句将行收集到组中,因此每一组中的每一行具有相同的客户号。在没有选择任何其它列的情况下,结果是唯一的customer_num值的列表。

select order_num,count(*) number,sum(total_price) price from items group by 1 order by 3;

select o.order_num,sum(i.total_price) from order o,items i where o.order_date > '01/01/98'

and ocustomer_num = 110 and o.order_num = i.order_num group by o.order_num;

使用having子句

要完成group by 子句,使用having 子句来在构成组之后将一个或多个限制条件应用于这些组。having子句对组的影响类似于

where 子句限制个别行的方式,使用having子句的一个优点是可以在搜索条件中包括聚集,而在where子句的搜索条件中却不能包括聚集。

每个having条件将组的一列或一个聚集表达式与组的另一个聚集表达式或与常量作比较。可以使用having来对列值或组列表中的聚集值设置条件。

下面的语句返回具有两个商品以上的订单上每个商品的平均总价格。having子句在每个构成的测试组,并选择由两行以上构成的那些组。

select order_num,count(*) number,avg(total_price) average from items group by order_num having count(*) > 2;

如果不带group by 子句使用having子句,则该having条件适应雨满足搜索条件的所有行。换言之,满足搜索条件的所有行构成一个组。

select avg(total_price) average from items having count(*) > 2;

select o.order_num,sum(i.total_price) price, paid_date - order_date span from orders o,items i where o.order_date > '01/01/98'

and o.customer_num > 110

and o.order_num = i.order_num

group by 1,3

having count(*) < 5

order by 3

into temp temptab1;

创建高级连接

自连接

连接不一定都是涉及两个不同的表。可以将表连接至它本身,缠结自连接。当想要将列中的值与同一列中的其它值进行比较时,将表连接

至它自身非常有用。

要创建自连接,在from 子句中列示表两次,并且每次为它指定不同的别名。与表之间的连接一样,可以在自连接中使用算术表达式。

可以测试空值,可以使用order by 子句来以升序或将序对指定列中的值进行排序。

select x.order_num,x.ship_weight,x,ship_date,y.order_num,y.ship_weight,y.ship_date from order x,order y

where x.ship_weight >= 5*y.ship_date

and x.ship_date is not null

and y.ship_date is not null

order by x.ship_date;

如果想要将自连接的结果存储到临时表中,则将Into temp子句追加到select语句中,并至少对一组列指定显示标号,以重命名这些列。否则,

重复列名将导致错误,并且不会创建临时表。

select x.order_num orders1,x.po_num purch1,

x.ship_date ship1,y.order_num orders2,

y.po_num purch2,y.ship_date ship2

from order x,orders y

where x.ship_weight >= 5*y.ship_weight

and x.ship_date is not null

and y.ship_date is not null

order by orders1,orders2

into temp shipping;

自连接子句三次

select s1.manu_code,s2.manu_code,s3.manu_code,

s1.stock_num,s1.descripton

from stock s1,stock s2,stock s3

where s1.stock_num = s2.stock_num

and s2.stock_num = s3.stock_num

and s1.manu_code < s2.manu_code

and s2.manu_code < s3.manu_code

order by stock_num;

如果想要从payroll表选择行来确定那些职员的薪水高于他们的经理,按照下面select 语句所示来构造自连接:

select emp.employee_num,emp.gross_pay,emp.level,

emp.detp_num,mgr.employee_num,mgr.gross_pay,

mgr.dept_num,mgr.level

from payroll emp,payroll mgr

where emp.gross_pay > mgr.gross_pay

and emp.level < mgr.level

and emp.dept_num = mgr.dept_num

order by 4;

使用相关子查询来检索并列示预定的10种价格最高的商品

select order_num,total_price

from items a

where 10 >

(select count(*)

from items b

where b.total_price < a.total_price )

order by total_price;

外连接

外连接使其中一个表成为控制表(也成为外部表),控制其它从属表(也成为内部表)。

在内连接或简单连接中,结果只保护满足连接条件的行组合,废弃部满足连接条件的行。在外连接中,结果包含满足连接条件的行与控制表中的行(如果在从属表中找不到匹配的行将废弃这些行)的组合。

在从属表中没有相匹配的行的控制表在从属表选择的列中包含null值。

外连接允许在应用连接条件之前将连接过虑器应用于内部表。

ANSI外连接语法用left join,left outer join,right join和right outer join关键字开始外连接。outer 关键字是可选的。

查询可在on子句中指定连接条件和可选连接过虑器。where 子句指定后连接过虑器。

左外连接

在左外连接的语法中,外连接的控制表显示在开始外连接的关键字的左边。左外连接返回连接条件为true的所有行,除此之外,

还返回控制表中的所有其它行并将从属表中的相应值显示为Null。

select c.customer_num,c.lname,https://www.wendangku.net/doc/6e4245356.html,pany,c.phone,u.call_dtime,u.call_descr

from customer c left outer join cust_calls u on c.customer_num = u.customer_num;

select c.customer_num,c.lname,https://www.wendangku.net/doc/6e4245356.html,pany,c.phone,u.call_dtime,u.call_descr

from customer c left outer join cust_calls u

on c.customer_num = u.customer_num

where u.customer_num is null;

右外连接

在右外连接的语法中,外连接的控制表显示在开始外连接的关键字右边,右外连接返回连接条件为true的所有行,除此之外,

还返回控制表中的所有其它行并将从属表中的相应值显示为null

select c.customer_num,c.fname,c.lname,o.order_num,o.order_date,o.customer_num

from customer c right outer join orders o on (c.customer_num = o.customer_num);

简单连接

select c.customer_num,c.lname,https://www.wendangku.net/doc/6e4245356.html,pany,c.phone,u.call_dtime,u.call_descr from customer c,cust_calls u

where c.customer_num = u.customer_num;

对两个表的简单外连接

select c.customer_num,c.lname,https://www.wendangku.net/doc/6e4245356.html,pany,c.phone,u.call_dtime,u.call_descr

from customer c,outer cust_calls u where c.customer_num = u.customer_num;

cust_calls表前面的附加关键字outer使该表成为从属表。外连接导致查询返回有关所有客户的信息,而不管他们是否已经致电客户

服务中心,检索控制表customer的所有行,并且将Null值指定给从属表cust_calls的列。

与第三个表进行简单连接的外连接

这种外连接也称为嵌套简单连接

select c.customer_num,c.lname,o.order_num,i.stock_num,i.manu_code,i.quantity

from customer c, left outer join (orders o,items i)

where c.customer_num = o.customer_num

and o.order_num = i.order_num

and manu_code in ('KAR','SHM')

ORDER BY lanme;

将两个表与第三个表进行外连接

作为两个表中的每一个与第三个表的外连接的结果的外连接。在此第三中类型的外连接中,连接关系可能仅仅使与

从属表之间的关系。

select c.customer_num,c.lname,o.order_num,order_date,call_dtime

from customer c,outer orders o,outer cust_calls x

where c.customer_num = o.customer_num

and c.customer_num = x.customer_num

order by lname

into temp service;

组合外连接的连接

要实现多级嵌套,可以创建使用三种外连接类型的任何组合的连接。使用ansi语法,创建作为对两个表与另一个外连接的简单外连接

组合结果的连接。

select c.customer_num,c.lname,o.order_num,stock_num,manu_code,quantity

from customer c,outer (orders o,outer items i)

where c.customer_num = o.customer_num

and o.order_num = i.order_num

and manu_code in ('KAR','SHM')

ORDER BY lname;

select 语句中的子查询

下列情况定义数据库服务器支持子查询类型:

1,嵌套在另一个select 语句的投影列表中的select语句

2,嵌套在另一个select语句(或insert,delete或update语句中)的where子句中的select 语句。

相关子查询

相关子查询使引用不在其from子句中的列或表的子查询。该列可以在projection子句或在where子句中。要查找查询引用的表,搜索不相关的列

直到找到相关为止。

projection子句中的子查询

子查询可以在另一个select语句的投影列表中发生。

select customer.customer_num,

(select sum(ship_charge)

from orders

where customer.customer_num = orders.customer_num)

as total_ship_chg

from customer;

where 子句中的子查询

下来关键子在select语句的where子句中引入子查询

all

any

in

exists

使用all

在子查询前面使用关键字all来确定对返回的每个值的比较是否为true.如果字查询不返回任何值,则搜索条件为true。

(如果字查询不返回任何值,则对于所有零值条件为true)

select order_num,stock_num,manu_code,total_price

from items

where total_price < all

(select total_price from items

where order_num = 1023);

使用any

在子查询前使用关键字any(或其同义词some)来确定是否对至少一个返回值的比较为true.如果子查询不返回任何值,则搜索

条件为false.(因为没有值存在,所以对于其中一个值条件不能为true)

select distinct order_num from items where total_price > any

(select total_price from items where order_num = 1005);

单值子查询

如果你指定子查询可能对外部级别查询返回刚好一个值,则不需要包括关键字all或any.可如同对待函数一样对待函数一样对待

只返回一个值的子查询。这种子查询通常使用聚集函数,原因是聚集函数总是返回单个值。

select order_num from items where stock_num = 9 and quantity =

(select max(quantity) from items where stock_num = 9);

相关子查询

select po_num,ship_date from orders main where 10>

( select count (idstinct ship_date) from orders sub where sub.ship_date < main.ship_date) and ship_date is not null

order by ship_date,po_num;

使用exists

关键字exists也称为存在限定符,原因是仅当外部select找到至少一行时,子查询才为true. select unique manu_name,lead_time from manufact where exists

(select * from stock where description mantches '*shoe*'

and manufact.manu_code = stock.manu_code);

集合运算

标准集合运算联合,相交和差异允许你出来数据库信息。这三种运算允许你使用select语句在执行更新,

插入或删除之后检查数据库的完整性。

联合

联合运算使用union关键字或运算符来将两个查询组合成单个符合查询。可以在两个或多个select语句之间使用

union运算符来联合它们,并产生一个临时表,它包含存在于任何一个原始表或所有原始表中的行。还可以在视图

的定义中使用union运算符。

例子:

select distinct stock_num,manu_code from stock where unit_price < 25.00 union

select stock_num,manu_code

from items where quantity > 3;

select distinct stock_num,manu_code from stock where unit_price < 25.00 union

select stock_num,manu_code from items where quantity > 3 order by 2;

将order by 与union 配合使用

select distinct stock_num,manu_code from stock where unit_price < 25.

union select stock_num,manu_code from items where quantity > 3 order by 2;

使用union all

缺省情况下,union关键字排除重复的行。要保留重复的行,添加可选的关键字all

select stock_num,manu_code from stock where unit_price < 25.00

union all select stock_num,manu_code from items where quantity >3 order by 2 into temp stock item;

使用不同的列名

select distinct state from customer where customer_num between 120 and 15

union select distinct code from state where sname matches '*a';

将union与多个表配合使用

select stock_num,manu_code from stock where unit_price > 600.00 union all

select stock_num,manu_code from ctalog where catalog_num = 10025

union all select stock_num,manu_code from items where quantity = 10 order by 2;

相交

两个行集的相交产生一个表,它包含同时存在于两个原始表的行。使用关键字exists或in 来引入显示两个集合相交的子查询

select stock_num,manu_code,unit_price from stock where stock_num in ( select stock_num from items)

order by stock_num;

差异

两个行集之间的差异产生一个表,它波包含在第一个行集中但不在第二个行集中的行,使用关键字not exists或not in 来

引入两个集合之间的差异的子查询。

select stock_num,manu_code,unit_price from stock where stock_num not in ( select stock_num from items)

order by stock_num;

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 ---------- ---------- --------- ---------- -------------- ---------- ----------

oracle的Select语句

oracle的Select语句 select 查询的一般格式是 select {[distinct|all] columns | *} //1 选择列 from {tables | views | other select} //2 数据来源 where conditions //3 选择条件 group by columns //4 分组 having conditions //5 选择 order by columns //6 排序 一、选择列 1. select后面出现的内容可以是from后面的数据来源中的列,也可以是*,也可以是常量或者T-SQL函数。 2. 如果列名很长,或者多个表的列名有冲突,可以使用as来定义别名。 二、数据来源 1. 数据来源可以是表,视图,还可以是其他的select语句(即,行集)。 2. from子句中可以包含连接说明,即inner join,outer join这样的内容。这个内容参见下面的内容。 3. 可以在from子句中为表,视图,或者其他select语句的结果指定别名,但是不要用as。 三、where子句 1. 多个条件之间可以用and 或者or连接。 2. null值查询要使用is null,或者is not null,而不是=null或者<>null 3. like是进行模式匹配的一种方式,列的数据类型可以是任何字符或者日期数据。它的否定形式是not like。%和_是通配符,一个表示0或多个任意字符,一个表示一个任意字符。但是这两个字符如果不出现在like后面的模式中,就是两个普通字符。 4. text列的where条件只能是like,is null,patindex。 5. 如果要在like中匹配普通字符%和_,可以使用escape定义一个转义字符,这个转义字符可以随意指定。然后将这个转义字符放在一个通配符或者单引号之前,就表示这个通配符或者单引号是一个普通的字符。 6. in ,not in,between and,not between and.“between a and b”将会包括a和b在内。in可以转换为一个连接,但是not in不能。 7. where exists R.当且仅当R非空时,条件exists R为真。其否定形式是where not exists R. 8. where s <| >| = | <>| >=|<= all/any R.否定形式是在s前对整个表达式加not. 四、连接查询 两张表的连接可以用from子句的ansi连接语法或者where子句中的sql连接语法实现。ansi 连接语法格式为From table1 join_type table2 on(conditions) join_type table3 on(conditions)。 连接有等值连接,笛卡尔积(交叉连接),自然连接,theta连接,外部链接五种。 1 等值连接 select students.* , stu_course.* from students inner join stu_course on(students.id = stu_course.studentid)--31条记录,stu_course的记录数目

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/6e4245356.html,O=https://www.wendangku.net/doc/6e4245356.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/6e4245356.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/6e4245356.html,o=https://www.wendangku.net/doc/6e4245356.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/6e4245356.html,o=https://www.wendangku.net/doc/6e4245356.html,o) 2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。 (1)--统计有学生选修的课程门数-- select count(distinct https://www.wendangku.net/doc/6e4245356.html,o)from sc; (2)--查询选修4号课程的学生的平均年龄-- select avg(s.age) from s,sc where s.sno=sc.sno and cno='4';

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;

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 值不是空的记录

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;

SQL语句

题型:SQL语句 题目3.1:设教学数据库中有4个关系: 教师关系 T(T#,TNAME,TITLE) 课程关系 C(C#,CNAME,T#) 学生关系 S(S#,SNAME,AGE,SEX) 选课关系 SC(S#,C#,SCORE) 试用SQL查询语句表示下列查询。 ①检索年龄小于17岁女生的学号和姓名。 ②检索男生所学课程的课程号和课程名。 ③检索男生所学课程的任课老师的工号和姓名。 ④检索至少选修两门课程的学生学号。 ⑤检索至少有学号为S2和S4学生选修的课程的课程号。 ⑥检索WANG同学不学的课程的课程号。 ⑦检索全部学生都选修的课程的课程号与课程名。 ⑧检索选修课程包含LIU老师所授全部课程的学生学号。 答案:①SELECT S#,SNAME FROM S WHERE AGE<17 AND SEX=’F’; ②SELECT C.C#,CNAME FROM S,SC,C WHERE S.S#=SC.S# AND SC.C#=C.C# AND SEX=’M’; ③SELECT T.T#’,TNAME FROM S,SC,C,T WHERE S.S#=SC.S# AND SC.C#=C.C# AND C.T#=T.T# AND SEX=’M’; ④SELECT DISTINCT X.S# FROM SC AS X,SC AS Y WHERE X.S#=Y.S# AND X.C#!=Y.C#; ⑤SELECT DISTINCT X.C# FROM SC AS X,SC AS Y WHERE X.S#=S2 AND Y.S#=S4 AND X.C#=Y.C#; ⑥SELECT C# FROM C WHERE NOT EXISTS(SELECT * FROM S,SC WHERE S.S#=SC.S# AND SC.C#=C.C# AND SNAME=’WANG’); ⑦SELECT C#,CNAME FROM C WHERE NOT EXISTS(SELECT * FROM S WHERE NOT EXISTS(SELECT * FROM SC WHERE S#=S.S# AND C#=C.C#)); ⑧SELECT DISTINCT S# FROM SC AS X

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 类型的语句,有注意到条件的前后顺序吗?我今天做个小实验。 比如查询地址里包含“海口市”及“振兴路” 两个关键字的数据,一般时候可能会用

sql中select语句详解及用途

sql中select语句详解及用途1 2 3 4 5 6 7 8 9 1 0 1 1 SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expression [ AS output_name ] [, ...] [ FROM from_item [, ...] ] [ WHERE condition ] [ GROUP BY expression [, ...] ] [ HAVING condition [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ] [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ] [ FOR UPDATE [ OF tablename [, ...] ] ] [ LIMIT { count | ALL } ] [ OFFSET start ] 这里 from_item 可以是: 1 2 3 4 5 6 7 8 [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias_list ) ] ] | ( select ) [ AS ] alias [ ( column_alias_list ) ] | from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column_list ) ] 输入 ? expression 表的列/字段名或一个表达式. output_name 使用 AS 子句为一个列/字段或一个表达式声明另一个名称.这个名称主要用于标记输出列用于显示。它可以在 ORDER BY 和 GROUP BY 子句里代表列/字段的值.但是 output_name 不能用于 WHERE 或 HAVING 子句;用表达式代替. from_item 一个表引用,子查询,或者 JOIN 子句.详见下文. condition 一个布尔表达式,给出真或假的结果.参见下面描述的 WHERE 和 HAVING 子句.

常用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(),

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/6e4245356.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/6e4245356.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/6e4245356.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练习答案,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');

相关文档