SQL statement is the basis - the basis of single-table queries

introduction

This article is a review of the principle of learning record in my database, although once learned, but some unusual statements or some details of the place will inevitably forget, we would like to take this opportunity to review some basic knowledge of the points are recorded , a good memory is not as bad written, not to mention my memory is not good. Previous studies also rely on the study notes more than you big brother, even if I can not record to help others, but also give yourself parts of the record.

Query structure

Sentence structure of the standard query

select <destination column name>
from <table name> [join <table name> on <connection condition>]
[WHERE <row selection condition>]
[Group by <packets based on condition>]
[HAVING <set selection condition>]
[Order by <sort_column>]

Where :

  • select clause specifies the output of the column; (projection)
  • from clause specifies the data source (join on connection multi-table queries of other tables);
  • where clause specifies row select selection criteria; (selection)
  • group by clause is used to subject the cable to group records;
  • having clause specifies the selection criteria for the grouping result;
  • order by clause is used to sort query results;

select from statement is a query clause must, as the case may be other statements choose
Save the query results to a table
Select statement query results generated are stored in memory, if you want the results permanently saved, for example, into a table, select statement can be achieved by clause into

SELECT 查询列表序列 INTO <新表名>
FROM 数据源
.......其他查询子句

Detailed specific inquiries

The following table as an example of a single-table query:
Stduent

Sno come off Shseksh Sbirthday Sdept Memo

Course

Who are Cname PreCno Credit Semester

SC

Sno Who are Grade

Specific data will not show up, as long as it can be queried according to the head of the table.

1. The lookup table all the students all the information

		select Sno,Sname,Ssex,Sbirthday,Sdept,Memo 
		from Student;
或者
  		select * from Student;

2. Query table does not have a column

	     select  Sname year(getdate())-year(Sbirthday)  年龄
	     from Student;

After the column name by which the expression meaning an alias for a column
can take an alias to the column and through expressions, functions, constants, and other structural rows when the query table does not have columns.

Basic usage 3.where clause

where conditions can be selected according to the actual needs of rows of
common query criteria are:

  • Comparison operators
    =,>, <,> =, <=,! =

  • Determining the range operator

    between. A  and  B ,not between. A  and  B
    

    Value is used to specify a column in the range of, A, B respectively indicate the next sector, the range including the boundary data type, the column values ​​must be consistent with the type of boundary value

  • Determining a set

    	in,not in
    

    Syntax: Column name [not] in (constant 1, 2 constant, constant ... 3)
    in, when the column is equal to the value set the value of a constant, the result is true, shows that this value match the query recording

  • Character matches

    like,not like
    

    Syntax: Column Name [not] like <match string>
    represents fuzzy query,
    matching string:
    _ (underline): match any one character
    % (percent): 0 to match a plurality of characters
    []: matching in [] any one character
    [^]: does not match any one character

  • Null

  null,not null

Null has a special meaning in the database, he said that the current uncertain or unknown value can not be used to determine comparison operators, need to involve judgments with null values ​​is null or is not null to judge.

  • Multiple conditions
	and,or

When the need to combine a plurality of query conditions, using the logical operators AND and connected or where the query
or operator and is higher than the priority, changing the order of the operator may be added parentheses () implementation.
E.g:

//查询机电系和计算机系1997年出生的学生的学号,姓名,所在系和出生日期。
select Sno,Sname,Sdept,Sbirthday from Student
where (Sdept='计算机‘ or Sdept=‘机电系’)
and Sbirthday between '1997-01-01' to '1997-12-31'

4. Sort Order By de-emphasis and DISTINCT

  • remove duplicate distinct columns
//找出表中所有的系
select distinct Sdept from Student

Sort syntax:
Order by <column name> [ASC | DESC]
ASC indicates ascending, descending order DESC denotes default ordering of ASC

The aggregate function

Aggregation function, also known as statistical functions, a set of values ​​is calculated, and a statistical result returns.

  • The number of tuples in tables: COUNT (*)
  • COUNT ([distinct] <column name>): a statistical value of the number in this column, distinct value indicates the repetition removed
  • SUM (<column name>): calculated value and the column value (column must be numeric)
  • AVG (<column name>): calculated value and the column value (column must be numeric)
  • MAX (<column name>): the maximum value of the column to give
  • MIN (<column name>): the minimum value of the column to give
//统计课程C01的最高分和最低分
select Max(Grade) 最高分,Min(Grade) 最低分
from SC where Cno='C01';

top:
Syntax: TOP n (percent)

 - n为非负整数,表示排序后的前n位。n precent表示取排序后的前n%的数据

E.g:

//查询C04号课程排名前三的学号和成绩
select top 3 Sno,Grade From SC where Cno='C04'

6. grouping queries Gruop By and HAVING

Group By the data packets may be, the statistical results that a control group, the polymerizable group may be used for statistical functions over the group, a plurality of column values grouped packet
example:

select Sno 学号,Sname 姓名,COUNT(Cno)选课门数,AVG(Grade)平均成绩
from SC Group By Sno;

HAVING is statistical results grouped screened again, and Group By clause is generally used together, the effect is similar to where

//查询选课门数超过三门的学生学号和选课门数
select Sno,COUNT*)选课门数 From SC
Group By Sno HAVING COUNT*>3;

But be careful: when where and group by appear at the same time, the selection criteria where clause will perform the first step, then carrying packet statistics
having an effect similar to where, but having that effect on top of the group, where records for a single, having clause can be reused clustering function, where not

//查询选课超过三门的学生的学号和选课门数
select Sno,COUNT*)选课门数 From SC
Group By Sno HAVING COUNT*>3

When comparing the two, you can be screened for the condition before the packet specified in the where clause is more efficient, thus reducing the data packets involved in the row. It should be specified filter criteria in the HAVING clause must be those filtering criteria to apply after the implementation of the grouping condition.

Epilogue

Remember when he had just learned SQL statement is not difficult to know, but also with the muddle, most of the time to write the query must be open book, homework learn from examples in the book is finished, but as the program use in preparation, most of the sentence may have been written to fill the gap Practice makes perfect, and review the first time really learning with completely different, aware of a lot of small details will not care about before. Reviewing the Old, the ancients Cheng bully me.
Knowledge of single-table queries has ended, the next blog post to share knowledge multi-table join query.
@Tang

Guess you like

Origin blog.csdn.net/livovil/article/details/93627946