㈠ sql查询语句大全
一、基础
1、说明:创建数据库
Create
DATABASE
database-name
2、说明:删除数据库
drop
database
dbname
3、说明:备份sql
server
---
创建
备份数据的
device
USE
master
EXEC
sp_admpdevice
'disk',
'testBack',
'c:\mssql7backup\MyNwind_1.dat'
---
开始
备份
BACKUP
DATABASE
pubs
TO
testBack
4、说明:创建新表
create
table
tabname(col1
type1
[not
null]
[primary
key],col2
type2
[not
null],..)
根据已有的表创建新表:
A:create
table
tab_new
like
tab_old
(使用旧表创建新表)
B:create
table
tab_new
as
select
col1,col2…
from
tab_old
definition
only
5、说明:删除新表
drop
table
tabname
6、说明:增加一个列
Alter
table
tabname
add
column
col
type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的
是增加varchar类型的长度。
7、说明:添加主键:
Alter
table
tabname
add
primary
key(col)
说明:删除主键:
Alter
table
tabname
drop
primary
key(col)
8、说明:创建索引:create
[unique]
index
idxname
on
tabname(col….)
删除索引:drop
index
idxname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:创建视图:create
view
viewname
as
select
statement
删除视图:drop
view
viewname
10、说明:几个简单的基本的sql语句
选择:select
*
from
table1
where
范围
**:insert
into
table1(field1,field2)
values(value1,value2)
删除:delete
from
table1
where
范围
更新:update
table1
set
field1=value1
where
范围
查找:select
*
from
table1
where
field1
like
’%value1%’
---like的语
法很精妙,查资料!
排序:select
*
from
table1
order
by
field1,field2
[desc]
总数:select
count
as
totalcount
from
table1
求和:select
sum(field1)
as
sumvalue
from
table1
平均:select
avg(field1)
as
avgvalue
from
table1
最大:select
max(field1)
as
maxvalue
from
table1
最小:select
min(field1)
as
minvalue
from
table1
㈡ sql数据库查询语句例子
各位同学们好,我们今天继续来说,sql数据库。我们今天的文档中主要介绍了SQL查询语句、查询语句示例等。这里我给大家做了一个总结,有需要的同学可以参考一下。
首先,我们来了解一下SQL数据库组成基本:
查找Movies表里的Title字段:
查找ID小于5的电影的Title和Director的以下这些:
查看电影的总条数
以下查询语句是常用于班级统计的:
查询“001”课程比“002”课程成绩高的所有学生的学号:
查询平均成绩大于60分的同学的学号和平均成绩:
查询所有同学的学号、姓名、选课数、总成绩:
查询姓“李”的老师的个数:
查询没学过“叶平”老师课的同学的学号、姓名:
查询学过“001”并且也学过编号“002”课程的同学的学号、姓名:
查询学过“叶平”老师所教的所有课的同学的学号、姓名:
查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名:
查询所有课程成绩小于60分的同学的学号、姓名:
查询没有学全所有课的同学的学号、姓名:
好了,这就是关于sql数据库查询语句的基础了。我都替你们整理好了,感兴趣的同学收藏一来,需要用的时候直接拿起来用就可以了。我们今天就到这里,下期再见!
本篇文章使用以下硬件型号:联想小新Air15;系统版本:win10;软件版本:sqlserver2008。
㈢ 数据库基础-SQL查询语句8条,帮忙写下。
1
select deptno,max(salary) from dbo.emp
2
select b.ename,a.dname,b.salary
from dbo.dept a inner join dbo.emp b on a.deptno=b.deptno
3
select b.ename,a.dname,b.salary
from dbo.dept a left join dbo.emp b on a.deptno=b.deptno
4
select b.ename,b.salary
from dbo.dept a inner join dbo.emp b on a.deptno=b.deptno and a.dname='开发部'
5
select a.ename,a.salary,b.ename,b.salary
from dbo.emp a left join dbo.emp b on a.mgr=b.empno
6
select esex,count(*) from dbo.emp group by esex
7
select * from dbo.emp where deptno between 2 and 4 union all
select * from dbo.emp where empno between 2 and 8
8
select empno,ename,deptno,salary into newemp from dbo.emp
㈣ SQL基础查询语句
SELECT cou.cno, tea.Tname, cs.cnt
FROM
(select cno as cno, count(sno) as cnt from sel group by cno) cs,
cou,tea,teacourse
WHERE
cou.cno = cs.cno
AND tea.Tno = teacourse.Tno
AND teacourse.cno = cou.cno
㈤ sql数据库基础语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
㈥ sql简单查询语句
1、首先打开数据库,建立好表。
㈦ 求三表联合查询的SQL查询语句
1、SQL语句:select u.*,r.*,r.id rid
from user u left join sys_user_role sur on u.id = sur.useridleft join sys_role r on sur.roleid = r.id
图片:(表名截图)
算了,建表语句也给你们了,你们自己测试,这样更详细,(程序员)多动手,比什么都好。(这里的 界面 对写代码不太友好,我放博客里了,自己复制粘贴测试使用就行)
sql语句地址:网页链接
2、SQL语句解释:
select a.*,b.*
from a表 a left join b表 b on a.id = b.aid
left join c表 c on b.cid = c.id
注2:此语句适合a表与c表连接,b表是关系表的情况。
㈧ sql基础查询语句
3:
select 语文,数学,英语,政治,物理,化学,sum(语文+数学+英语+政治+物理+化学) 总分, sum(语文+数学+英语+政治+物理+化学)/6 平均分 from 成绩表
where 姓名 = '卢良红'
4:
select sum(语文),sum(数学),sum(英语),sum(政治),sum(物理),sum(化学),sum(语文+数学+英语+政治+物理+化学) 总分 from 成绩表
5:
select avg(语文),avg(数学),avg(英语),avg(政治),avg(物理),avg(化学),sum(语文+数学+英语+政治+物理+化学)/6 平均分 from 成绩表
6:
select 学号,姓名,语文,数学,英语,政治,物理,化学,sum(语文+数学+英语+政治+物理+化学) 总分,sum(语文+数学+英语+政治+物理+化学)/6 平均分,成员,职务 from 成绩表,地址表,家庭表 where 成绩表.学号=地址表.学号 and 地址表.编号= 家庭表.编号 and 语文>90 and 地址 like '%东阳%'
㈨ SQL基础查询2
1、select sum(总数量) as '总数量' from sell where 商品编号=2
2、select * from employees where (姓名 like '李*')
3、select 商品名称 from goods where price between '2000' and '3000'
4、select 商品名称,进货价格 from goods order by 进货价格 desc
5、select * from goods order by 商品数量 asc ,进货价格 desc
6、select sum(人数) as '总人数' from employees where 部门='财务部'
7、select sum(人数) as '总人数' from employees group by 部门
8、select sum(人数) as '总人数' from employees group by 部门,性别
9、select sum(人数) as '总人数' from employees where 性别=‘男’group by 部门
10.select 商品名称,零售价 from goods where 生产厂商 in('惠普','联想','佳能')
11.select 姓名,电话号码 from employees where (电话号码 like '010?21*')
12. select 零售价格=零售价格*0.9 from goods
㈩ 怎么学习SQL语句
创建数据库
创建之前判断该数据库是否存在 if exists (select * from sysdatabases where name='databaseName') drop database 'databaseName' go Create DATABASE database-name
删除数据库
drop database dbname
备份sql server
--- 创建 备份数据的 device USE master EXEC sp_admpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始 备份 BACKUP DATABASE pubs TO testBack
创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only
删除新表
drop table tabname
增加一个列
Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
添加主键
Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col)
创建索引
create [unique] index idxname on tabname(col….) 删除索引:drop index idxname on tabname 注:索引是不可更改的,想更改必须删除重新建。
创建视图
create view viewname as select statement 删除视图:drop view viewname
几个简单的基本的sql语句
选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count(*) as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1[separator]
几个高级查询运算词
A: UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 B: EXCEPT 运算符 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 C: INTERSECT 运算符 INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。
使用外连接
A、left outer join: 左外连接(左连接):结果集既包括连接表的匹配行,也包括左连接表的所有行。 SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right outer join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full outer join: 全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。