SQL Server database basic query

Source: Internet
Author: User
SQL Server basic syntax -- view the data table select * fromStudent -- use SQL to query data -- 1. query all the classes in the table Select * FromTableName -- 2. Specify the field category in the query table SelectColumnName, FromTableNameSelectstuName, stuNo, stuSexFromstuInfo -- 3. query with Where Condition

SQL Server basic syntax -- view the data table select * fromStudent -- use SQL to query data -- 1. query all the classes in the table Select * FromTableName -- 2. Specify the field class SelectColumnName, FromTableName SelectstuName, stuNo, stuSexFromstuInfo -- 3. query with Where Condition

Basic SQL Server syntax

-- View data tables

Select * from Student

-- Use SQL to query data

-- 1. query all the class capacities in the table

Select * From TableName

-- 2. query the specified field category in the table

Select ColumnName ,..., From TableName

Select stuName, stuNo, stuSex From stuInfo

-- 3. query with Where Condition

Select * | ColumnName From TableName Where condition

Select * From stuInfo where stuSex = 'femal'

-- 4. query with sorting (Order By ColumnName)

-- Syntax: Select * | ColumnName From TableName Order By ColumnName Desc | Asc

Select * From stuInfo Order By stuAge, stuSeat desc

-- 5. Select a specified number of records, usually used with order

-- Syntax: Select Top num * | ColumnName From TableName

-- Syntax: Select Top num * | ColumnName From TableName Order By Desc | Asc

Select Top 2 * From stuInfo Order By stuAge Desc

-- 6. query Group By groups

-- The columns contained in the GROUP query must be included in the aggregate function or group by clause.

Select * From stuInfo

Select stuSex, Max (stuAge) As 'average age' From stuInfo Group By stuSex

-- 7. filter the grouped results

-- Having (equivalent to Where)

Select * From stuInfo

Select stuSex, Avg (stuAge) As 'average age' From stuInfo Group By stuSex having Avg (stuAge)> 20

-- 8. Use Group By with Where

Select * From stuInfo

Select stuSex, Avg (stuAge) As 'average age' From stuInfo where stuAge> 18 Group By stuSex having Avg (stuAge)> 20

-----------------------------------------------------------

-- Insert data using SQL

-- 1. Insert data without specifying Columns

Syntax: Insert Into TableName Values (Value List)

Insert Into stuInfo Values ('small 8', 's25311', 'male', 24, 'beijing ')

Select * from stuInfo

-- 2. Specify the column name to insert data.

-- Syntax: Insert Into TableName (column Name List) Values (Value List)

-- Note: You can specify the order of the column name list, but the order of the Value List should be the same as that of the column name list.

Insert Into stuInfo (stuName, stuNo, stuSex, stuAddress, stuAge) Values ('9', 's25312 ', 'mal', 'shanghai', 25)

Select * from stuInfo

-- 3. Insert multiple records at a time

-- 1) Insert Into TableName (column name table) Select... Insert From to an existing table

-- Note: the data type of the column name must be the same in number

Insert Into stuInfoCopy (stuName, stuNo, stuSex, stuAge, stuSeat, stuAddress)

Select * From stuInfo

Select * From stuInfoCopy

-- 3. Insert multiple records at a time

-- 2) Select column name list Into new table name From SourceTable insert to existing table

-- Note: the data type of the column name must be the same in number and the new table must not exist.

Select Identity (int, 1, 1) As 'id', stuName, stuNo, stuSex, stuAge, stuAddress

Into # temp

From stuInfo

Select * from # temp

-- 3. Insert multiple rows at a time

-- 3) use Union to merge data rows

Insert # temp (stuName, stuNo, stuSex, stuAge, stuAddress)

Select 'Baby ', 's25318', 'male', 22, 'hubei 'Union

Select 'Baby 2', 's25318 ', 'female', 23, 'hunan'

Select * from # temp

-- 4. Change Data

-- Syntax: Update TableName Set ColumnName = value where Condititon

Update # temp Set stuName = 'Baby 3' Where stuName = 'Baby'

Select * from # temp

-- 5 delete data

-- Syntax: Delete From TableName Where Condition

Delete From # temp Where Id = 8

Select * From # temp

-- 5 delete data

-- Syntax: Truncate Table TableName (When deleting all data in a Table, it is more efficient than Delete, but cannot

-- Delete a table containing foreign key constraints

Truncate Table stuMarks

-- Where Condition Type

-- 1. ColumnName Between low value And high value

Select * from stuInfo Where stuAge Between 20 And 25

-- 2, And Or Not (And, Or, Not)

-- 3. In (Value List)

Select * from stuInfo Where stuAge IN (225-25)

-- 4. Like (fuzzy query)

-- % Indicates any number of characters _ one character [] a range [^] is not in a range

Select * from stuInfo Where stuName like 'small %'

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.