Experiment 2 single table query


Based on the creation of libraries, tables, and data entry in Experiment 1, complete the following operations.

1. Query the student ID, name and grade of all students in grade 2016.

select sno,sname,Ssex
from Students
where grade='2016';

Analysis: What is to be displayed is the student’s student number, name and class, so use it select sno,sname,Ssex. These attributes are all in the Students table, so you can directly select it from Students. The condition to be selected is that the grade is 2016, and the condition selection useswhere

2. Inquire about the name and class hours of courses with 64 or 48 hours.

select Cname,Chour
from Courses
where Chour=64 or Chour=48;

Analysis: What we need is that the two attributes of course name (cname) and class hour (chour) are in the courses table, so we can directly select the courses table (from Courses), the condition is that chour is 64 or 48, and the where condition can be compared with operatoror concatenation

Commonly used comparison operators:

operator meaning
=, >,<,>=,<=, !=,> Comparison of size
AND, OR, NOT multiple conditions
IN Determine the collection
BETWEEN AND Determine the scope
IS NULL empty value
LIKE character match

3. Query the course numbers of all selected courses.

select distinct cno
from STC;

DISTINCT: Return all non-duplicate rows, if not written, the default is ALL (return all rows, including duplicate rows)

4. Query the course numbers and course names containing "number" in all courses.

select Cno,Cname
from Courses
where Cname LIKE '%数%';

This article involves模糊查询字符串匹配

string match

Format: attribute name LIKE '<matching string>'
attribute name NOT LIKE '<matching string>'
for example

Cname LIKE '%数%'  #含有“数”的
Cname NOT LIKE '%数%'  #不含有“数”的 例如 数学 高数 只要字符串里有‘数’都符合条件
Cname LIKE '数%'  #以“数”开头的,例如 数学 数字 ,但是‘高数’不符合,因为‘高数’是高开头的

◆<matching string>=character constant+wildcard
two kinds of wildcards:
%: represents 任意长度a string (length can be 0)
_: represents any 单个character

5. Calculate the average salary of all teachers.

select AVG(salary)
from Teachers

SQL has many built-in functions for counting and calculations.
The AVG() function returns the average value of a numeric column.
Click to see more functions

6. Query the student ID of the student who did not take the test.

select Sno
from STC
where score is null;

Students who did not take the exam, that is, 成绩为空students of

Notice:

The value is empty, the null in score is null cannot be quoted ' ', after adding quotes, it means the string null, this way of writing is wrongscore is 'null'

7. Query the average grade of each teacher's courses, and arrange them in descending order of the average grade (reminder: each teacher only takes one course).

select Tno,avg(score)
from STC
group by Tno
order by avg(score) desc;
the code meaning
select Tno,avg(score) Select (select) to display the average grade (avg(score)) and teacher number (Tno),
group by Tno Group by teacher number (Tno)
order by avg(score) desc According to grade point average, descending order (desc)

8. Count the number of electives and the highest grades of each course.

select count(*),max(Score)
from STC
group by Cno
the code meaning
group by Cno Count the things of each course, so group by course number
count(*) Count the number of course students, that is, count the number of occurrences of each course number count(cno) in the stc table, because it has been grouped by cno (group by Cno), so it can be written as count(*), and count(cno) is also good
max(Score) The highest score can use the max function

9. Query the number of students who have taken at least 3 courses and the number of courses taken.

select Sno,count(Cno)
from STC
group by Sno having count(Cno)>=3;

To group by student number, count the number of course numbers for each student number.
The reason for adding the HAVING clause to SQL is that the WHERE keyword cannot be used with aggregate functions.
The HAVING clause allows us to filter the groups of data after grouping. .

10. Query the student IDs of students who have taken any course B001 or B0 02.

select distinct sno
from STC
where Cno='B001' or Cno='B002';

11. Inquire about the course number taken by at least three students.

select Cno
from STC
group by Cno HAVING count(Cno)>=3;

Summarize

  1. What you select is followed by what you want to display, for example: select Cno, then the result will only display the content of Cno
  2. When using group by, you cannot use where. The WHERE keyword cannot be used with aggregate functions. To use having, the HAVING clause allows us to filter each group of data after grouping
  3. count() counts the number of contents in parentheses. The COUNT (*) function returns the number of records in the table. It is often used in conjunction with group by. After grouping, it counts the number of each content grouped by group by. For example, we base cno grouping, group by cno, COUNT(*) will return the number of each type of cno

Guess you like

Origin blog.csdn.net/qq_25887493/article/details/124074176