- Date: 2017-09-29 Finishing: Byzqy
This article still takes "Liangshan heroes" data table as an example, introduces several commonly used T-SQL query statements:
- Go to re-query, keyword: distinct
- Using wildcard characters for fuzzy queries, keywords: like, wildcard:%
- Sort queries (ascending, descending), keywords: order BY, DESC
- Grouped queries, keywords: GROUP by
- Using Functions, Keywords: max, avg
- Specify the number of records to return, keyword: top
Files: Sqlquery3.sql
/*Description: Sqlquery3.sql (This is a new query in SQL Server Management Studio) OS name: Microsoft Windows 7 Environment: Microsoft SQL Server 2012 tools: Mi Crosoft SQL Server Management Studio database: Liangshanhero data sheet: Hero Details:--id name nick_name hometown sex sal--1 Song Jiang timely Shandong male 2200--2 Lu Junyi Jade Kylin Hebei male 8000--3 Wu Yong Mastermind Shandong male 1100--4 Gongsun wins into Yunlong Chongqing male 5000--4 Erniang dominatrix Shanxi Female 2000--5 lin Leopard head Henan male*/--simple calculation with Select, definition of new column name as keywordSelect A* theSelect A* - asCalculation Results--Remove duplicate information querySelect * fromHeroSelect distinctSex fromHero--set query criteria, use wildcard characters to blur a querySelect * fromHerowhereHometown like 'River%' andSex='male'--sort, ascending, descendingSelect * fromHeroSelect * fromHeroOrder bySal,namedesc--GroupingSelectNameavg(SAL) asWages fromHerowhereSex='male' Group bynameSelectSexavg(SAL) asWages fromHeroGroup bySex--using FunctionsSelect Max(SAL) asMaximum wage fromHeroSelect Top TenSal asThe first three wages fromHeroSelect Top 3 * fromHeroOrder bySalSelect Top 3 * fromHeroOrder bySaldesc-------------------------------------------Create a database, create a table, add dataCreate DatabaseLiangshanhero UseLiangshanheroGoCreate TableHero (IDint, namevarchar( -), Nick_namevarchar( -), Hometownvarchar( -), SexChar(2), Salint)Insert intoHeroValues(1,'Song Jiang','Timely Rain','Shandong','male', -)Insert intoHeroValues(2,'Lu Junyi','Jade Kylin','Hebei','male',8000)Insert intoHeroValues(3,'for Wu','mastermind','Shandong','male', +)Insert intoHeroValues(4,'Gongsun Sheng','into Yunlong','Chongqing','male', the)Insert intoHeroValues(4,'Erniang','Dominatrix','Shanxi','female', -)Insert intoHeroValues(5,'Lin','Leopard Head','Henan','male', -)
View Code
:
01
02
03
04
05
06
--the End.
sqlserver_query3_ Query | fuzzy query | sorting | grouping | using functions