The SELECT command is used to retrieve data stored in tables in MySQL. It
allows for various operations, from simple calculations to more elaborate queries
in databases.
In the first examples, we will only work with MySQL’s internal operations, without involving
tables. Later, we will explore commands applied to tables, which require adapting
the names according to the context of the database being used.
Simple Arithmetic Operations
These operations use MySQL as a calculator, allowing for sums, multiplications, and other basic mathematical functions.
1 | |
The query SELECT FORMAT(3/7,12) divides 3 by 7 and uses the FORMAT function to display the result with 12 decimal places.
In the query SELECT FORMAT(POWER(2,64),0), the POWER function calculates 2 raised to the power of 64.
Here, FORMAT is used to display the number without any decimal part, making the result more organized.
Logical and Boolean Operators
Logical operators, such as AND (conjunction), OR (disjunction), and NOT (negation), are used to combine or modify conditions.
They work with boolean values, where 1 represents true and 0 represents false.
For example, AND returns true only if all conditions are true, while OR returns true if at least one condition is true.
The NOT operator inverts the logical value, turning true into false and vice versa.
1 | |
MySQL follows the logical precedence where AND is evaluated before OR, unless parentheses explicitly state another order.
Bitwise Operations
Bitwise operations allow for the direct manipulation of the bits of integer numerical values.
Unlike logical operators (AND, OR), which work with boolean values (0 or 1), bitwise operators act on each individual bit in the binary numbers.
Bitwise AND Operation
The bitwise AND is a binary operation that uses two binary representations of the same length and performs the logical AND operation on each pair of corresponding bits.
Thus, if both bits in the compared position are 1, the bit in the resulting binary representation will be 1 (1 × 1 = 1); otherwise, the result will be 0 (1 × 0 = 0 and 0 × 0 = 0).
1 | |
MySQL allows us to perform this type of operation by abstracting the process of converting from decimal to binary. Below is the previous example done in MySQL:
1 | |
Bitwise OR Operation
The bitwise OR is a binary operation that uses two bit patterns of the same length and performs the inclusive OR logical operation on each pair of corresponding bits.
The result in each position will be 0 if both bits are 0; otherwise, the result will be 1.
1 | |
Below is how this operation occurs in MySQL:
1 | |
Selecting Date and Time
MySQL offers several functions to return information about the system’s date and time. These functions are useful for capturing the current time, the current date, or even timestamps. Below are some examples:
1 | |
In addition to the basic functions for getting date and time, MySQL offers several functions to manipulate and extract specific information from dates. These functions are useful for more detailed temporal analysis. Below are some examples:
1 | |
Mathematical Functions
MySQL has several built-in mathematical functions that can be used to perform calculations directly in the database. These functions are useful for operations like rounding, calculating powers, trigonometry, among others. Below are several examples:
1 | |
Working with User Variables
In MySQL, it is possible to use user variables to store temporary values during the execution of queries. These variables are useful for manipulating data, performing intermediate calculations, or sharing values between different SQL commands within the same session.
User variables are identified by the @ prefix and can be assigned and accessed directly. Below are some examples of use:
1 | |
Operations using tables
Now, we will start working with tables. Remember to replace the table and column names with the corresponding ones from your database.
Below, we explain the main uses of the SELECT command with practical examples:
Querying all records from a table
To retrieve all records from a table, use the * character:
1 | |
Querying specific columns
If you want to return only some columns from the table, specify the column names:
1 | |
For example, if we want to get the id and name from the customers table:
1 | |
Filtering records with WHERE
The WHERE clause is used to filter records based on specific conditions:
1 | |
For example, if we want to select the record in the customers table with id = 1:
1 | |
Using comparison operators
Comparison operators in MySQL are powerful tools that allow for more detailed and precise queries. They are used to compare values in a table, returning only the records that meet the specified conditions. Below are the main operators:
- Equality (
=): Returns records where the values of a field are exactly equal to a certain value.1
2-- Returns all records that meet the condition column1 equal to 10 SELECT * FROM table_name WHERE column1 = 10; - Not equal (
!=or<>): Returns records where the values are different from a specified value.1
2-- Returns all records that meet the condition column1 not equal to 10 SELECT * FROM table_name WHERE column1 != 10; - Greater than (
>): Returns records where the values are greater than a specified value.1
2-- Returns all records that meet the condition column1 greater than 10 SELECT * FROM table_name WHERE column1 > 10; - Less than (
<): Returns records where the values are less than a specified value.1
2-- Returns all records that meet the condition column1 less than 50 SELECT * FROM table_name WHERE column1 < 50; - Greater than or equal to (
>=): Returns records where the values are greater than or equal to a specified value.1
2-- Returns all records that meet the condition column1 greater than or equal to 20 SELECT * FROM table_name WHERE column1 >= 20; - Less than or equal to (
<=): Returns records where the values are less than or equal to a specified value.1
2-- Returns all records that meet the condition column1 less than or equal to 30 SELECT * FROM table_name WHERE column1 <= 30;
These operators make queries more flexible and allow for the extraction of specific information, which is very useful when working with large volumes of data.
Sorting queries with ORDER BY
The ORDER BY clause is used to sort the results of a query based on one or more columns.
It allows organizing data in ascending order (default, using ASC) or descending order (using DESC).
This is especially useful for viewing records in a structured and hierarchical way.
1 | |
Limiting the number of records
The LIMIT clause is used to restrict the number of records returned by a query.
It is especially useful when you want to view only a portion of the data, such as the first few results of a table or the most relevant records.
For example, to limit the number of results to 10 records, use LIMIT 10:
1 | |
You can also combine LIMIT with OFFSET to specify from which record the query should start:
1 | |
This functionality is widely used in results pagination and optimized queries.
Querying unique records with DISTINCT
The DISTINCT keyword is used to eliminate duplicate values in the results of a query.
It ensures that only unique records are returned, considering the values of the specified columns.
This is useful when you need to identify distinct values in a table, such as categories, names, or other attributes.
1 | |
Using LIKE to search for a pattern
The LIKE operator in MySQL is used to perform pattern searches in text columns.
It is especially useful when you need to find records that contain, start with, or end with certain characters.
To define the patterns, the wildcards % and _ are used:
For example, to search for all records containing strings that start with “A”:
1 | |
Now if we want to search for all records containing strings in column1 that end with “B”:
1 | |
All records containing strings in column1 that have “C”:
1 | |
The % wildcard character represents any sequence of characters (including none).
Another example, let’s say we need records that start with “A” and contain exactly 2 characters in sequence.
For this, you will need to use the _ wildcard, which represents exactly 1 character:
1 | |
In the statement above, values like ANA, ABC, and A23 would meet the condition. But values like ABCD and ANAS would not, as they have more than 2 characters after the “A”.
Counting Records with COUNT
The COUNT function returns the total number of records in the table or in a filtered subset:
1 | |
Performing Calculations with Aggregate Functions
In addition to COUNT, other aggregate functions can be used for calculations:
- Sum of the values of a column:
1
SELECT SUM(column1) FROM table_name; - Average of the values of a column:
1
SELECT AVG(column1) FROM table_name; - Maximum value of a column:
1
SELECT MAX(column1) FROM table_name; - Minimum value of a column:
1
SELECT MIN(column1) FROM table_name;
Grouping Results with GROUP BY
The GROUP BY clause is used to group records based on one or more columns:
1 | |
Filtering Groups with HAVING
The HAVING clause is used to filter the results after grouping:
1 | |
This material was created based on the sources listed below:
- MySQL 8.4 Reference Manual: https://dev.mysql.com/doc/refman/8.4/en/non-typed-operators.html
- Bitwise operation: https://en.wikipedia.org/wiki/Bitwise_operation