MySQL MariaDB

How to Use SELECT Statement in MySQL?

The SELECT statement is a fundamental component of MySQL that can be utilized for manipulating and retrieving data from a database in a more precise and efficient manner. The SELECT statement can be used to match values, apply functions, and print table data with or without conditions. By using the SELECT statement the user can easily analyze the data in many different ways by applying different conditions and functions.

This guide will explore how to use the SELECT statement in MySQL.

How to Utilize a SELECT Statement in MySQL?

First, you need to log in to the MySQL database, after the login, the SELECT statement can be utilized to retrieve or print any data.

Let’s move to the examples to understand various use cases of the SELECT statement.

Example 1: Basic Usage of SELECT Statement

An example of getting a string is given below:

SELECT "HELLO" AS greeting;

 

In the above example, “HELLO” is printed as a “greeting” by using the “SELECT” statement.

Output

The word “HELLO” is printed using the SELECT statement.

Example 2: Match From a List

The “SELECT” statement can be utilized to match a specific number or string in the list by using the “IN” clause with it. An example of matching a particular number in a given list is provided below:

SELECT 25 IN (0, 10, 15, 12, 25);

 

In the above example, the number “25” is matched.

Output

The output “1” showed that the specified number has been found.

Example 3: Get Function Values

The “SELECT” statement can also be utilized to print the value of any particular function. The example of printing the “CURRENT_DATE()” function value is given below:

SELECT CURRENT_DATE();

 

Output

The output displayed the current date.

Example 4: Fetch Tables Data

The table data can be fetched by utilizing the “SELECT” statement. An example of fetching the data of the “categories” table is given below:

SELECT * FROM categories;

 

Output

The output showed that the table’s data has been fetched successfully.

Example 5: Fetched Tables Data With Condition

The condition can be applied using the “WHERE” clause while fetching any particular table’s data with a “SELECT” statement. The example of printing the “orders” table data with the specific condition is given below:

SELECT * FROM orders
WHERE id >= 19;

 

In the above example, the condition states that the “id” must be equal to or greater than “19”.

Output

The output showed the table’s data according to the specified condition.

Example 6: Get Tables Data With Built-in Function

The built-in functions can be used with the “SELECT” statement to perform various functionalities on the table’s data. An example of applying the “COUNT(*)” function on the “customers” table is shown below:

SELECT COUNT(*)
FROM customers;

 

Output

The output showed the count/number of records or rows available in the given table.

Example 7: Using SELECT Statement in a Subquery

An example of utilizing the “SELECT” statement in a subquery is demonstrated in the below code snippet:

SELECT * FROM orders
WHERE user_id IN (
SELECT id FROM users
WHERE CHAR_LENGTH(password) > 11
);

 

Output

The output showed the data according to the query.

Conclusion

The “SELECT” statement is a useful command in MySQL that can retrieve and print data from a database. It can be used to print strings, match values from a list, print function values, and print table data with or without conditions. The “WHERE” clause can be used to add conditions, and functions can be applied to table data to perform calculations. This guide has explained how to use the SELECT statement in MySQL.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.