SELECT Statement in MySQL
The SELECT statement is used for querying data. It allows you to select data from one or more tables.
Syntax:
SELECT column1, column 2,....
FROM table_name; - In the above syntax for select statements, there can be one or multiple column names after the ‘SELECT’ keyword from which you want to select data. All the column names need to be separated by a comma (
,). - ‘table_name’ denotes the name of the table from which you want to select the data.
- The semicolon (
;)is optional if there is only one statement. It is needed to separate multiple statements to execute them individually. - It's also not mandatory to write the SQL keywords in uppercase. It works fine in both upper and lower case but it is written in uppercase by convention.
- MySQL evaluates the FROM clause before the SELECT clause at the time of execution.
Example :
| student | ||
|---|---|---|
| roll_no | name | stream |
| 1 | Ashish | CSE |
| 2 | Prashant | CSE |
| 3 | Ujjwal | CSE |
| 4 | Ankit | IT |
Selecting data from a single column
SELECT name FROM student;Output :
| name |
|---|
| Ashish |
| Prashant |
| Ujjwal |
| Ankit |
Selecting data from multiple columns
SELECT name, stream FROM student;Output :
| name | stream |
|---|---|
| Ashish | CSE |
| Prashant | CSE |
| Ujjwal | CSE |
| Ankit | IT |
Selecting data from all the columns
SELECT * FROM student;
Output:
| roll_no | name | stream |
|---|---|---|
| 1 | Ashish | CSE |
| 2 | Prashant | CSE |
| 3 | Ujjwal | CSE |
| 4 | Ankit | IT |
SELECT statement without FROM clause
The SELECT statement can also be used without the FROM clause.
For Example: SELECT 5 + 5;
Output:
| 5+5 |
|---|
| 10 |
MySQL has many built-in functions like string, date, and math functions. They can be used with the SELECT statement.
For Example:
1. SELECT date();
Output:
| date() |
|---|
| 2021 - 11 - 16 |
The date() is a built-in function of MySQL. It can be used with the SELECT statement. It returns the current date of the MySQL server as an output.
2. SELECT CONCAT('Be' , ' ' , 'Awesome');
Output:
| CONCAT('Be' , ' ' , 'Awesome') |
|---|
| Be Awesome |
DUAL table
The DUAL table is not an actual table. It is a dummy table. It can be used with the SELECT statement without referencing any actual table.
SELECT DISTINCT statement in MySQL
The SELECT DISTINCT statement is used in MySQL to remove duplicate rows. It outputs every row a single time even if it appears multiple times in the actual table.
Syntax:
SELECT DISTINCT column1, column2,...
FROM table_name; SELECT DISTINCT with Single Column
For Example:
| student | |
|---|---|
| name | stream |
| Vivek | CSE |
| Ankit | CSE |
| Ankit | IT |
| Vivek | CSE |
SELECT DISTINCT name FROM student;
Output:
| name |
|---|
| Vivek |
| Ankit |
Note: If there are multiple NULL in a column the SELECT DISTINCT clause will return only one NULL in the output.
SELECT DISTINCT with multiple columns
In the case of multiple columns, it removes the duplicates by using the combination of the columns.
SELECT DISTINCT name, stream from student;
Output:
| name | stream |
|---|---|
| Vivek | CSE |
| Ankit | CSE |
| Ankit | IT |