A single-table query is performed only in one table. You can perform single-table queries to retrieve, filter, and sort data.
This topic describes how to use the SELECT statement to perform single-table queries on data in the MySQL mode of OceanBase Database.
Syntax
The syntax of a single-table query is as follows:
SELECT [ALL | DISTINCT] select_list FROM table_name
[ WHERE query_condition ]
[ GROUP BY group_by_expression ]
[ HAVING group_condition ]
[ ORDER BY column_list ][ASC | DESC]
[ LIMIT limit_clause ]
column_list:
column_name[,column_name...]
For more information about how to use the SELECT statement, see SELECT.
When the keywords WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT are used together, there are specific restrictions on the order. The execution order of these keywords is as follows:
Execute
FROMto find the required table.Execute
WHEREto specify the constraint conditions.Execute
GROUP BYto group or aggregate each retrieved record. IfGROUP BYis not executed, all records are considered a group.Execute
HAVINGto filter the grouped results.Execute
SELECT.Execute
DISTINCTto remove duplicate rows.Execute
ORDER BYto sort the results in ascending or descending order based on the conditions.Execute
LIMITto limit the number of records.
Notice
The difference between WHERE and HAVING is that WHERE filters data before grouping or aggregation, while HAVING filters data after grouping and returns the entire query results.
Examples
Assume that there is a student information table student, which includes the following fields: student id (id), student name (name), gender (gender), age (age), score (score), and enrollment date (enrollment_date). The gender field is represented by the numbers 0 and 1 for female and male, respectively.
The data is as follows:
+----+-----------+--------+-----+-------+-----------------+
| id | name | gender | age | score | enrollment_date |
+----+-----------+--------+-----+-------+-----------------+
| 1 | Emma | 0 | 20 | 85 | 2021-09-01 |
| 2 | William | 1 | 21 | 90.5 | 2021-09-02 |
| 3 | Olivia | 0 | 19 | 95.5 | 2021-09-03 |
| 4 | James | 1 | 20 | 87.5 | 2021-09-03 |
| 5 | Sophia | 0 | 20 | 91.5 | 2021-09-05 |
| 6 | Benjamin | 1 | 21 | 96.5 | 2021-09-01 |
| 7 | Ava | 0 | 22 | 89.5 | 2021-09-06 |
| 8 | Michael | 1 | 18 | 93.5 | 2021-09-08 |
| 9 | Charlotte | 1 | 19 | 88 | 2021-09-06 |
| 10 | Ethan | 1 | 20 | 92 | 2021-09-01 |
+----+-----------+--------+-----+-------+-----------------+
SELECT queries
Query all columns
You can execute the
SELECT * FROM student;statement to query all student information.You can also manually list all the fields by using the statement
SELECT id, name, gender, age, score, enrollment_date FROM student;.
Note
Although you can use an asterisk (*) to quickly list all fields, manually listing all fields in the statement improves query performance and code readability and maintainability.
Example 1: Query the data of all rows in the student table, and return the data from the id, name, gender, age, score, and enrollment_date columns in each row.
SELECT id,name,gender,age,score,enrollment_date FROM student;
The result is as follows:
+----+-----------+--------+-----+-------+-----------------+
| id | name | gender | age | score | enrollment_date |
+----+-----------+--------+-----+-------+-----------------+
| 1 | Emma | 0 | 20 | 85 | 2021-09-01 |
| 2 | William | 1 | 21 | 90.5 | 2021-09-02 |
| 3 | Olivia | 0 | 19 | 95.5 | 2021-09-03 |
| 4 | James | 1 | 20 | 87.5 | 2021-09-03 |
| 5 | Sophia | 0 | 20 | 91.5 | 2021-09-05 |
| 6 | Benjamin | 1 | 21 | 96.5 | 2021-09-01 |
| 7 | Ava | 0 | 22 | 89.5 | 2021-09-06 |
| 8 | Michael | 1 | 18 | 93.5 | 2021-09-08 |
| 9 | Charlotte | 1 | 19 | 88 | 2021-09-06 |
| 10 | Ethan | 1 | 20 | 92 | 2021-09-01 |
+----+-----------+--------+-----+-------+-----------------+
10 rows in set
Query specified columns
You can query data in specified columns based on column names.
Example 2: Query the data of all rows in the student table, and return the data from the id and name columns in each row.
SELECT id,name FROM student;
The result is as follows:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Emma |
| 2 | William |
| 3 | Olivia |
| 4 | James |
| 5 | Sophia |
| 6 | Benjamin |
| 7 | Ava |
| 8 | Michael |
| 9 | Charlotte |
| 10 | Ethan |
+----+-----------+
10 rows in set
Query calculated values
You can process the data of specified columns in a query.
Example 3: Query the data of the id, name, and age columns of all rows in the student table, and increase the value of the age column in each row by 1. The result will contain three columns: id, name, and age+1.
SELECT id,name,age+1 FROM student;
For more information about how to process data of specified columns in a query, see Use operators and functions in queries.
Conditional queries
To query data that satisfies specific conditions, add a WHERE clause to the SELECT statement.
The WHERE keyword can be followed by one or more conditions. These conditions are used to filter the data, and only the data that satisfies the WHERE conditions will be returned.
The commonly used query conditions in the WHERE clause are shown in the table below.
| Query condition type | Predicate |
|---|---|
| Comparison query | =, >, <, >=, <=, !=, and <> |
| Logical query (multiple conditions supported in a query) | AND, OR, and NOT |
| Fuzzy query (matching by characters) | LIKE and NOT LIKE |
| Interval query (with a specified range) | BETWEEN AND and NOT BETWEEN AND |
| Query with a specified set | IN and NOT IN |
| NULL value query | IS NULL and IS NOT NULL |
For more information about query conditions, see Comparison operators.
Queries with comparison operators
Comparison operators include equal to (=), greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), and not equal to (!= and <>).
Equal to (
=): Query data from the specified column that is equal to the target value. If the value is of string type, it needs to be enclosed in single or double quotes.Example 4: Query all rows in the
studenttable wheregendervalues are equal to1, and return the data from theid,name, andgendercolumns in these rows.SELECT id,name,gender FROM student WHERE gender = 1;The result is as follows:
+----+-----------+--------+ | id | name | gender | +----+-----------+--------+ | 2 | William | 1 | | 4 | James | 1 | | 6 | Benjamin | 1 | | 8 | Michael | 1 | | 9 | Charlotte | 1 | | 10 | Ethan | 1 | +----+-----------+--------+ 6 rows in setNot equal to (
<>and!=): Not equal to operators include<>and!=.Example 5: Query all rows in the
studenttable wheregendervalues are not equal to1, and return the data from theid,name, andgendercolumns in these rows.SELECT id,name,gender FROM student WHERE gender <> 1;The result is as follows:
+----+--------+--------+ | id | name | gender | +----+--------+--------+ | 1 | Emma | 0 | | 3 | Olivia | 0 | | 5 | Sophia | 0 | | 7 | Ava | 0 | +----+--------+--------+ 4 rows in setGreater than (
>) and less than (<): The greater than operator (>) and the less than operator (<) compare numbers based on their values. If characters are compared, they are converted into their respective ASCII codes, and then the ASCII codes are compared from left to right.Note
The greater than or equal to (>=) and less than or equal to (<=) operators operate in a similar manner.
Example 6: Query all rows in the
studenttable wherescorevalues are less than90, and return the data from theid,name, andscorecolumns in these rows.SELECT id,name,score FROM student WHERE score < 90;The result is as follows:
+----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 1 | Emma | 85 | | 4 | James | 87.5 | | 7 | Ava | 89.5 | | 9 | Charlotte | 88 | +----+-----------+-------+ 4 rows in set
Queries with logical conditions
Logical query operators AND and OR support queries with multiple conditions.
AND: Return data that satisfies both conditions withAND.Example 7: Query all rows in the
studenttable wheregendervalues are equal to1andscorevalues are less than or equal to90, and return the data from theid,name,gender, andscorecolumns in these rows.SELECT id,name,gender,score FROM student WHERE gender = 1 AND score <= 90;The result is as follows:
+----+-----------+--------+-------+ | id | name | gender | score | +----+-----------+--------+-------+ | 4 | James | 1 | 87.5 | | 9 | Charlotte | 1 | 88 | +----+-----------+--------+-------+ 2 rows in setOR: Return data that satisfies either one of the conditions.Example 8: Query all rows in the
studenttable wheregendervalues are equal to1orscorevalues are less than90, and return the data from theid,name,gender, andscorecolumns in these rows.SELECT id,name,gender,score FROM student WHERE gender = 1 OR score < 90;The result is as follows:
+----+-----------+--------+-------+ | id | name | gender | score | +----+-----------+--------+-------+ | 1 | Emma | 0 | 85 | | 2 | William | 1 | 90.5 | | 4 | James | 1 | 87.5 | | 6 | Benjamin | 1 | 96.5 | | 7 | Ava | 0 | 89.5 | | 8 | Michael | 1 | 93.5 | | 9 | Charlotte | 1 | 88 | | 10 | Ethan | 1 | 92 | +----+-----------+--------+-------+ 8 rows in set
Fuzzy queries (LIKE)
The predicate LIKE can be used for string matching.
The syntax means finding data that matches the corresponding column value with the pattern. The pattern can be a complete string or contain wildcards % and _, where:
The underscore (
_) exactly matches any character in the value.The percent sign (
%) matches zero or multiple characters in the value. The pattern%cannot matchNULL.
Notice
If the database character set uses ASCII, one Chinese character requires two underscores (_); if the database character set uses GBK, one Chinese character requires only one underscore (_).
Example 9: Query all rows in the student table where name values contain am, and return the data from the id and name columns in these rows.
SELECT id,name FROM student WHERE name LIKE '%am%';
The result is as follows:
+----+----------+
| id | name |
+----+----------+
| 2 | William |
| 4 | James |
| 6 | Benjamin |
+----+----------+
3 rows in set
Range queries (BETWEEN AND)
The BETWEEN AND operator selects data between two values. These values can be numerals, literals, or dates.
Notice
Do not swap the two boundary values of a range query. The left boundary value should be greater than or equal to the starting point, and the right boundary value should be less than or equal to the ending point.
Example 10: Query all rows in the student table where score values range from 85 to 90, and return the data from the id, name, and score columns in these rows.
SELECT id,name,score FROM student WHERE score BETWEEN 85 AND 90;
The result is as follows:
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 1 | Emma | 85 |
| 4 | James | 87.5 |
| 7 | Ava | 89.5 |
| 9 | Charlotte | 88 |
+----+-----------+-------+
4 rows in set
Queries with a specified set (IN)
The IN operator is used to specify multiple values as a set in a WHERE clause. It returns data from the specified column that matches any value in the set. On the other hand, the NOT IN operator returns data from the specified column that does not match any value in the set.
Notice
- The value in the
[NOT] INset must be of the same type or compatible with each other. - The values in the
[NOT] INset do not support wildcards.
Example 11: Query all rows in the student table where id values belong to the set of (1, 3, 5, 7), and return the data from the id and name columns in these rows.
SELECT id,name FROM student WHERE id IN (1,3,5,7);
The result is as follows:
+----+--------+
| id | name |
+----+--------+
| 1 | Emma |
| 3 | Olivia |
| 5 | Sophia |
| 7 | Ava |
+----+--------+
4 rows in set
NULL value queries
Due to the inaccurate results obtained when using comparison operators, LIKE, BETWEEN AND, IN, and NOT IN to query for NULL values, we recommend that you use the dedicated query statements IS NULL and IS NOT NULL for NULL value queries. Additionally, the safe equal operator (<=>) can be used to compare both normal values and NULL values.
IS NULL: Query for data where the specified column has aNULLvalue.Example 12: Query all rows in the
studenttable wherescoreis NULL, and return the data from theid,name, andscorecolumns in these rows.SELECT id,name,score FROM student WHERE score IS NULL;The result is as follows:
Empty setIS NOT NULL: Query for data where the specified column has a non-NULL value.Example 13: Query all rows in the
studenttable where the score is notNULL, and return the data from theid,name, andscorecolumns in these rows.SELECT id,name,score FROM student WHERE score IS NOT NULL;The result is as follows:
+----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 1 | Emma | 85 | | 2 | William | 90.5 | | 3 | Olivia | 95.5 | | 4 | James | 87.5 | | 5 | Sophia | 91.5 | | 6 | Benjamin | 96.5 | | 7 | Ava | 89.5 | | 8 | Michael | 93.5 | | 9 | Charlotte | 88 | | 10 | Ethan | 92 | +----+-----------+-------+ 10 rows in set
GROUP BY queries
The GROUP BY clause is used to group the query results.
GROUP BY supports grouping by a single field or multiple fields. You can also use the WHERE clause to filter data before grouping, use the HAVING clause to filter data after grouping, and use the ORDER BY clause to sort data after grouping.
The following table lists frequently-used aggregate functions in GROUP BY queries.
| Aggregate function | Description |
|---|---|
| MAX() | Queries the maximum value of the specified column. |
| MIN() | Queries the minimum value of the specified column. |
| COUNT() | Returns the number of rows in the query result. |
| SUM() | Returns the sum of the specified column. |
| AVG() | Returns the average value of the data in the specified column. |
Examples
Assume that the fruit_order table contains the following data:
+----------+---------+-----------+-------------+------------+
| order_id | user_id | user_name | fruit_price | order_year |
+----------+---------+-----------+-------------+------------+
| 1 | 1011 | A1 | 13.11 | 2019 |
| 2 | 1022 | B2 | 23.34 | 2019 |
| 3 | 1022 | B2 | 12.22 | 2019 |
| 4 | 1011 | A1 | 22.21 | 2020 |
| 5 | 1033 | C3 | 51.55 | 2020 |
| 6 | 1011 | A1 | 58.83 | 2020 |
| 7 | 1022 | B2 | 14.66 | 2021 |
| 8 | 1022 | B2 | 34.44 | 2021 |
| 9 | 1033 | C3 | 63.66 | 2021 |
+----------+---------+-----------+-------------+------------+
GROUP BYquery based on a single fieldExample 14: Query the number of orders placed by each customer and return the data of
user_idandCOUNT(order_id).SELECT user_id, COUNT(order_id) FROM fruit_order GROUP BY user_id;The result is as follows:
+---------+-----------------+ | user_id | COUNT(order_id) | +---------+-----------------+ | 1011 | 3 | | 1022 | 4 | | 1033 | 2 | +---------+-----------------+ 3 rows in setGROUP BYquery based on multiple fieldsExample 15: Query the number of orders placed by each customer each year and return the data of
user_id,order_year, andCOUNT(order_id).SELECT user_id,order_year, COUNT(order_id) FROM fruit_order GROUP BY user_id,order_year;The result is as follows:
+---------+------------+-----------------+ | user_id | order_year | COUNT(order_id) | +---------+------------+-----------------+ | 1011 | 2019 | 1 | | 1022 | 2019 | 2 | | 1011 | 2020 | 2 | | 1033 | 2020 | 1 | | 1022 | 2021 | 2 | | 1033 | 2021 | 1 | +---------+------------+-----------------+ 6 rows in setFilter data before grouping
Example 16: Query the number of orders placed by each customer in 2020 and return the data of
user_idandCOUNT(order_id).SELECT user_id, COUNT(order_id) FROM fruit_order t WHERE t.order_year = 2020 GROUP BY user_id;The result is as follows:
+---------+-----------------+ | user_id | COUNT(order_id) | +---------+-----------------+ | 1011 | 2 | | 1033 | 1 | +---------+-----------------+ 2 rows in setFilter data after grouping
Example 17: Query customers who placed two or more orders in 2019 and return the data of
user_idandCOUNT(order_id).SELECT user_id, COUNT(order_id) FROM fruit_order t WHERE t.order_year = 2019 GROUP BY user_id HAVING COUNT(order_id)>=2;Note
When a
GROUP BYquery includes theHAVINGclause, it first obtains the SQL query results without theHAVINGclause, and then uses theHAVINGcondition to filter the data based on these results, and finally returns the filtered data. Therefore, afterHAVING, you can use aggregate functions, and these aggregate functions do not have to be the same as the aggregate functions afterSELECT.The result is as follows:
+---------+-----------------+ | user_id | COUNT(order_id) | +---------+-----------------+ | 1022 | 2 | +---------+-----------------+ 1 row in setSort data after grouping
Example 18: Query the maximum order amount of each customer and return the data of
user_idandMAX(fruit_price)sorted by maximum order amount in descending order.SELECT user_id, MAX(fruit_price) FROM fruit_order t GROUP BY user_id ORDER BY MAX(fruit_price) DESC;The result is as follows:
+---------+------------------+ | user_id | MAX(fruit_price) | +---------+------------------+ | 1033 | 63.66 | | 1011 | 58.83 | | 1022 | 34.44 | +---------+------------------+ 3 rows in set
Aggregate queries
For more information about aggregate queries, see Use aggregate functions in queries.
ORDER BY queries
The ORDER BY clause is used to sort the query results in ascending (ASC) or descending (DESC) order based on one or more attribute columns. The default order is ascending.
ORDER BY supports sorting by a single field, multiple fields, an alias, or a function. Separate multiple fields with commas (,).
If the ASC or DESC keyword is not added in an ORDER BY query, the query results are sorted in ascending order by default.
Examples
Querying data in the student table where the score value is between 85 and 90.
Sorting by a single field
Example 19: Query student information in the
studenttable and return the information sorted byscorein ascending order.SELECT id,name,score FROM student ORDER BY score;The result is as follows:
+----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 1 | Emma | 85 | | 4 | James | 87.5 | | 9 | Charlotte | 88 | | 7 | Ava | 89.5 | | 2 | William | 90.5 | | 5 | Sophia | 91.5 | | 10 | Ethan | 92 | | 8 | Michael | 93.5 | | 3 | Olivia | 95.5 | | 6 | Benjamin | 96.5 | +----+-----------+-------+ 10 rows in setExample 20: Query student information in the
studenttable and return the information sorted byscorein descending order.SELECT id,name,score FROM student ORDER BY score DESC;The result is as follows:
+----+-----------+-------+ | id | name | score | +----+-----------+-------+ | 6 | Benjamin | 96.5 | | 3 | Olivia | 95.5 | | 8 | Michael | 93.5 | | 10 | Ethan | 92 | | 5 | Sophia | 91.5 | | 2 | William | 90.5 | | 7 | Ava | 89.5 | | 9 | Charlotte | 88 | | 4 | James | 87.5 | | 1 | Emma | 85 | +----+-----------+-------+ 10 rows in setSorting by multiple fields
Example 21: Query student information in the
studenttable and return the information sorted byscorein descending order and byenrollment_datein ascending order.SELECT id,name,score,enrollment_date FROM student ORDER BY score DESC,enrollment_date ASC;The result is as follows:
+----+-----------+-------+-----------------+ | id | name | score | enrollment_date | +----+-----------+-------+-----------------+ | 6 | Benjamin | 96.5 | 2021-09-01 | | 3 | Olivia | 95.5 | 2021-09-03 | | 8 | Michael | 93.5 | 2021-09-08 | | 10 | Ethan | 92 | 2021-09-01 | | 5 | Sophia | 91.5 | 2021-09-05 | | 2 | William | 90.5 | 2021-09-02 | | 7 | Ava | 89.5 | 2021-09-06 | | 9 | Charlotte | 88 | 2021-09-06 | | 4 | James | 87.5 | 2021-09-03 | | 1 | Emma | 85 | 2021-09-01 | +----+-----------+-------+-----------------+ 10 rows in setSorting by using functions
Example 22: Query student information in the
studenttable and return the information sorted byscorein descending order and byDAY(enrollment_date)in ascending order.SELECT id,name,score,enrollment_date FROM student ORDER BY score DESC,DAY(enrollment_date) ASC;The result is as follows:
+----+-----------+-------+-----------------+ | id | name | score | enrollment_date | +----+-----------+-------+-----------------+ | 6 | Benjamin | 96.5 | 2021-09-01 | | 3 | Olivia | 95.5 | 2021-09-03 | | 8 | Michael | 93.5 | 2021-09-08 | | 10 | Ethan | 92 | 2021-09-01 | | 5 | Sophia | 91.5 | 2021-09-05 | | 2 | William | 90.5 | 2021-09-02 | | 7 | Ava | 89.5 | 2021-09-06 | | 9 | Charlotte | 88 | 2021-09-06 | | 4 | James | 87.5 | 2021-09-03 | | 1 | Emma | 85 | 2021-09-01 | +----+-----------+-------+-----------------+ 10 rows in setSorting after the
WHEREclauseUse the
WHEREclause to filter data before sorting.Example 22: Query information about students whose
scorevalues are greater than85in thestudenttable and return the information sorted byDAY(enrollment_date)in ascending order.SELECT id,name,score,DAY(enrollment_date) FROM student WHERE score > 85 ORDER BY DAY(enrollment_date) ASC;The result is as follows:
+----+-----------+-------+----------------------+ | id | name | score | DAY(enrollment_date) | +----+-----------+-------+----------------------+ | 6 | Benjamin | 96.5 | 1 | | 10 | Ethan | 92 | 1 | | 2 | William | 90.5 | 2 | | 3 | Olivia | 95.5 | 3 | | 4 | James | 87.5 | 3 | | 5 | Sophia | 91.5 | 5 | | 7 | Ava | 89.5 | 6 | | 9 | Charlotte | 88 | 6 | | 8 | Michael | 93.5 | 8 | +----+-----------+-------+----------------------+ 9 rows in set
Limit the number of query results
The LIMIT clause can be used to limit the number of rows returned by a SELECT query, and is commonly used for pagination operations.
The syntax of the SQL queries that include the LIMIT clause is as follows:
LIMIT [offset,] count_num
where
offsetspecifies the number of rows to skip. It is optional. The default value ofoffsetis 0, which specifies to skip zero rows. The value range is [0,+∞).count_numspecifies the number of rows to retrieve after the rows specified byoffsetare skipped. The value range is [0,+∞).The values of
offsetandcount_numcannot be specified by using expressions, and can be set only to numbers.The number following
LIMITcannot be negative.
Queries that use LIMIT n, m
Retrieve the first m rows of records
Example 23: Query the first five records of the
idandnamecolumns in thestudenttable.SELECT id,name FROM student LIMIT 5;The result is as follows:
+----+---------+ | id | name | +----+---------+ | 1 | Emma | | 2 | William | | 3 | Olivia | | 4 | James | | 5 | Sophia | +----+---------+ 5 rows in setRetrieve the m rows of records after skipping n rows
Note
When the number of remaining rows after skipping n rows is less than m, the query result will include all the remaining data.
Example 23: Query the three rows of the
idandnamecolumns after the third row in thestudenttable.SELECT id,name FROM student LIMIT 3,3;The result is as follows:
+----+----------+ | id | name | +----+----------+ | 4 | James | | 5 | Sophia | | 6 | Benjamin | +----+----------+ 3 rows in setRetrieve the row with the maximum value
Example 24: To retrieve the row with the maximum
scorevalue in thestudenttable, you can sort data in the table byscorein descending order and then retrieve the first row.SELECT id,name,score FROM student ORDER BY score DESC LIMIT 1;The result is as follows:
+----+----------+-------+ | id | name | score | +----+----------+-------+ | 6 | Benjamin | 96.5 | +----+----------+-------+ 1 row in set
Paging queries
The LIMIT clause can be used based on the logic of paging queries.
The SQL syntax is as follows:
LIMIT (page_no - 1) * page_size, page_size;
where
page_nospecifies the page number that starts from 1. The value range is [1,+∞).page_sizespecifies the number of records per page. The value range is [1,+∞). For example, ifpage_nois set to 5 andpage_sizeis set to 10, the 10 records on page 5 are retrieved.
Example 25: In the student table, set page_size to 2 to retrieve data of page 1, page 2, and page 3.
Page 1:
SELECT id,name FROM student ORDER BY id LIMIT 0,2;
The result is as follows:
+----+---------+
| id | name |
+----+---------+
| 1 | Emma |
| 2 | William |
+----+---------+
2 rows in set
Page 2:
SELECT id,name FROM student ORDER BY id LIMIT 2,2;
The result is as follows:
+----+--------+
| id | name |
+----+--------+
| 3 | Olivia |
| 4 | James |
+----+--------+
2 rows in set
Page 3:
SELECT id,name FROM student ORDER BY id LIMIT 4,2;
The result is as follows:
+----+----------+
| id | name |
+----+----------+
| 5 | Sophia |
| 6 | Benjamin |
+----+----------+
2 rows in set