Menu Close

MySQL SELECT Statement

In this article, you will learn all about the MySQL SELECT statement to select records from the MySQL database table.MySQL select statement is one of the most popular queries and most commonly used query used by most the programmer and database administrators.

MySQL SELECT Statement Introduction

MySQL provides a SELECT statement to select or fetch records from the database table.

Syntax

The basic syntax of MySQL SELECT statement is:-

SELECT select_list FROM table;

Let’s break down the above syntax.

  • First, you have to specify one or more columns which you want to select from table just after the SELECT statement.If you want to select more than one columns you need to seperate them by commas.
  • Second, specify the name of the table from which you want to select data after the FROM keyword.

Note: You can use multiple statements in a single line separated by a colon ( ; ). MySQL will execute one statement at a time.

MySQL SELECT Statement Example

To use the MySQL SELECT statement, we will create a simple table just for testing purposes.

Example: Create a table in MySQL


CREATE TABLE students (
  st_id int(11) NOT NULL AUTO_INCREMENT,
  first_name varchar(100) NOT NULL,
  last_name varchar(100) DEFAULT NULL,
  course varchar(100) NOT NULL,
  created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (st_id)
);

Click here to learn the MySQL CREATE TABLE statement.

Insert some records into students table using the MySQL INSERT INTO command.


insert into students (st_id, first_name, last_name,course) values (1,'Vishvajit', 'Rao', 'MCA'), (2, 'John', 'Doe', 'Mtech'), (3, 'Shivam', 'Kumar','B.A.');

Now, it’s time to use the MySQL SELECT statement to fetch data from the students table.

Select one column using MySQL SELECT statement

To select only a column from the table, you need to just specify the column name after the SELECT keyword.

Example: Select a column from MySQL

Here we are selecting the first name of the student from the students table.

SELECT first_name FROM students;

Output


+------------+
| first_name |
+------------+
| Vishvajit  |
| John       |
| Shivam     |
+------------+
3 rows in set (0.00 sec)

Select more than one columns using MySQL SELECT statement

To select multiple columns from the table, you need to just specify all the column names separated by commas after the SELECT keyword.

Example: Select multiple columns from MySQL

Here we are going to select first_name, last_name, and course from the students table.

select first_name, last_name, course from students;

Output:

The output look likes this.


+------------+-----------+--------+
| first_name | last_name | course |
+------------+-----------+--------+
| Vishvajit  | Rao       | MCA    |
| John       | Doe       | Mtech  |
| Shivam     | Kumar     | B.A.   |
+------------+-----------+--------+
3 rows in set (0.00 sec)

Select all the columns using MySQL SELECT statement

To select all the columns from the table, you need to just specify all the column names separated by commas or asterisk ( * ) instead of the columns name just after the SELECT keyword.

Example: Select all records from MySQL

In this example, we are selecting all the columns from the students table.

select * from students;

Output

+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     1 | Vishvajit  | Rao       | MCA    | 2021-11-13 14:26:20 |
|     2 | John       | Doe       | Mtech  | 2021-11-18 17:26:39 |
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-10 19:40:30 |
+-------+------------+-----------+--------+---------------------+
3 rows in set (0.00 sec)

Conclusion

In this article, you will learn all about the MySQL SELECT statement to select all the data from the students table. this is the very basic command to select all the records or specific records from the table. In a later tutorial, we have seen more complex queries to select data from the table.

I hope this article will help you. if you like this article, share and keep visiting for further MySQL tutorials.

Thanks for your valuable time …

Reference:- Click Here

Drop MySQL Database
MySQL AND Operator

Related Posts