How to use select Statement in MySQL?

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     | [email protected] |         100 |
| Rajeev     | Singh     | [email protected] |         500 |
| Dinesh     | Singh     | [email protected] |         200 |
| John       | T         | [email protected]   |         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     | [email protected] |
| Rajeev     | Singh     | [email protected] |
| Dinesh     | Singh     | [email protected] |
| John       | T         | [email protected]   |
| Ishu       | roy       | NULL                 |
+------------+-----------+----------------------+
5 rows in set (0.00 sec)

mysql> select email from email;
+----------------------+
| email                |
+----------------------+
| [email protected] |
| [email protected] |
| [email protected] |
| [email protected]   |
| NULL                 |
+----------------------+
5 rows in set (0.00 sec)

mysql> select email, email_count from email;
+----------------------+-------------+
| email                | email_count |
+----------------------+-------------+
| [email protected] |         100 |
| [email protected] |         500 |
| [email protected] |         200 |
| [email protected]   |         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:
How to use select Statement in MySQL?
PHP MySQLI Prep Statement
Advertisements
MySQL Select Statement
The SELECT statement for SQL
mysql select statement - SQL
Help with SELECT/JOIN statement
ModuleNotFoundError: No module named 'mysqlx-connector'
SQL SELECT DISTINCT Statement
where clause in select statement
PHP MySQLi Affected Rows
SQL SELECT DISTINCT Statement
Select Statement in SQL, SQL Tutorial
How to display all the rows in JSP ,MySQL select condition statement IN dept_table
PHP MySQLi
SQL select statement doesn't return any rows,
How to use 'if' statement in jsp page?
PHP MySQLi Query
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
SQL Select, HQL Select Example, HQL Select Query
How to break statement in JSP
How to break statement in JSP
How to break statement in JSP
How to break statement in JSP
Use if statement with LOOP statement
JDBC Batch Example With SQL Select Statement
JDBC: Select Records using Prepared Statement
how I do select from select in hql
JDBC Select Statement Example
Select Records Using Prepared Statement
Using Select Statements in JDBC
use of return statement
how to select second combobox value .
How to use switch statement in jsp code
in string how will write the return statement..........?
how to operate on select box using ajax in struts2?
Use DECLARE Statement in Procedure
Use DEFAULT Statement in Procedure
how to operate on select box using ajax in struts2?
Use Compound Statement in JSP Code
Use DEFAULT Statement in Procedure
how to pass the parameter in callable statement
Use DECLARE Statement in Procedure
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?
Use Break Statement in jsp code
How to align the select tags in a single column.
for statement
Select data from Table in Database

Ads