问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

SQL数据库语句

发布网友 发布时间:2022-04-22 16:04

我来回答

1个回答

热心网友 时间:2022-04-08 05:56

/*--------------------------------------------------
--SQL Server 2012 & T-SQL Lesson 3 - BasicQueries
----------------------------------------------------*/
--Topic 1
SELECT <table fields list>
FROM <table names list>
WHERE <row constraints specification>
GROUP BY <grouping specification>
HAVING <grouping selection specification>
ORDER BY <order rules specification>
--Topic 2
use [AdventureWorks2012]
go
select Top 100 * from [Proction].[Proct]
SELECT *
FROM SALES.SALESORDERDETAIL
--Topic 3
select * from [Proction].[Proct]
select ProctID, Name, ProctNumber, Color, Size, ListPrice
from Proction.Proct
select ProctID, Name, ProctNumber, Color, Size, ListPrice
from Proction.Proct
order by listprice desc --desc=descending order ; asc=ascending order
select ProctID, Name, ProctNumber, Color, Size, ListPrice
from Proction.Proct
order by listprice desc,Name
select ProctID, Name, ProctNumber, Color, Size, ListPrice
from Proction.Proct
order by 2
--Topic 4
select ProctID, Name, ProctNumber, isnull(Color,''), isnull(Size,''), ListPrice
from Proction.Proct
--Topic 5
select ProctID, Name, ProctNumber,
isnull(Color,'') as Color, isnull(Size,'') as Size123, --using an alias
ListPrice
from Proction.Proct
select ProctID, Name as ProctName, --using an alias
'The list price for ' + ProctNumber + ' is $ ' + convert(varchar,ListPrice) +'.' ,--using the concatenation to join character end-to-end.
'The list price for ' + ProctNumber + ' is $ ' + convert(varchar,ListPrice) +'.' as [Description] --using brackets to let SQL server conside the strin as a column name
from Proction.Proct
--Topic 6
select BusinessEntityID,rate from [HumanResources].[EmployeePayHistory]
select BusinessEntityID
,rate*40*52 as AnnualSalary
,round(rate*40*52,1) as AnnualSalary
,round(rate*40*52,0) as AnnualSalary
from [HumanResources].[EmployeePayHistory]
select BusinessEntityID
,(rate+5)*40*52 as AnnualSalary
from [HumanResources].[EmployeePayHistory]
--Topic 7
select * from [Sales].[SalesOrderHeader]
select * from [Sales].[SalesOrderHeader]
where SalesPersonID=275
select * from [Sales].[SalesOrderHeader]
where SalesOrderNumber='so43670'
select * from [Sales].[SalesOrderHeader]
where TotalDue>5000
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID=275 and TotalDue>5000 --Comparison conditions: =,>,<,>=,<=,<>
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID=275 and TotalDue>5000 and Orderdate between '2005-08-01' and '1/1/2006'
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID=275 and TotalDue>5000 and Orderdate >= '2005-08-01' and Orderdate < '1/1/2006'
select * from [Proction].[Proct]
where name ='Mountain-100 Silver, 38'
--Topic 8
select * from [Proction].[Proct]
where name like'Mountain'
select * from [Proction].[Proct]
where name like'%Mountain%' --Wildcard % matches any zero or more characters
select * from [Proction].[Proct]
where name like'mountain%' -- "_" matches any single character
select * from [Proction].[Proct]
where name like'_ountain%'
--Topic 9
select * from [Proction].[Proct]
where color in ('red','white','black')
select * from [Proction].[Proct]
where size in ('60','61','62')
select * from [Proction].[Proct]
where class not in ('H') -- same as using: <> 'H'
--Topic 10
select * from [Proction].[Proct]
where size is null
select * from [Proction].[Proct]
where size is not null
--Topic 11
select * from [Proction].[Proct]
where color ='white'or color ='black'
select * from [Proction].[Proct]
where color ='white'and color ='black'
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000
--Topic 12
select count(SalesPersonID)
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
select distinct(SalesPersonID)
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
select count(distinct(SalesPersonID))
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
--Topic 13
select
Avg(TotalDue) as AverageTotalSales --aggregate functions
from [Sales].[SalesOrderHeader]
select
Avg(TotalDue) as AverageTotalSales
,Min(TotalDue) as MinimumTotalSales
,Max(TotalDue) as MaximumTotalSales
,Sum(TotalDue) as SummaryTotalSales
from [Sales].[SalesOrderHeader]
select SalesPersonID,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
--Error Message: Column 'Sales.SalesOrderHeader.SalesPersonID' is invalid in the select list
--because it is not contained in either an aggregate function or the GROUP BY clause.
select SalesPersonID,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by SalesPersonID
order by SalesPersonID
select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by SalesPersonID,OrderDate --Remember to put all un-aggregated columns after "group by"!!!
order by SalesPersonID
select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by SalesPersonID,OrderDate
having Max(TotalDue)>150000
order by SalesPersonID
----The classical T-SQL query!!!
select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null and OrderDate >='2007/1/1'
group by SalesPersonID,OrderDate
having Max(TotalDue)>150000
order by OrderDate desc
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
离过年还有2个月,是找工作,还是等过年? 还有两个多月就过年了,我是现在找工作呢还是过完年再找呢?现在找工作是... i57300u处理器属于什么水平 i5-7300u处于什么水平 国家线2024考研分数线是多少 电脑软件使用记录如何查看电脑当天的使用记录 在哪里查电脑最近使用情况怎么查看笔记本电脑的使用记录 英语题急求 ...以前都是以1MB/s速度下载,但是现在都不到300K/s,是不是WiFi路由器... 我家4兆的网线1台电脑可是我今天测网速时却只有16KB/S 这是为什么啊 耳后疼痛是什么原因? 好又多连锁超市有限公司的历史沿革 关于耳后根疼痛与什么有关的问题 求好又多超市更多相关介绍 数据库中常用的sql语句有哪些 关于好又多超市购物卡的问题 早上起来耳朵根后面疼 佛山好又多超市在哪里 新市好又多超市的东西真不真啊? 耳朵根后面痛肿大,是什么原因啊 好又多被沃尔玛收购? 好又多连锁超市 “好又多”超市不开发票,我该怎么办? 好又多究竟是哪个国家的 好又多购物怎么样? 好又多超市跟沃尔玛的关系是什么样子? 好又多的介绍 好又多方便面,你知道在那里可以买到吗? 好又多购物券怎么用? 好又多购物广场有限公司怎么样? 数据库SQL语言 耳后根疼痛,导致头皮都也跟着痛,是什么原因 感冒时耳朵根后面会有阵阵疼痛是为什么? 耳后根神经痛是怎么回事? 数据库语言SQL是一种什么语言? 耳后根疼痛是什么原因 sql数据库语言 什么是SQL 还有什么SQL语言 数据库的是什么? 关于SQL数据库语言的问题 SQL语句怎么写 电脑线稿如何去白色背景 Photoshop:怎么保留线条,擦掉白色背景? 在photoshop中线稿和背景色怎么样分离? ps怎么能把素描线稿变成白色 ps怎么提取线稿并更改线稿颜色 在ps里怎么把黑色的东西调成白色的呢? PS那种用快速蒙版去除黑白线稿白色背景的方法,我忘记了,求教!!! 在PS中怎么把一个棋盘图片的白色背景去掉,只留下线条 怎么去掉线条图的背景 用PS怎么把黑底白色的线条变成白底黑色的线条