Common SQL Server Query statements

Source: Internet
Author: User
Tags logical operators sql server query

//Note: Here are some of the more frequently used SQL statement syntax to query and use later

SQL statements consist of commands, clauses, operators, and statistical functions.
I. Order

1. Data definition commands:
Create: Create a new data table, field, and Index table;
Drop: Deletes a data table or index from the database;
ALTER: Add or modify field properties;

2. Data manipulation Commands:
SELECT: Find the record that satisfies the condition;
INSERT: Add a record or merge two data tables;
Update: Updates the record that satisfies the condition;
Delete: Deletes the record that satisfies the condition;

two. Clauses

From: Specify the data table;
WHERE: Set conditions;
GROUP BY: Set up grouping;
Having: set the grouping conditions;
ORDER by: Sets the condition of the output;

three. Operators

1. Logical operators: and (with), or (or), not (non);
2. Relational operators: <, <=, >, >=, =, <> (not equal to);
3. Other operators: between (set range), like (wildcard settings), in (Set settings);

four. Statistical functions

AVG: averaging;
Count: ask for quantity;
sum: sum;
Max: Maximum value;
Min: To find the minimum value;

Five. Common Select, INSERT, UPDATE, DELETE statements

1. Data query Operations
The common SELECT statement consists of 6 parts, in the syntax form:

SELECT [TOP (value)] [all | distinct] < target column expression; * 

From table name
WHERE Query Statement
Group BY Group field (property name)
Having group conditions
ORDER by field [ASC | DESC]

The whole SELECT statement means that, depending on the query condition expression of the WHERE clause, the base table specified from the FROM clause (separated by commas if there are multiple tables) finds the tuple that satisfies the condition and then presses the property name in the SELECT clause (with multiple property names separated by commas) When the field name comes from a different table, the field prefix should be preceded by a field name, the attribute values in the tuple are selected to form the result table, and if there is a group clause, the result is grouped by the value of the attribute, which is a group of equal tuples. Aggregate functions are typically used in each group. If the group clause has a HAVING clause, only the groups that meet the specified criteria are output, and if there is an ORDER BY clause, the result table is also arranged in ascending (ASC) or descending (DESC) Order of the value of the field, which is ascending by default.

which
*: Indicates all fields of the corresponding table;
DISTINCT: Remove duplicate rows from the result table;

2. Data insertion Operation

(1) Insert of the unit group:
INSERT into base table name [(Column name of table)] values (tuple value)
(2) Insertion of multiple groups:
INSERT into base table name [(Column name of table)] values (tuple value), (tuple value),.., (tuple value)
(3) Insert of query result:
INSERT into base table name [(Column name of table)]
This statement can insert the query result of a SELECT statement into a base table.

3. Modifying Data Operations

UPDATE base table Name
SET Property Name = property value
WHERE Query Statement

4. Delete Data Operations

DELETE
From base table name
WHERE Query Statement

Six. Parameterized SQL statements

In the SQL statement, the global variable name begins with @@ 开头 The local variable begins with @.
There are two ways to assign a value to a variable, one is to use a SELECT statement, and the other is to use a set statement.
SELECT @varName = Expression
Or:
DECLARE @varName Char (20)
SET @varName = "Hello world!"

Common SQL Server Query statements

Related Article

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.