Database - single-table queries

[3.29] Example No. query learn the details of students 201 215 121.

SELECT *
FROM Student
WHERE Sno LIKE '201215121';

Equivalent to

SELECT *
FROM Student
WHERE Sno = '201215121';

Here Insert Picture Description
[3.30] to query all cases surnamed Liu student's name, student number and gender.

SELECT Sname, Sno, Ssex
FROM Student
WHERE  Sname LIKE '刘%';

The general format of the string matching

 [NOT] LIKE<匹配串>[ESCAPE<换码字符>]

Means that the value for the specified attribute column tuple <match string> match. <Match string> may be a full string may contain wildcards% and _.
Wherein,% represents a string of any length.
_ Stands for any single character.

It can be achieved only part of the display data required by the specified attribute name.
Here Insert Picture Description
[3.31] Example query name "Liu" and the full name of the two Chinese characters student's name.

 SELECT Sname
 FROM Student
 WHERE Sname LIKE '刘_';

Here Insert Picture Description
Note: The environment of different characters, different numbers _ on behalf of a Chinese character needed.
When the database is the ASCII character set, a character _ it requires two
databases when GBK character set, a character requires a _
where _ represents a character, and the byte phase difference.
Under the ASCII code, a character only one byte, but characters need two characters.
Next GBK, a character requires two bytes, but only need a kanji character.

Queries character set in SQLserver

SELECT COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage')

Here Insert Picture Description
936 represents the GBK code.
[3.32] Example query name in the second word is "positive," the student's name and student number.

SELECT Sname,Sno
FROM Student
WHERE  Sname LIKE '_阳%';

Here Insert Picture Description
[3.33] to query all cases not surnamed Liu, student name, student number and gender.

 SELECT Sname,Sno,Ssex
 FROM Student
 WHERE Sname NOT LIKE '刘%';

Here Insert Picture Description
[3.34] Example query DB_Design course number and course credits.

SELECT Cno,Ccredit
FROM Course
WHERE Cname LIKE 'DB\_Design'ESCAPE '\'; 

Here Insert Picture Description
[3.35] Example queries that begin with "DB_", and the countdown to the first three characters of course i details.

SELECT *
FROM Course
WHERE Cname LIKE 'DB\_%i__'ESCAPE '\';

Here Insert Picture Description
[3.36] did not participate in some patients after elective courses students exam, so there are elective record, but did not test scores. Query lack of student achievement and school number corresponding course number.

SELECT Sno,Cno
FROM SC
WHERE Grade IS NULL;

[3.37] to search all cases the student number and the course number has been fruitful.

SELECT Sno,Cno
FROM SC
WHERE Grade IS NOT NULL;

[3.38] Example queries Department of Computer Science at the age of 20 years old names of the students.

SELECT Sname
FROM Student
WHERE Sdept='CS'AND Sage<20;

Here Insert Picture Description
[3.39] query elective cases No. 3 courses students learn numbers and their results, the query results in descending order by score.

SELECT Sno,Grade
FROM SC
WHERE Cno= ' 3 '
ORDER BY Grade DESC;

For null, display the sorting order is determined by a specific system implementation.
GROUP BY:
refining the object aggregation function
if no packet query results, aggregate function applied to the entire query result of
the query results grouping, aggregate functions are applied to each group to
the specified value of the packet one or more columns, value is set equal to
[3.40] Discover all students embodiment, the query results by line number where the lines are arranged in ascending order, the same train students in descending order according to age.

SELECT *
FROM Student
ORDER BY Sdept ASC, Sage DESC;  

Here Insert Picture Description
[3.41] Example query the total number of students.

SELECT COUNT(*)
FROM  Student; 

Here Insert Picture Description
[3.42] Example query the number of students enrolled in the course.

SELECT COUNT(DISTINCT Sno)
FROM SC;

Student grade point average [3.43] Example No. 1 course.

SELECT AVG(Grade)
FROM SC
WHERE Cno='1';

[Query] 3.44 cases of elective courses students No. 1 highest score.

SELECT MAX(Grade)
FROM SC
WHERE Cno='1';

Here Insert Picture Description
[3.45] inquiry cases of students 201 215 012 elective courses of their total number of scores.

SELECT SUM(Ccredit)
FROM  SC,Course
WHERE Sno='201215122' AND SC.Cno=Course.Cno; 

[3.46] patients seek each course number and the corresponding number of elective.

SELECT Cno,COUNT(Sno)
FROM SC
GROUP BY Cno; 

[3.47] Example query enrolled student number three or more courses.

SELECT Sno
FROM SC
GROUP BY Sno
HAVING COUNT(*)>3; 

[Example] 3.48 grade point average query is greater than or equal to 90 minutes of student number and grade point average.

SELECT Sno,AVG(Grade)
FROM SC
GROUP BY Sno
HAVING AVG(Grade)>=80;

Here Insert Picture Description
Note WHERE acting before the polymerization, HAVING acting after the polymerization.

Released six original articles · won praise 5 · Views 2776

Guess you like

Origin blog.csdn.net/jiesfriend/article/details/104818484