Table of contents
Create student table
Create Students and Courses tables
CREATE TABLE Students (
StudentID int PRIMARY KEY,
Name varchar(255),
Gender varchar(1),
Age int,
City varchar(255)
);
INSERT INTO Students VALUES(1, 'David', 'M', 21, 'Shanghai');
INSERT INTO Students VALUES(2, 'Kevin', 'M', 19, 'Beijing');
INSERT INTO Students VALUES(3, 'Emily', 'F', 22, 'Shanghai');
INSERT INTO Students VALUES(4, 'William', 'M', 20, 'New York City');
INSERT INTO Students VALUES(5, 'Alice', 'F', 19, 'Los Angeles');
INSERT INTO Students VALUES(6, 'Frank', 'F', 22, 'Los Angeles');
CREATE TABLE Courses (
CourseID int PRIMARY KEY,
CourseName varchar(255)
);
INSERT INTO Courses VALUES(1, 'CS101');
INSERT INTO Courses VALUES(2, 'CS202');
INSERT INTO Courses VALUES(3, 'EE101');
Aggregation query
aggregate function
direct inquiry
SELECT COUNT(*) FROM Students;
Set up alias query
SELECT COUNT(*) AS StudentsNum FROM Students;
Set conditional query
Using COUNT(*) has the same effect as COUNT(StudentID), because StudentID is the primary key, and the primary key of each row is different. In addition, we can still use the WHERE clause in aggregate queries. For example, if we want to find the number of students older than 20 years old , we can use the following SQL statement:
SELECT COUNT(*) FROM Students AS s WHERE s.age > 20;
Commonly used aggregate functions
Function description
SUM Calculate the sum of a certain column, the column must be of numeric type
AVG Calculate the average of a certain column, the column must be of numeric type
MAX Calculate the maximum value of a certain column
MIN Calculate the minimum value of a certain column
Query the average age of students
SELECT AVG(Age) FROM Students;
Using AVG aggregate functions
Group query
Single fieldGroup by
Group students according to city, and need to query how many students are in each city
SELECT City, COUNT(*) FROM Students GROUP BY City;
Error report group query
If you also put Name into the query results, an error will be reported because there may be many students from the same city with different names, so the following statement is illegal:
SELECT City, COUNT(*), Name FROM Students GROUP BY City;
The error message is as follows:
Expression #3 of the SELECT list is not in the GROUP BY clause, and contains the non-aggregated column "tuling.Students.Name", which does not functionally depend on the columns in the GROUP BY clause; this is consistent with sql_mode= only_full_group_by is not compatible.
#3 refers to the Name column
Multi-field group query
GROUP BY can also put multiple fields. For example, if we want to group students by City first, and then by gender , we can use the following statement:
SELECT City, Gender, COUNT(*) FROM Students GROUP BY City, Gender;
The two people in Los Angeles in the student table have the same gender. The group display is F and the count is 2.
Now I change the gender of one of them to M. Think about whether the query results will change?
Those with the same location but different genders were distinguished.
It can be seen that the original Los Angeles was 2, but now it has become two 1s, and the genders M and F are distinguished.
Multi-table query
In addition to querying data from a single table, SELECT queries can also query data from multiple tables. The syntax is as follows:
direct inquiry
SELECT * FROM Students, Courses;
The query result is the Cartesian product of the two tables
Assume that Students has 5 columns of fields and 7 rows of records, and Courses has 2 columns of fields and 3 rows of records. The result is a two-dimensional table with 21 (3 * 7) rows of records and 7 (5 + 2) columns of fields, namely Students Each row of the table is paired with each row of the Courses table. The number of columns in the result set will be the sum of the number of columns in the two tables, and the number of rows will be the product of the number of rows in the two tables.
Rename query
Sometimes two tables may have fields with the same name, and the result will be confusing. We can use AS to alias the fields to distinguish them. For example, use the following statement to rename StudentID and CourseID to StudentId and CourseId:
SELECT Students.StudentID AS StudentId, Courses.CourseID AS CourseId FROM Students, Courses;
The syntax for aliasing a field is similar to column_name AS new_column_name. The above statement aliases StudentID and CoursesID respectively, StudentId and CourseId, although in this example, we just change the last letter D to lowercase. But consider another situation: assuming that the primary key fields of Students and Courses are both called ID, then this alias is very helpful. We can use the following statement to make the query results clearer:
SELECT Students.ID AS StudentId, Courses.ID AS CourseId FROM Students, Courses;
In addition to aliasing the output fields, it is also useful to alias the table. The syntax is similar to table_name AS alias. We can write the above statement as follows:
SELECT S.ID AS StudentId, C.ID AS CourseId FROM Students AS S, Courses AS C;
Create a new column CourseID in the Students table
In multi-table queries, the WHERE clause can still be used. In order to help everyone understand the following content, we need to add a new field CourseID to Students, which represents the course chosen by this student:
Create a new column of CourseID after City
alter table Students add column CourseID int(4) not null after City;
The value of CouseID is set to half the value of StudentID
update Students set CourseID = StudentID/2
Query StudentID and its corresponding course name
SELECT S.StudentID,C.CourseName
FROM Students AS S,Courses AS C
WHERE S.CourseID = C.CourseID;
Report an error! ! ! Do you know why? Generally, erro syntax is a problem with Chinese characters.
Students as S,Courses as C 这个逗号是中文字符,改成英文字符后运行成功啦
In addition to the WHERE clause, other clauses similar to ORDER BY and GROUP BY are also suitable for multi-table queries.
In addition to the WHERE clause, other clauses similar to ORDER BY and GROUP BY are also suitable for multi-table queries.
JOIN query
INNER JOIN
Join query is another type of multi-table query. Join query performs JOIN operation on multiple tables. That is to say, first determine a main table as the result set, and then selectively "embed" records from other tables into the main table result set.
Suppose we want to know the course name selected by each student. In addition to the multi-table query plus WHERE clause mentioned above, we can also use the INNER JOIN clause:
SELECT S.StudentID, C.CourseName
FROM Students AS S
INNER JOIN Courses AS C ON S.CourseID = C.CourseID;
This statement can query each StudentID and its corresponding course name. Please note that the inner execution process of the INNSER JOIN statement is as follows:
- 1 Determine the main table, use FROM table_name
- 2 Then confirm the connected table and use INNER JOIN table_name
- 3 Determine the connection conditions and use ON condition. The condition of the above statement is S.CourseID = C.CourseID
- 4 Finally, we can add: WHERE, ORDER BY and other clauses
. In addition to INNER JOIN, we also have LEFT JOIN, RIGHT JOIN, and FULL JOIN.
Supplement: Aliases are not required, just to increase readability.
RIGHT JOIN, LEFT JOIN
If we change the statement to RIGHT JOIN:
SELECT S.StudentID, C.CourseName
FROM Students AS S
RIGHT JOIN Courses AS C ON S.CourseID = C.CourseID;
Modify the Students table, let 5 and 6 select course 1, and re-right join the query.
After executing the above statement, if there is a class without any students joining, we will have a redundant record with only CourseName in the record, but StudentID is NULL.
Why is this so? INNER JOIN will return data that exists in two tables at the same time. If Students has course numbers 1, 2, 3, and 5, and Courses also has course numbers 1, 2, 3, and 4, then the result is the intersection set 1, 2, and 3. RIGHT JOIN returns the records that exist in the right table. If some rows in the right table do not exist in the left table, those rows in the result will be NULL.
LEFT JOIN will return the numbers that exist in the left table. If CourseID=10 is added to Students, even if there is no course record with ID 10 in the Courses table, the result of LEFT JOIN will still have one more row of records, and its corresponding CourseName is NULL. (Supplement: LEFT JOIN is called LEFT OUT JOIN in some databases. Similarly, RIGHT JOIN may also be called RIGHT OUT JOIN.)
FULL JOIN
The last type of JOIN is FULL JOIN. The result set will select all the records of the two tables, and automatically add the columns that do not exist in the two tables to NULL.
In order to help you understand the logic of connection query, you can refer to the following diagram. The circle on the left can be understood as the left table, and the circle on the right can be understood as the right table.
The above is a basic tutorial on SQL syntax. Now that you have learned how to use SQL to create tables and records, and use advanced SQL statements to perform complex queries, in the next chapter we will start learning to use the real database software MySQL.
Homework
Please write a SQL statement to find out the number of students and course names who have joined CourseID 1. Please note that we are only looking for the number of male students who are older than 20 years old.
Method 1 Use Where
SELECT COUNT(*) AS StudentsNum,CourseName
FROM Students AS S,Courses AS C
WHERE S.CourseID = C.CourseID AND S.CourseID = 1 AND S.Age > 20 AND S.Gender = 'M';
Method 2 Group by + where
SELECT COUNT(*) AS StudentsNum, CourseName
FROM Students LEFT JOIN Courses ON Students.CourseID = Courses.CourseID
WHERE Students.Age > 20 AND Students.Gender = 'M'
GROUP BY Students.CourseID;
review
- 1 Determine the main table, use FROM table_name
- 2 Then confirm the connected table and use INNER JOIN table_name
- 3 Determine the connection conditions and use ON condition. The condition of the above statement is S.CourseID = C.CourseID
- 4 Finally, we can add: WHERE, ORDER BY and other clauses
. In addition to INNER JOIN, we also have LEFT JOIN, RIGHT JOIN, and FULL JOIN.
reference
[SQL advanced query statements: aggregation query, multi-table query, connection query [Relational Database SQL Tutorial 5]] https://www.bilibili.com/video/BV1Zp4y1Q7mj/?share_source=copy_web&vd_source=fe6c23f6f1353ed1eff5d5e866171572