MySQL SELECT Statement – Basic fundamentals of SQL

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


IDNameContact NameAddressCityPostal CodeCountry
1 Thomas ShelbyTommyObere Str. 57Birmingham376 548UK
2Arthur ShelbyArthurAvda. de la Constitución 2222London376 987 UK
3John ShelbyJohnMataderos 2312Boston624 394 UK
4Freddie ThorneFreddie120 Hanover SqParis356 943US
5Polly GrayPollyBerguvsvägen 8California637 208US

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

NameCityCountry
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

IDNameContact NameAddressCityPostal CodeCountry
1 Thomas ShelbyTommyObere Str. 57Birmingham376 548UK
2Arthur ShelbyArthurAvda. de la Constitución 2222London376 987 UK
3John ShelbyJohnMataderos 2312Boston624 394 UK
4Freddie ThorneFreddie120 Hanover SqParis356 943US
5Polly GrayPollyBerguvsvägen 8California637 208US

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!!!

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?