Data Application Master's SQL Basic Tutorial Sharing 6-Comparison and Logical Operation

Chapter 3 Operator-Conditional Queries in SQL


(There are many operators in SQL, they are all used to satisfy SQL queries, especially for specifying conditions for WHERE statements, so in this chapter, we will explain SQL operators one by one in conjunction with the use of WHERE statements)

1. Comparison operation

Bibi just knows

1. Students who study plants - equal, not equal

【Introduction to knowledge points】

The comparator is based on the comparison of two values, and the same is true in SQL, there are equal, not equal, greater than, less than and other comparators.
Let's first understand equality and inequality:
Equal and inequality are a common set of comparison operation symbols in SQL. Equality was mentioned when the WHERE statement was introduced in the previous section.
Equal means = judges whether two values ​​are equal, returns TRUE if they are equal, otherwise returns FALSE;
not equal means =! (or <>) Judges whether two values ​​are not equal, returns TRUE if they are not equal, otherwise returns FALSE.

In addition, we need to know one thing:
when the data to be compared is a string type, the comparison value needs to be quoted with '', and the numeric type can be compared directly.

【Example】

Use the = symbol to query the information of students whose major is Botany (Botany) in the Student table, and sort by credits.

SELECT * FROM Student
WHERE Major = 'Botany'
ORDER BY Credit;

 

Remember to add '' for string type data.

2. Greater than, less than

【Introduction to knowledge points】

Greater than and less than are also commonly used comparison operators in SQL. They are also a set of operations for comparing values. The symbols are as follows:
greater than >
less than <
not greater than <=
not less than >=

【Example】

Query the information of students whose credit ID is not less than 28.
Use >= to query the student table.

SELECT * FROM Student
WHERE Credit >= 28; -- Credit is an integer number, so no quotes are needed

 

2. Logic operation

logical thinking

1、IS NULL

【Introduction to knowledge points】

Logical operators can judge and compare each column, and return TRUE or FALSE according to the judgment results, such as IS NULL and other logical operators in this section.
The role of IS NULL is that it can be used to search whether the value in the column is NULL.

【Example】

Query the student information that is not filled in the major in the student form.

SELECT * FROM Student
WHERE Major IS NULL;

 

2、BETWEEN AND

【Introduction to knowledge points】

The BETWEEN AND operator is used to find the value between two specified values ​​(maximum and minimum), and the returned result is a closed interval, that is, contains the maximum and minimum values.
We can use this operator to perform range queries on lists, such as employee information within a certain salary range, student information within a certain score range, and so on.

【Example】

Find information about students with credits between 20 and 24.

SELECT * FROM Student
WHERE Credit BETWEEN 20 AND 24;

 

3、IN

【Introduction to knowledge points】

The IN operator compares a column's value to a list of values ​​and returns True as long as one of the column's values ​​matches it.

【Example】

Find information about students with 28, 29 or 30 credits.

SELECT * FROM Student
WHERE Credit IN (28,29,30);

 

If the match list is a string type of data, it needs to be quoted.

4. LIKE and wildcards

【Introduction to knowledge points】

The LIKE operator is used to search the specified pattern of the query column, and the wildcard is used to replace one or more characters.
In the database, LIKE and wildcards need to be used in combination, and wildcards are the contents of the specified pattern of the LIKE query.
The syntax is as follows:

SELECT column name FROM table name
WHERE column name LIKE specified mode;

 

The rules for wildcards are as follows:


 

If we want to query the student information whose name starts with H in the student table, we can use the WHERE clause like this:

WHERE SName LIKE 'H%';

 

【Example】

In the query student table, the length of the name is 4 and the last letter of the student is y.

SELECT * FROM Student
WHERE SName LIKE '___y';

 

3 underscores specify letters of 3 lengths.

 

To be continued below. . . . . .

 

Welcome to visit our official website:

http://www.datanew.com/datanew/homepage

http://www.lechuangzhe.com/homepage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326265774&siteId=291194637