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
PHP MySQLi
SQL select statement doesn't return any rows,
How to display all the rows in JSP ,MySQL select condition statement IN dept_table
PHP MySQLi Query
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
Hibernate 5 MySQL5 Dialect
How to use 'if' statement in jsp page?
SQL Select, HQL Select Example, HQL Select Query
Use if statement with LOOP statement
JDBC Batch Example With SQL Select Statement
JDBC: Select Records using Prepared Statement
JDBC Select Statement Example
How to break statement in JSP
How to break statement in JSP
How to break statement in JSP
How to break statement in JSP
Select Records Using Prepared Statement
how I do select from select in hql
Using Select Statements in JDBC
use of return statement
Use DECLARE Statement in Procedure
Use DEFAULT Statement in Procedure
Use Compound Statement in JSP Code
Use DEFAULT Statement in Procedure
how to select second combobox value .
in string how will write the return statement..........?
Use DECLARE Statement in Procedure
how to operate on select box using ajax in struts2?
How to use switch statement in jsp code
how to operate on select box using ajax in struts2?
Use Break Statement in jsp code
for statement
Select data from Table in Database
how to pass the parameter in callable statement
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?
How to align the select tags in a single column.

Ads