Select and Update Query

Written by

Soumya Shaw

SELECT QUERY:

Now, the time has come to know about the ‘Select’ Query formally. You might have already noticed the informal use of the Query before as well, picking out rows from the Table.

But, we can now, very particularly pick some special rows or columns from the table that has the special property that we are interested in. We will be starting off with basic use and slowing moving down to more varied use along with extensions in its syntax.

Syntax:

SELECT field1, field2…fieldN

FROM table_name1, table_name2…

[WHERE Clause]

[OFFSET M][LIMIT N]

So, let’s start off with the first type which is the most used till now in or course.

Syntax is:

SELECT * FROM table_name;

When we break down the components, we find that SELECT keyword selects the attributes from the table and ‘*’ marks the selection of all the elements in the table.

In simple words, it will select all the attributes and tuples from the table.

Let’s try it.

SELECT * FROM school;

Primarily, this query with ‘*’ in it is useful to see the full table and get the rough idea of all the values and parameters. Now, let’s go back to a different syntax use.

We can also opt to visualize only particular columns (attributes) as well from the desired table.

Taking the example of the same table and keeping in mind that now, we just need the names of students and their Phone numbers, not anything else, let’s see how we can visualize that.

SELECT Name, Mobile FROM school;

The fact that MySQL provides data security is very well implemented in this example that shows how other data that are not required/asked by the User is not revealed.

UPDATE QUERY:

UPDATE table_name

SET field1 = new-value1, field2 = new-value2

[WHERE Clause]

To understand this using example, let us try this at a different table. Let it be ‘railways’. Think of a Scenario where the Govt. decides to change the Express Train fare to Rs. 74 instead of Rs. 76.

UPDATE railways
SET CostperKM = 74
WHERE CostperKM = 76;

The Output including the Select command can be used to detect the change in the table now. You will notice that all the imperfect values are updated or converted.

Here, ‘WHERE’ clause is used to describe the condition for which the change must be carried out otherwise all the values will be changed (by default).

Don’t worry if the use of WHERE clause went above your head, we will be discussing it in detail in later stages.

This is how the Table will look after the Update query.

Select and Update Query