sqlserver_query3_ Query | fuzzy query | sorting | grouping | using functions

Source: Internet
Author: User
Tags microsoft sql server management studio sql server management sql server management studio

    • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.