SQL database: Data Query

data query

Statement format

Here Insert Picture Description

  • SELECT子句: Attribute columns to be displayed,
  • FROM子句: Specify the query object (base table or view)
  • WHERE子Sentence: Specify the query criteria
  • GROUP BYClauses: query results by the value of the packet of the specified sequence (for example, a male groups, one woman), equal to the property value tuples as a row group. Each group typically used in aggregate functions (for example a set count of the male and female a group count)
    • HAVINGPhrase: only meet the specified criteria to be set before the output
  • ORDER BYClauses: table column values ​​specified ascending or descending order of the query results

Functional decomposition:

Here Insert Picture Description

1.SELECT related usage

[Example 1] query all students learn numbers and names.

SELECT Sno,Sname   //选择属性  SELECT * 代表所有列
From Student		//指明表的来源
SELECT supplement usage:
SELECT Sno,2020-Sage   //可以得到出生年份,可以是表达式	
SELECT LOWER(Sdept)    //将得到的Sdept值全部化为小写
使用列的别名改变查询结果的列标题
SELECT Sname NAME,2020-Sage BIRTHDAY  //定义别名
Eliminate duplicate rows
  • SELECT default is not to delete duplicate rows, eliminating duplicate rows need to use the DISTINCT keyword
SELECT DISTINCT Sno

2.WHERE related usage

Query conditions predicate
Compare =,>,>=,!=, !>
Determine the scope BETWEEN AND , NOT BETWEEN AND
Determining a set IN , NOT IN
Character matches LIKE , NOT LIKE
Null IS NULL , IS NOT NULL
logic operation AND , OR , NOT

Comparison:
Department of Computer Science and Example 2. Query the list of students younger than 20 years old

SECLECT Sname
FROM Student
WHERE Sdept='CS' AND Sage<20;      //注意,这个分号不能少

To determine the scope:
Example 3. Query Age is not the student's name between 20-23 years old, and age lines do not

SELECT Sname,Sdpt,Sage
FROM Student
WHERE Sage NOT BETWEEN 20 AND 23;  

Determining a set:
Example 4. Query Department of Computer Science (CS), Department of Mathematics (MA) student's name and gender

SELECT Sname, Ssex
FROM Student
WHERE Sdept IN('CS','MA'); 

Character matches:

  • Syntax: predicate: [NOT] LIKE '<match string>' [ESCAPE '<escape character>']
  • <String> may be a full string may contain wildcards% and _
    • % Has a length representative of a string, a% b, representative beginning a, b at the end of a string of any length
    • _ Stands for any single character

Example 5. Query learn details of the number of students 1830130281

SELECT *
FROM Student
WHERE Sno LIKE '1830130281'  //<==> WHERE Sno = '1830130281'
  • ESCAPE escape character usage
    • Action: escape using the wildcard character escapes ordinary characters.
      Example 6. Query DB_Design course number and course credits
SELECT Cno , Ccredit
From Course
WHERE Cname LIKE 'DB\_Design'ESCAPE'\';

3.ORDER BY clause

  • Action: with one or more attributes may be arranged in columns,
  • ASC: ASC; Descending: DESC; default is ascending

Example 7. Results are arranged in descending score, reflects the ORDER BY clause can use

SELECT .....
FROM ....
WHERE ....
ORDER BY Grade DESC;  

Example 8. Results location-based number in the ascending, descending Age

SELECT ...
FROM ...
WHERE ...
ORDER BY Sdept ASC , Sage DESC;    //ASC省略也可以

4. aggregate functions (often with the GROUP BY HAVING used together or SELECT)

  • It can only be used SELECTwithHAVING
  • Statistics Ganso number:COUNT(*)
  • A number of statistics worth:COUNT( [ DISTINCT|ALL ] <列名>)
  • Calculating a sum of: SUM(同第二行)
  • Calculating an average value: AVG(同第二行)
  • Seeking a maximum and minimum of: MAX(同第二行), MIN(同第二行)
  • 注意: DISTINCTNot a must have, it means de-emphasis (default is ALL)

Example 9. query the number of students enrolled in the course.

SELECT COUNT(DISTINCT Sno)
FROM SC;

5.GROUP BY clause groups

  • Specified value or more columns of a packet, a set of values is equal.
    Usage Note:

错误示例:

SELECT Cnpo     //
FROM SC			//
GROUP BY Cno   //

Expression must correspond to the back of the SELECT and GROUP BY expressions behind. If the GROUP BY y the SELECT y, if you do not have to correspond to the SELECT followed by aggregate functions, such as COUNT (Cnpo)

Example 10. The individual seeking enrollment program number and the corresponding number. (Idea: the same number of courses as a group)

SELECT Cno, COUNT(Sno)
FROM SC
GROUP BY Cno;   //意思是课程号相等的所有列为一组。

Similar results shown in the table below:

Who are COUNT(Sno)
1 22
2 34
3 45
4 12

Example 11. Query elective courses for more than three student number

SELECT Sno
FROM SC
GROUP BY Sno
HAVING COUNT(*)>3;  //HAVING函数是在对每一个组进行操作,某一组列数大于3的输出

Example 12. The average score of the query points is 90 and the average score of the student number

SELECT Sno , AVG(Grade)
FROM SC
GROUP BY Sno   
HAVING AVG(Grade)>=90;  //AVG不能放在WHERE中!!!  

13. The Department of examples and statistics to distinguish between the number of male and female students in each department, and sorted in descending order of eucalyptus

SELECT Sdept, Ssex , COUNT(Sno)
FROM Student
GROUP BY Sdept , Ssex
ORDER BY COUNT(Sno) DESC;
Published 22 original articles · won praise 0 · Views 154

Guess you like

Origin blog.csdn.net/weixin_42649617/article/details/104834312