In this article, we cover the Select statement in MySQL. As the term/keyword itself suggests, it is used to select the data from a database. To understand it better, we take a reference database with the table: test.
Customer_ID | Customer_Name | City |
---|---|---|
1 | ABC | London |
2 | XYZ | Amsterdam |
3 | QWERTY | New York |
4 | DHG | Boston |
5 | YTU | San Francisco |
Most of the time, you would see both SELECT and FROM in the commands.
Select Statement in MySQL
We begin with the basic Select command:
mysql> SELECT * FROM test;
The asterisk (*) here returns with all the columns from the mentioned table (i.e. test) through FROM. It is important to put a semicolon (;) at the end of the SQL statement. It will only execute those statements that have a semicolon (;) at the end. If you forget to put it there then, you can use it in the new line. For instance,
mysql> SELECT * FROM test -> ;
Furthermore, the database we have chosen has three columns. And, if want it to select just two columns then, we can modify the statement:
mysql> SELECT Customer_ID, Customer_Name FROM test;
If we have got duplicate values in the columns then by default it selects all the entries. To get rid of repetitions, use DISTINCT:
mysql> SELECT DISTINCT Customer_ID, Customer_Name FROM test;
If you wish to select only that data where Customer_ID is greater than 2 then, use WHERE:
SELECT * FROM test WHERE Customer_ID>2;
In conclusion, we have covered here the Select Statement in MySQL.