How to use select Statement in MySQL?

Hi, How to use select Statement in MySQL? I want to learn the various ways to use the SQL select statement.

Thanks

View Answers

June 26, 2016 at 5:56 AM

Hi,

What is the use of MySQL select clause?

MySQL select clause is used to select the data from table based on various criteria. It allows to selectively get the data from database table.

Here are various ways to use the select statement in MySQL

Suppose we have following table:

CREATE TABLE email
(
first_name varchar(20),
last_name varchar(20),
email varchar(40),
email_count Int
)

Then we can get the data using various select statement.

Select all data:

select * from email;

Above query displays all the data from email table.

Select particular columns from table:

Here are various ways to use select statement:

select first_name from email;

select first_name,last_name from email;

select email from email;


select email, email_count from email;

select <column1, column2,...> from table;

Here is the sample examples with various select statement:

mysql> select * from email;
+------------+-----------+----------------------+-------------+
| first_name | last_name | email                | email_count |
+------------+-----------+----------------------+-------------+
| Deepak     | Kumar     | deepak@roseindia.net |         100 |
| Rajeev     | Singh     | rajeev@roseindia.net |         500 |
| Dinesh     | Singh     | dinesh@roseindia.net |         200 |
| John       | T         | john@roseindia.net   |         400 |
| Ishu       | roy       | NULL                 |         800 |
+------------+-----------+----------------------+-------------+
5 rows in set (0.00 sec)

mysql> select first_name from email;
+------------+
| first_name |
+------------+
| Deepak     |
| Rajeev     |
| Dinesh     |
| John       |
| Ishu       |
+------------+
5 rows in set (0.00 sec)

mysql> select first_name,last_name from email;
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Deepak     | Kumar     |
| Rajeev     | Singh     |
| Dinesh     | Singh     |
| John       | T         |
| Ishu       | roy       |
+------------+-----------+
5 rows in set (0.00 sec)

mysql> select first_name,last_name,email from email;
+------------+-----------+----------------------+
| first_name | last_name | email                |
+------------+-----------+----------------------+
| Deepak     | Kumar     | deepak@roseindia.net |
| Rajeev     | Singh     | rajeev@roseindia.net |
| Dinesh     | Singh     | dinesh@roseindia.net |
| John       | T         | john@roseindia.net   |
| Ishu       | roy       | NULL                 |
+------------+-----------+----------------------+
5 rows in set (0.00 sec)

mysql> select email from email;
+----------------------+
| email                |
+----------------------+
| deepak@roseindia.net |
| rajeev@roseindia.net |
| dinesh@roseindia.net |
| john@roseindia.net   |
| NULL                 |
+----------------------+
5 rows in set (0.00 sec)

mysql> select email, email_count from email;
+----------------------+-------------+
| email                | email_count |
+----------------------+-------------+
| deepak@roseindia.net |         100 |
| rajeev@roseindia.net |         500 |
| dinesh@roseindia.net |         200 |
| john@roseindia.net   |         400 |
| NULL                 |         800 |
+----------------------+-------------+
5 rows in set (0.00 sec)

mysql> desc email;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| first_name  | varchar(20) | YES  |     | NULL    |       |
| last_name   | varchar(20) | YES  |     | NULL    |       |
| email       | varchar(40) | YES  |     | NULL    |       |
| email_count | int(11)     | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql>

Thanks









Related Tutorials/Questions & Answers:
Advertisements