Article directory
content
Example: Prepare tables and basic data first
Select a specific column in a query
Using Arithmetic Expressions in Queries
aliases for columns used in queries
Use the DISTINCT operator in a query
Comparison operators: IN, NOT IN
Limit the priority of query operators
foreword
Hello everyone, I am the color of ice three points. Personal homepage: blog of ice three colors
This article talks about some knowledge and examples about single-table queries.
Friends passing by, please like and follow before walking. Welcome to the comment area to communicate. It is never too late to start working hard, so it is better to start with this article!
Let's grow together! refill
5.1 Single table query
Single-table query refers to querying the data of a table.
Example: Prepare tables and basic data first
This table must be present, otherwise an error will be reported when creating the employee table below.
--employee table
CREATE TABLE IF NOT EXISTS employee (
empno iNT(4) NOT NULL AUTO_INCREMENT,
ename CHAR(8) NOT NULL COMMENT 'employee name',
job VARCHAR(20) NOT NULL COMMENT 'job',
mgr INT(4) COMMENT 'Manager',
hiredate DATE NOT NULL COMMENT'hired date',
sal FLOAT(7,2) NOT NULL COMMENT 'salary',
comm VARCHAR(20) COMMENT 'Remark',
deptno iNT(2) NOT NULL COMMENT 'Department number',
CONSTRAINT pk_emp PRIMARY KEY(empno),
CONSTRAINT fk_emp_dept FOREIGN KEY (deptno)REFERENCES department(deptno)
);
INSERT INTO employee VALUES
(7369,'SMITH','CLERK',7902,'2008-12-17',800,NULL,20),
(7521,'WARD','SALESMAN',7698,'2008-04-22',1250,500,30),
(7566,'JONES','MANAGER',7839,'2008-03-02',2975,NULL,20),
(7654,'MARTIN','SALESMAN',7698,'2008-09-28',1250,1400,30),
(7698,'BLAKE','MANAGER',7839,'2008-05-01',2850,NULL,30),
(7839,'KING' ,'PRESIDENT',NULL,'2008-01-17',5000,NULL,10),
(7844,'TURNER' ,'SALESMAN',7698,'2008-09-08',1500,0,30),
(7876,'ADAMS','CLERK',7788,'2009-03-23',1100,NULL,20),
(7900,'JAMES','CLERK',7698,'2008-12-03' ,950,NULL,30),
(7902,'FORD' ,'ANALYST',7566,'2008-12-03',3000,NULL,20);
OK! Get the data ready to start querying
Select a specific column in a query
Query all the information of all employees in the employee table (*indicates all columns of the query data table)
SELECT *FROM employee;
Query employee number, name, monthly salary. Query part of the columns (Columns are separated by commas, and the order of the columns determines the display order of the columns.)
SELECT empno, ename, sal FROM employee;
Using Arithmetic Expressions in Queries
Use arithmetic expressions to calculate an increase of 500 yuan per employee's monthly salary
SELECT empno, ename, sal,500+sal FROM employee;
Use arithmetic expressions to calculate the annual salary after the monthly salary is increased by 500 yuan
SELECT empno, ename,(500+sal)*12 FROM employee;
aliases for columns used in queries
Use AS or a space between the column name and the alias, which may or may not be in quotes. Quotes are required if the alias contains special characters.
Column alias (you can give the column a name that comes to your mind)
SELECT empno AS 'employee number',ename 'Employee Name',(500+sal)*12 annual salary FROM employee;
Use the DISTINCT operator in a query
DISTINCT operator is used to remove duplicate records of specified column data
SELECT DISTINCT job FROM employee;
Qualified query
Restricted query refers to setting a series of filter conditions when querying data, and only data that meets the specified conditions will be displayed.
WHERE is used to form a conditional expression that restricts the row data in the retrieved table;
Followed by the conditional expression for data filtering. In the conditional expression, relational operators, logical operators, BETWEEN...AND range query operators, IN (NOT IN) list range query operators, and NULL value judgment operators can be used. , LIKE fuzzy query operator, etc.
Query the names, monthly salaries and annual salaries of all employees in the SALES department (department number 30) (assuming the department's year-end bonus is 10,000/person)
SELECT ename,sal,12*sal+10000 AS 年薪 FROM employee WHERE deptno=30;
Query the list of employees with salary between 1500 and 2900
SELECT empno,ename,sal FROM employee WHERE sal >= 1500 AND sal<=2900;
BETWEEN...AND operator
Mainly used to query against a specified data range. The data in the specified range can be numbers, strings or dates.
Query the list of employees with salary between 1500 and 2900
SELECT empno,ename,sal FROM employee WHERE sal BETWEEN 1500 AND 2900;
Find employees who joined the company between January 1, 1981 and December 31, 1981
SELECT empno,ename,hiredate FROM employee WHERE hiredate BETWEEN '2008-12-01'AND '2008-12-31';
Data outside the specified range uses the NOT BETWEEN...AND operator
Query for employees who did not join the company between January 1, 1981 and December 31, 1981
SELECT empno,ename,hiredate FROM employee
WHERE hiredate NOT BETWEEN '2008-12-01'AND '2008-12-31';
Comparison operators: IN, NOT IN
IN is used to test if certain values appear in the list. NOT IN is used to test if some value does not appear in the list
Find people whose job title is Sales, Clerk, or Manager.
SELECT empno,ename,job FROM employee
WHERE job IN('SALESMAN','CLERK' ,'MANAGER');
NULL
In SQL is a special value called null. It is neither 0 nor space. Its value is undefined, unknown, and indeterminate. Use the IS keyword to judge whether it is NULL, and cannot use "=".
Query employee information with bonus (COMM column value is not NULL)
SELECT empno,ename,job,sal,comm FROM employee WHERE comm IS NOT NULL;
LIKE fuzzy query
Fuzzy queries are used when the memory of the data being queried is not very clear. Fuzzy queries use the LIKE operator. (This was mentioned before) LIKE is followed by a match condition, enclosed in quotation marks, consisting of keywords and wildcards. NOT LIKE means that the fuzzy query is not satisfied.
Note: There are the following two wildcards in the LIKE clause: %: can match characters of any type and length (0 or more). _: matches a single arbitrary character. Often used to limit the character length of an expression.
Query employee information whose name begins with J, the last two characters of the name are E, and an arbitrary character
SELECT empno,ename,job FROM employee WHERE ename LIKE 'J%E_';
Limit the priority of query operators
In order from high to low:
Arithmetic Operators > Concatenation Operators > Comparison Operators > IS NULL, IS NOT NULL > LIKE, NOT LIKE, IN, NOT IN > BETWEEN, NOT BETWEEN > NOT > AND > OR
ORDER BY
When performing a query operation, the row data is displayed in the order in which the row data was inserted by default. In practical applications, it is often necessary to sort data to display more intuitive data. Sorting is using the ORDER BY clause. WHERE means condition.
Note: ASC stands for ascending order, which is the default value and can be omitted; DESC stands for descending order.
Query the salary and bonus information of employees in the sales department (No. 30), the salary is sorted in descending order
SELECT ename,job,sal,comm FROM employee WHERE deptno=30 ORDER BY sal DESC;
limit limit query
Used to limit the number of query result records. Limit is followed by a numerical value, which is omitted from the initial record index value, and only displays how many records there are in total. Followed by two values, the first value is the initial record index value, and the second value is the total number of records.
Query the information of the three highest-paid employees in department 20.
SELECT * FROM employee WHERE deptno=20 ORDER BY sal DESC LIMIT 3;
Query the information of the 2nd and 3rd employees with the highest salary in the department 20
SELECT * FROM employee WHERE deptno=20 ORDER BY sal DESC LIMIT 1,2;
GROUPBY query
The GROUP BY clause can group query results in row direction by attribute column or combination of attribute columns, each group having the same aggregate value on the attribute column or combination of attribute columns.
Note: The GROUP BY clause is usually used in conjunction with aggregate functions to perform query statistics operations. If the GROUP BY keyword is used alone, the query result only displays one record per group, which is usually meaningless.
Every column that appears in the query's SELECT list must also appear in the GROUP BY clause.
SELECT deptno, count(*)FROM employee GROUP BY deptno;