sql查询语句常用例子
1、查找与jams在同一个单位的员工姓名、性别、部门和职称:
select emp_no, emp_name, dept, title from employee where emp_name<>'jams' and dept in (select dept from employee where emp_name=' jams'')
2、按部门进行汇总,统计每个部门的总工资
select dept, sum(salary) from employee group by dept
3、查找员工的编号、姓名、部门和出生日期,并按部门排序输出
select emp_no ,emp_name ,dept , birthday from employee order by dept
4、查找商品名称为三星笔记本商品的销售情况,显示该商品的编号、销售数量、单价和金额
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='三星笔记本'
5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额
select prod_id, sum(qty) totqty, sum(qty*unit_price) totprice from sale_item group by prod_id
6、使用convert函数按客户编号统计每个客户2011年的订单总金额
select cust_id, sum(tot_amt) totprice from sales where convert(char(4),order_date,120)='2011' 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
8、查找在2012年中有销售记录的客户编号、名称和订单总额
select a.cust_id,cust_name,sum(tot_amt) totprice from customer a,sales b where a.cust_id=b.cust_id and convert(char(4),order_date,120)='2012' group by a.cust_id,cust_name
9、查找一次销售额最大的销售记录
select order_no, cust_id, sale_id, tot_amt from sales where tot_amt= (select max(tot_amt) from sales)
10、查找至少有5次销售的业务员名单和销售日期
select emp_name,order_date from employee a,sales b where emp_no=sale_id and a.emp_no in (select sale_id from sales group by sale_id having count(*)>=5)
order by emp_name
11、用存在量词查找没有订货记录的客户名称
select cust_name from customer a where not exists (select * from sales b where a.cust_id=b.cust_id)
12、 求各部门的平均薪水,要求按平均薪水从小到大排序
select dept, avg(salary) from employee group by dept order by avg(salary)
13、 查询总订购金额超过’D0011’客户的总订购金额的客户号,客户名及其住址
Select cust_id, cust_name,addr From customer Where cust_id in (select cust_id from sales Group by cust_id Having sum(tot_amt)>
(Select sum(tot_amt) from sales where cust_id='D0011'))
14、 查询没有承接业务的员工的信息
Select * From employee a Where not exists (select * from sales b where a.emp_no=b.sale_id)
15、 查询订购的产品至少包含了订单20004中所订购产品的订单
Select distinct order_no From sale_item a Where order_no<>'20004' and not exists ( Select * from sale_item b where order_no ='20004' and not exists
(select * from sale_item c where c.order_no=a.order_no and c.prod_id=b.prod_id))
16、 查询订购了4种以上产品的订单号。
Select order_no from sale_item Group by order_no Having count(*)>4
17、 求每位客户订购的每种产品的总数量及平均单价,并按客户号,产品号从小到大排列。
Select cust_id,prod_id,sum(qty),sum(qty*unit_price)/sum(qty) From sales a, sale_item b Where a.order_no=b.order_no Group by cust_id,prod_id
Order by cust_id,prod_id
18、 求出每位客户的总订购金额,显示出客户号及总订购金额,并按总订购金额降序排列。
Select cust_id,sum(tot_amt) from sales Group by cust_id Order by sum(tot_amt) desc
19、 找出公司女业务员所接且订单金额超过80000元的订单号及订单金额。
Select order_no,tot_amt From sales ,employee Where sale_id=emp_no and sex='F' and tot_amt>80000
20、在employee表中查询薪水超过员工平均薪水的员工信息。
Select * from employee where salary>(select avg(salary) from employee)
21、将技术部员工的薪水上调5%。
update employee set salary=salary*1.05 where dept='技术部'
22、计算出一共销售了几种产品。
select count(distinct prod_id) as '共销售产品数' from sale_item
23、选取编号界于‘A0001’和‘B0004’的客户编号、客户名称、客户地址。
select CUST_ID, cust_name, addr from customer where cust_id between 'A0001' AND 'B0004'
24、将表中住址为"上海市"的员工住址改为"北京市"
update employee set addr='北京市' where addr like '上海市'
25、查询表中的同一部门的职工的平均工资,但只查询"住址"是"北京市"的员工
select avg(salary) avg_sal,dept from employee where addr like '北京市%' group by dept
26、执行带参数的存储过程
EXEC sp2_F_QueueConfirmCustomerInsert @OrderID ='343235365'
27、统计两个时间差值
SELECT datediff(s,a.createtime,b.createtime) as '耗时(s)',a.orderid,b.createtime,a.createtime FROM db..AutomationIssueRequest a,db..AutomationInStock b where a.orderid=b.orderid and b.createtime >'2013-01-10 15:56:20.000'
28、查看存储过程源码
EXEC sp_helptext sp_QueueCustomerInsert
29、查询订单量在40--50间的用户名
select count(distinct orderid),uid from Orders group by uid having count(distinct orderid) between 40 and 50
30、只查询显示前50个订单
Select top 50 orderid from Orders
sql查询语句常用例子的更多相关文章
- SQL查询语句 常用示例
SQL语言的应用 1. 找出姓李的读者姓名和所在单位. 2. 列出图书库中所有藏书的书名及出版单位. 3. 查找高等教育出版社的 所有图书及单价,结果按单价降序排序. 4. ...
- WordPress 常用数据库SQL查询语句大全
在使用WordPress的过程中,我们少不了要对数据库进行修改操作,比如,更换域名.修改附件目录.批量修改文章内容等等.这个时候,使用SQL查询语句可以大大简化我们的工作量. 关于如何操作SQL查询语 ...
- [转] 常用SQL查询语句
sunada 的原文地址 常用SQL查询语句 一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指 ...
- (转)经典SQL查询语句大全
(转)经典SQL查询语句大全 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql s ...
- 经典SQL查询语句大全
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
- SQL查询语句大全及其理解
转自:https://www.cnblogs.com/1234abcd/p/5530314.html 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删 ...
- SQL查询语句大全集锦
SQL查询语句大全集锦 一. 简单查询 简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的 表或视图.以及搜索条件等. 例如,下面的语句查询t ...
- MySQL数据库详解(一)执行SQL查询语句时,其底层到底经历了什么?
一条SQL查询语句是如何执行的? 前言 大家好,我是WZY,今天我们学习下MySQL的基础框架,看一件事千万不要直接陷入细节里,你应该先鸟瞰其全貌,这样能够帮助你从高维度理解问题.同样,对于MyS ...
- 15个初学者必看的基础SQL查询语句
本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 本文将分享15个初学者必看的基础SQL查询语句,都很基础,但是你不一定都会,所以好好看看吧. 1.创建表和数据插 ...
随机推荐
- linux环境搭建记录
第一次搭建环境,部署服务,在此记录一下过程 1.项目用到的hosts设置好 2.mkdir data,在data文件夹下建server,log,soft,resource路径,上载jdk.zip到so ...
- 开源中文检索引擎Coreseek简单使用
Coreseek结合MySQL使用简单示例,如下所示: echo 北京 | iconv -f gbk -t utf-8 | search -c D:\web\coreseek\etc\csft_mys ...
- ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)
Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...
- LG3835 【模板】可持久化平衡树
题意 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作(对于各个以往的历史版本): 插入x数 删除x数(若有多个相同的数,因只删除一个,如果没有请忽略该操作) 查询x数的排名 ...
- new/delete和malloc/free区别
相同点: 都可用于申请动态内存和释放内存 不同点: 操作对象有所不同. 本质区别: malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符,对象在创建的同时要自动 ...
- Protel 99 铺铜的一个坑 Pour Over Same
Protel 99 铺铜的一个坑 Pour Over Same 好久没用 Protel 99 了,修改了一个旧的 PCB 文件. 需要修改线路,由于改了线路需要重新铺铜,得重新画铺铜的边框. 以下这个 ...
- 将SQLite移植到ARM板上 (转)
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它, 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够 ...
- centos下svn的主要常用命令(解决商城系统添加的文件无法自动更新至svn服务器)
问题描述: 在商城中通过网页上传的png文件无法自动添加到版本库中. 查找过程: 通过程序分析,增加的主要是数据文件,主要分布在data目录中. svn list /home/ggg --depth= ...
- 洛谷1527(bzoj2738)矩阵乘法——二维树状数组+整体二分
题目:https://www.luogu.org/problemnew/show/P1527 不难想到(?)可以用二维树状数组.但维护什么?怎么查询是难点. 因为求第k小,可以考虑记权值树状数组,把比 ...
- jdk1.8新特性之接口default方法
众所周知,default是java的关键字之一,使用场景是配合switch关键字用于条件分支的默认项.但自从java的jdk1.8横空出世以后,它就被赋予了另一项很酷的能力——在接口中定义非抽象方法. ...