Skip to main content

Select Query in MySQL

Introduction

In this tutorial, we will be exploring the SELECT statement in MySQL, which is one of the most frequently used SQL commands. The SELECT statement is used to select data from a database. The data returned is stored in a result table, also called the result-set.

Basic Syntax of SELECT statement

The basic syntax of the MySQL SELECT statement is as follows:

SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

Examples

Example 1: Selecting Data from One Column

Let's say we have a table named "Students" with the following data:

IDNameAgeGrade
1John1510
2Jane1611
3Alice1510

If you want to select only the "Name" column from the "Students" table, the SELECT statement would be as follows:

SELECT Name FROM Students;

The result-set will look like this:

Name
John
Jane
Alice

Example 2: Selecting Data from Multiple Columns

If you want to select data from more than one column, simply separate the column names with a comma. For example, to select "Name" and "Age", use the following SELECT statement:

SELECT Name, Age FROM Students;

The result-set will look like this:

NameAge
John15
Jane16
Alice15

Example 3: Selecting Data from All Columns

If you want to select data from all the columns of the "Students" table, use the * symbol:

SELECT * FROM Students;

The result-set will look like this:

IDNameAgeGrade
1John1510
2Jane1611
3Alice1510

Conclusion

The SELECT statement is a powerful tool in SQL. It allows us to retrieve exactly the data we need from our database. In this tutorial, we have seen how to use the SELECT statement to select data from one column, multiple columns, or all columns of a table.

As you continue your journey in learning SQL, you'll discover that the SELECT statement can do much more, including filtering data, ordering data, and performing calculations.

Happy querying!