In this article, we will look into the basic, important and most commonly used statement in SQL. i.e , SELECT statement.
We use SELECT statement to select data from the database. The selected datas are stored in a table called result table.
Syntax for SELECT Statement
SELECT column1, column2, ....
FROM table_name;
Here, the column1 and column2 are the fields data we are selecting from the table.
We can select the entire table by,
SELECT * FROM table_name;
Let’s look into it briefly by an example
Customers
ID | Name | Contact Name | Address | City | Postal Code | Country |
---|---|---|---|---|---|---|
1 | Thomas Shelby | Tommy | Obere Str. 57 | Birmingham | 376 548 | UK |
2 | Arthur Shelby | Arthur | Avda. de la Constitución 2222 | London | 376 987 | UK |
3 | John Shelby | John | Mataderos 2312 | Boston | 624 394 | UK |
4 | Freddie Thorne | Freddie | 120 Hanover Sq | Paris | 356 943 | US |
5 | Polly Gray | Polly | Berguvsvägen 8 | California | 637 208 | US |
This is just an example of how data looks like in SQL as tables.
SELECT Columns Example
SELECT Name, City, Country FROM Customers;
The above code, selects and shows only Name, City and Country from Customers Table as follows.
Output
Name | City | Country |
---|---|---|
Thomas Shelby | Birmingham | UK |
Arthur Shelby | London | UK |
John Shelby | Boston | UK |
Freddie Thorne | Paris | US |
Polly Gray | California | US |
SELECT * Example
Select * is used to select all rows and columns from the table. We can also say that it shows the whole, complete table.
SELECT * FROM Customers;
Output
ID | Name | Contact Name | Address | City | Postal Code | Country |
---|---|---|---|---|---|---|
1 | Thomas Shelby | Tommy | Obere Str. 57 | Birmingham | 376 548 | UK |
2 | Arthur Shelby | Arthur | Avda. de la Constitución 2222 | London | 376 987 | UK |
3 | John Shelby | John | Mataderos 2312 | Boston | 624 394 | UK |
4 | Freddie Thorne | Freddie | 120 Hanover Sq | Paris | 356 943 | US |
5 | Polly Gray | Polly | Berguvsvägen 8 | California | 637 208 | US |
It shows the entire table.
SELECT DISTINCT
SELECT DISTINCT is used to remove the duplicate values in the table.
Syntax for SELECT DISTINCT
SELECT DISTINCT column1, column2, ....
FROM table_name;
This selects the table elements unique values.
Example
SELECT DISTINCT Name FROM Customers;
This returns the name with no duplicate values.
Name |
---|
Thomas Shelby |
Arthur Shelby |
John Shelby |
Freddie Thorne |
Polly Gray |
As our table contains no duplicate values, it shows the same, but if you have any duplicate values, it removes it and shows only the unique values.
COUNT
Count is a common function that we use to count the number of records in the fields.
For example,
SELECT COUNT(NAME) FROM Customers;
This returns 5 as its output. Because we have 5 records in the Name column. It is useful to identify the total number of records if you have a large set of data. There are many functions like COUNT in SQL. We will make a separate article for functions in SQL.
If you like this above article on MySQL statements, make sure to share it with your friends and your colleagues who will finds this useful. If you have any doubts, leave it in the comments section. We are happy to help you. Happy Learning!!!