Page 29 - Modular_V2.0_SQL_Flipbook
P. 29

Preceding command will return the following result:
                                                    +------------+-----------+
                                                    | First_name | Last_name |
                                                    +------------+-----------+
                                                    | Amit       | Sharma    |
                                                    +------------+-----------+
                                                    | Divya      | Kaushik   |
                                                    +------------+-----------+
                                                    | Aadarsh    | Kumar     |
                                                    +------------+-----------+
                                                    | Dishu      | Gupta     |
                                                    +------------+-----------+
                 WHERE Clause


                 The ‘SELECT’ command has many various optional clauses which you can use with it to enhance the
                 result. The WHERE clause is one of the most useful clauses.
                 WHERE clause is used to specify the criteria according to which records are retrieved from the table.
                 The syntax of the WHERE clause is as follows:

                     SELECT * FROM <table_name> WHERE <column_name> = <value>;
                 For example, suppose, you want to retrieve the record of the students whose last name is Kumar,
                 then the SELECT command will be as follows:

                     SELECT * FROM Students WHERE Last_name = "Kumar";
                 The preceding command will display the following output:
                 +------------+------------+-----------+-------+--------------------------------+
                 | Student_id | First_name | Last_name | Marks | Address                        |
                 +------------+------------+-----------+-------+--------------------------------+
                 | 10003      | Aadarsh    | Kumar     | 475   | 22/12, Sarita Vihar, New Delhi |
                 +------------+------------+-----------+-------+--------------------------------+
                 Creating new columns using data from the table. If you want to see the percentage of students,
                 considering the max marks can be 500, you would use the following command:
                 Select First_name, (Marks/500 * 100) as Percentage from Students;

                 The output shall be:
                                                   +------------+------------+
                                                   | First_name | Percentage |
                                                   +------------+------------+
                                                   | Amit       | 90         |
                                                   +------------+------------+
                                                   | Divya      | 96         |
                                                   +------------+------------+
                                                   | Aadarsh    | 95         |
                                                   +------------+------------+
                                                   | Dishu      | NULL       |
                                                   +------------+------------+
                 Any calculation on Null shall produce a NULL result only.

                     UPDATING RECORDS IN A TABLE


                 Sometimes, you may need to change the inserted records into a table. In that case, the UPDATE
                 command will work. The syntax of the UPDATE command is:

                     UPDATE table_name SET field_change=value WHERE field_criteria=value;

                                                                                                                     27
                                                                                          Handling Records in MySQL
   24   25   26   27   28   29   30   31   32   33   34