Basic format for query (SELECT):
Select < column name > from < table name > [where < query qualifications;]
Explain:
command body for SELECT queries
Column name: Columns to query, multiple columns with commas, separated by asterisks (*) to represent all columns
Where: is optional, the result of the non-filled query is the entire column of data
Example:
Select score from score table where name = ' Zhang San '
Meaning: Check the scores of Zhang San in the score table, show only Zhang San points. If you do not add where, all values of the score column in the score table are displayed
Syntax for the SELECT statement in SQL Server:
Select < column name > [into new table name] from < table name >
[where query condition] [GROUP by group condition] [having query Condition] [ORDER BY sort condition [ASC | desc]]
Explanation:
into: Optional parameter for saving the query to a new table without having to create a new table in the database. The query criteria field for the
where is either a conditional expression or a logical expression.
GROUP BY: Optional parameter that specifies the grouping criteria for the query result, usually a column name, but not an alias.
Having: Optional parameters, which specify grouping search criteria, are usually used with the group by sentence.
ORDER BY: Optional parameter that specifies how the query results are sorted. The sort criteria asc denotes ascending, desc indicates descending, and the default sort is ASC ascending.
Various query examples:
1. Query all the columns in the score table.
SELECT * FROM Score table
2. Check the name of the score table and the contents of the results list.
Select name, score from score table
3. Check the scores of Zhang San in the score table and show the name and results
Select name, score from score table where name = ' Zhang San '
4. Check the scores of the scores in the score table between 90~100.
SELECT * from score table where scores between and 100
5. Check all information in the score table for 80,90,95 score
SELECT * from score table where score in (80,90,95)
6. Check all information of students surnamed Zhang in the score table.
SELECT * from score table where name like ' sheet% '
7. Check all information of Class 1 Zhang San in the score table.
SELECT * from score table where name = ' Zhang San ' and class =1
8. Check all information of people who are not empty in the score table
SELECT * FROM Score table where memo is not NULL
9. Use the top keyword to limit the number of rows returned by the query and query the first 10 rows of data in the score table.
Select top * FROM score table
10. Change the query result name: Query the score table in the name and score two columns of data column names appear in English Name,grade
Select name as name, score as grade from score table
Manipulating data tables with T-SQL statements-querying data