{"id":1349,"date":"2022-06-13T01:34:51","date_gmt":"2022-06-13T01:34:51","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=1349"},"modified":"2023-08-07T08:24:28","modified_gmt":"2023-08-07T08:24:28","slug":"mysql-commands","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/mysql-commands","title":{"rendered":"MySQL commands"},"content":{"rendered":"<p><a href=\"https:\/\/www.checkmateq.com\/full-stack-development\"><strong>MySQL<\/strong><\/a> is one of the most used RDMS based database management systems based on SQL, it is a relational database stores data in tables in which data in tables may be related to each other these relations between tables helps to structure the data and preform operations like Create , Read, Update and Delete\u00a0 famously known as CRUD operations, In this blog let&#8217;s learn some MySQL commands.<\/p>\n<p><strong>How to use SELECT statement to get data from a MySQL table<\/strong><\/p>\n<p>MySQL select statement can be used to get data from one or more tables, you can get records of specific or all the fields of table using select statement, below is basic syntax of select statement<\/p>\n<p>SELECT\u00a0 field1,field2 FROM table_name;<\/p>\n<p>field1, field2 should be your tables column names, this gives specific columns\/fields of the table, let us use\u00a0 select statement on below table &#8220;Persons&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1431 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/m1.jpeg\" alt=\"\" width=\"613\" height=\"184\" \/><\/p>\n<p>To get all the fields of a table use below command.<\/p>\n<pre>SELECT * FROM Persons;<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1353 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2-300x98.jpeg\" alt=\"\" width=\"717\" height=\"234\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2-300x98.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2-1024x334.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2-768x250.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2-1536x500.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2-1200x391.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-2.jpeg 1796w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>MySQL select statement to get specific fields of a table.<\/strong><\/p>\n<p>To get specific fields from a table mention field names and table name, in the above snapshot you can see table &#8220;Persons&#8221; have five fields PersonID, LastName, FirstName, Address and\u00a0 City.<\/p>\n<pre>select PersonID,FirstName,City from Persons;<\/pre>\n<p>This command gives fields PersonID, FirstName, City\u00a0 from table named Persons.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1354 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3-300x86.jpeg\" alt=\"\" width=\"981\" height=\"281\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3-300x86.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3-1024x294.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3-768x220.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3-1536x440.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3-1200x344.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-3.jpeg 1671w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>MySQL select statement to get fields from more than one table.<\/p>\n<p><strong>How to use MySQL &#8220;ORDER BY&#8221; key word<\/strong><\/p>\n<p>ORDER BY\u00a0 is used to sort result in ascending or descending order, use ASC for ascending and DESC for descending, you can use this key word to sort integer or character fields.<\/p>\n<p>Consider below table <strong>Persons <\/strong>to understand following syntax of ORDER BY.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1355 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons-300x77.jpeg\" alt=\"\" width=\"662\" height=\"170\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons-300x77.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons-1024x264.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons-768x198.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons-1536x396.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons-1200x309.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-persons.jpeg 1630w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<pre>mysql&gt; select * from Persons ORDER BY PersonID DESC;\r\nmysql&gt; select * from Persons ORDER BY Address DESC;<\/pre>\n<p>above is the basic syntax for ORDER BY key word it gives all the field from table &#8220;Persons&#8221; and sorts PersonID in descending order and second command sorts Address field in DESC order.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1356 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6-300x174.jpeg\" alt=\"\" width=\"688\" height=\"399\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6-300x174.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6-1024x594.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6-768x445.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6-1536x891.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6-1200x696.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-6.jpeg 1647w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>How to use WHERE clause in MySQL commands<\/strong><\/p>\n<p>WHERE Clause is MySQL is used to filter the results, you can specify a condition to get the output according to your requirement, let us see an example using WHERE clause with SELECT statement on the below table named &#8220;Persons&#8221;.<\/p>\n<p>Table Persons.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1360 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-300x113.jpeg\" alt=\"\" width=\"693\" height=\"261\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-300x113.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1024x387.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-768x290.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1536x581.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1200x454.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2.jpeg 1830w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<pre>mysql&gt; select * from Persons where City='delhi';<\/pre>\n<p>above command gives output from table Persons whose value for column City is delhi.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1362 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-9-300x89.jpeg\" alt=\"\" width=\"681\" height=\"202\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-9-300x89.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-9-1024x303.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-9-768x227.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-9-1200x355.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-9.jpeg 1435w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<pre>mysql&gt; select PersonId,FirstName,City from Persons where PersonID&gt;2;<\/pre>\n<p>above command gives fields of PersonId, FirstName, City whose PersonId is greater than 2.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1361 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8-300x91.jpeg\" alt=\"\" width=\"712\" height=\"216\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8-300x91.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8-1024x312.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8-768x234.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8-1536x468.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8-1200x366.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-8.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to use AND ,OR, NOT operators in MySQL commands.<\/strong><\/p>\n<p>you can use AND, OR\u00a0 operators along with WHERE clause, see below command to understand AND operator usage. consider table Employees for this.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1364 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-11-300x133.jpeg\" alt=\"\" width=\"505\" height=\"224\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-11-300x133.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-11-1024x454.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-11-768x341.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-11-1200x532.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-11.jpeg 1226w\" sizes=\"(max-width: 505px) 85vw, 505px\" \/><\/p>\n<pre>mysql&gt; select * from Employees where performance &gt; 101 AND performance &lt;105 ;<\/pre>\n<p>when you use AND operator a record will be displayed only if it satisfies all the conditions separated by AND operator, above command gives all the fields from table Employees whose value for performance is greater than 101 and less than 105 .<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1365 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-12-300x80.jpeg\" alt=\"\" width=\"1083\" height=\"289\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-12-300x80.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-12-1024x272.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-12-768x204.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-12-1200x319.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-12.jpeg 1484w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>Now let&#8217;s see an example to understand how OR operator works, consider below table Persons for this.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1366 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1-300x113.jpeg\" alt=\"\" width=\"658\" height=\"248\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1-300x113.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1-1024x387.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1-768x290.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1-1536x581.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1-1200x454.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1.jpeg 1830w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<pre>mysql&gt; select PersonId,FirstName,City from Persons where City = 'jagtial' OR City = 'Delhi' OR City = 'Mexico';<\/pre>\n<p>above commands gives details of persons from table Persons whose value for city is jagtial or Mexico.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1367 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13-300x58.jpeg\" alt=\"\" width=\"698\" height=\"135\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13-300x58.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13-1024x200.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13-768x150.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13-1536x299.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13-1200x234.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-13.jpeg 1898w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to use Between operator in MySQL<\/strong><\/p>\n<p>BETWEEN is a logical operator which you can use to specify a range to get a specific data, see below example to know how to use it, consider table Employees for this example.<\/p>\n<p><img loading=\"lazy\" class=\"wp-image-1371 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-10-300x173.jpeg\" alt=\"\" width=\"487\" height=\"281\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-10-300x173.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-10-1024x589.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-10-768x442.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-10-1200x691.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-10.jpeg 1223w\" sizes=\"(max-width: 487px) 85vw, 487px\" \/><\/p>\n<pre>mysql&gt; select * from Employees where salary BETWEEN 40000 AND 50000;<\/pre>\n<p>This command gives all the rows from table Employees whose value for salary is between 40000 and 50000.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1372 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-14-300x58.jpeg\" alt=\"\" width=\"724\" height=\"140\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-14-300x58.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-14-1024x199.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-14-768x149.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-14-1200x233.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-14.jpeg 1536w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>How to use LIMIT clause in MySQL<\/strong><\/p>\n<p>LIMIT clause is used to get specific number of records it used when working with large tables with thousands of\u00a0 rows because returning large data may impact performance, Let&#8217;s see how to use LIMIT clause .consider below table Persons for this<\/p>\n<p>Table <strong>Persons<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1374 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-15-300x113.jpeg\" alt=\"\" width=\"717\" height=\"270\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-15-300x113.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-15-1024x384.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-15-768x288.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-15.jpeg 1188w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>MySQL command to use LIMIT<\/p>\n<pre>mysql&gt; select * from Persons LIMIT 3;<\/pre>\n<p>as you can see table Persons have 6 rows, above command gives only 3 rows as LIMIT given is 3.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1375 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-17-300x90.jpeg\" alt=\"\" width=\"694\" height=\"208\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-17-300x90.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-17-1024x307.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-17-768x230.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-17.jpeg 1193w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>How to add data to a table using INSERT statement in MySQL<\/strong><\/p>\n<p>INSERT statement is used to add data in a MySQL table , you can insert one row or multiple rows with a single query, and also you can add data\u00a0 to all the columns of a row or to specified columns of a rows.<\/p>\n<p>syntax of insert statement<\/p>\n<p>INSERT INTO table_name(field_name1,field_name2&#8230;) VALUES (value1,value2,value3..);<\/p>\n<p>see the following example MySQL to understand how to use insert statement, let&#8217;s use table Persons for this.<\/p>\n<p>Table <strong>Persons<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1360 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-300x113.jpeg\" alt=\"\" width=\"611\" height=\"230\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-300x113.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1024x387.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-768x290.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1536x581.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2-1200x454.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-per2.jpeg 1830w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>check above given\u00a0 Persons table it has five fields, when you insert values to all the fields of a table you can do it directly without mentioning field names but when inserting values to specific fields you have to mention field names of the table, Let&#8217;s a example for each inserting values to all the fields of a table and specific fields of a table.<\/p>\n<p>Inserting values to all the fields of a table.<\/p>\n<pre>mysql&gt; INSERT INTO Persons VALUES (7,'Tej','Kiran','koramangala','banglore'),(8,'Patrik','Jhon','Madhapur','Hyderabad');<\/pre>\n<p>above command will add two new rows to table Persons.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1378 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18-300x93.jpeg\" alt=\"\" width=\"691\" height=\"214\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18-300x93.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18-1024x316.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18-768x237.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18-1536x474.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18-1200x370.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-18.jpeg 1715w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Inserting values to specific fields of a table.<\/p>\n<pre>mysql&gt; INSERT INTO Persons(PersonID,FirstName,City) Values (9,'williom','Gurgaon');<\/pre>\n<p>as mentioned above when you are adding values to specific columns of a table using INSERT statement you have to give field names, In above command I am inserting values to specific fields of the table\u00a0 Persons.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1379 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-300x124.jpeg\" alt=\"\" width=\"644\" height=\"266\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-300x124.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1024x424.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-768x318.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1200x496.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19.jpeg 1267w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>In the above snapshot observe 9th row values for last name and address are NULL (default value) as I inserted values into PersonID, FirstName and City only.<\/p>\n<p><strong>How to update a value in MySQL table using UPDATE statement<\/strong><\/p>\n<p>UPDATE statement in MySQL is used to modify existing data in tables, let&#8217;s make some updates to table <strong>Persons <\/strong> using <strong>UPDATE<\/strong> statement.<\/p>\n<p>Table <strong>Persons<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1384 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1-300x124.jpeg\" alt=\"\" width=\"609\" height=\"252\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1-300x124.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1-1024x424.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1-768x318.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1-1200x496.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-19-1.jpeg 1267w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<pre>mysql&gt; UPDATE Persons\r\n-&gt; SET Address='Ejipura', City='Banglore'\r\n-&gt; WHERE PersonID =1;<\/pre>\n<p>above command changes the values of Address and City of person with PersonID equal to 1, here Persons is table name and Address, City, PersonID are fields of the table.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1387 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-20-300x135.jpeg\" alt=\"\" width=\"631\" height=\"284\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-20-300x135.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-20-1024x460.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-20-768x345.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-20-1200x539.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-20.jpeg 1338w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>you can aslo use UPDATE without WHERE clause , but use it only when you want update all the rows of that column.<\/p>\n<pre>mysql&gt; UPDATE Persons\r\n-&gt; SET City='Bangalore';<\/pre>\n<p>above command changes the value of City to Bangalore.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1389 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-21-300x121.jpeg\" alt=\"\" width=\"696\" height=\"281\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-21-300x121.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-21-1024x413.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-21-768x310.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-21-1200x484.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-21.jpeg 1375w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>How to use JOIN clause in MySQL<\/strong><\/p>\n<p>MySQL JOINS are used to get data from multiple tables and combine the out put based on the related column the tables have, we have various types of joins in mysql INNER JOIN, RIGHT JOIN,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 LEFT JOIN, CROSS JOIN, below are two example tables Orders, CustomerDetails let&#8217;s use these tables to perform JOIN clause.<\/p>\n<p>Table <strong>CustomerDetails<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1400 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust1-300x108.jpeg\" alt=\"\" width=\"636\" height=\"229\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust1-300x108.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust1-1024x368.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust1-768x276.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust1-1200x431.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust1.jpeg 1343w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Table <strong>Orders<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1402 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-300x98.jpeg\" alt=\"\" width=\"631\" height=\"206\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-300x98.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-1024x333.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-768x250.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-1200x390.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1.jpeg 1255w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>INNER JOIN<\/strong><\/p>\n<p>checkout above tables, I want list of\u00a0 customers who has ordered along with their orderID , inner join will do this for us as it gives all the records that have matching values in both the tables that means it searches for rows with common customerID from both tables and joins them.<\/p>\n<pre>mysql&gt; SELECT customerName,orderID,orderDate\r\n-&gt; FROM CustomerDetails\r\n-&gt; INNER JOIN Orders\r\n-&gt; ON CustomerDetails.customerID=Orders.customerID;<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1405 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-22-300x107.jpeg\" alt=\"\" width=\"630\" height=\"225\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-22-300x107.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-22-1024x366.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-22-768x274.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-22-1200x429.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-22.jpeg 1469w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>In above snapshot you can see I got customer names from CustomerDetails table for all the orders.<\/p>\n<p><strong>LEFT JOIN<\/strong><\/p>\n<p>Left join will return all the data from left table, and matching data from right table, let&#8217;s understand this with an example , I will use below tables to perform a Left join.<\/p>\n<p>Table<strong> CustomerDetails<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1406 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-300x82.jpeg\" alt=\"\" width=\"640\" height=\"175\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-300x82.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1024x281.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-768x211.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1200x329.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2.jpeg 1247w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Table <strong>Orders<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1401 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-300x98.jpeg\" alt=\"\" width=\"646\" height=\"211\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-300x98.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1024x333.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-768x250.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1200x390.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1.jpeg 1255w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>Problem statement:\u00a0<\/strong>get order details of each customer , for this I should get all the customers from customerDetails table along with their order details from Orders table.<\/p>\n<pre>mysql&gt; select CustomerDetails.customerID,customerName,orderID,orderDate\r\n-&gt; FROM CustomerDetails\r\n-&gt; LEFT JOIN Orders\r\n-&gt; ON CustomerDetails.customerID=Orders.customerID\r\n-&gt; ORDER BY customerName;<\/pre>\n<p>use tableName.columnName when you are accessing a common column from both tables, above command gives all the rows of customerID,customerName from CustomerDetais table and order details of each customer from Orders table.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1408 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-23-300x105.jpeg\" alt=\"\" width=\"795\" height=\"278\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-23-300x105.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-23-1024x360.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-23-768x270.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-23-1536x540.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-23.jpeg 1792w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>RIGHT JOIN<\/strong><\/p>\n<p>MySQL RIGHT JOIN gives all the records from right table and matching records from the other table, see below example to understand this consider below tables for this.<\/p>\n<p>Table <strong>CustomerDetails<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1406 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-300x82.jpeg\" alt=\"\" width=\"633\" height=\"173\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-300x82.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1024x281.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-768x211.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1200x329.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2.jpeg 1247w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Table<strong> Orders<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1402 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-300x98.jpeg\" alt=\"\" width=\"652\" height=\"213\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-300x98.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-1024x333.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-768x250.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1-1200x390.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/order1-1.jpeg 1255w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>Problem statement:\u00a0<\/strong>Get all the order ID&#8217;s with customer name for each order.<\/p>\n<pre>mysql&gt; SELECT orderID,orderDate,customerName\r\n-&gt; FROM CustomerDetails\r\n-&gt; RIGHT JOIN Orders\r\n-&gt; ON CustomerDetails.customerID=Orders.customerID;<\/pre>\n<p>above command gives all the rows of orderID and orderDate from Orders table with customer name for each order, as\u00a0 as right join gives all the rows from right table and matching rows from left table.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1410 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-25-300x124.jpeg\" alt=\"\" width=\"668\" height=\"276\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-25-300x124.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-25-1024x423.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-25-768x317.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-25-1200x496.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-25.jpeg 1525w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to use GROUP BY statement in MySQL<\/strong><\/p>\n<p>GROUP BY clause in MySQL is generally use with SELECT statement, it groups similar data from all rows to a summary column .<\/p>\n<p>Table <strong>CustomerDetails<\/strong><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1413 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1-300x82.jpeg\" alt=\"\" width=\"578\" height=\"158\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1-300x82.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1-1024x281.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1-768x211.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1-1200x329.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-1.jpeg 1247w\" sizes=\"(max-width: 578px) 85vw, 578px\" \/><\/p>\n<p><strong>Problem statement: <\/strong>Get all the country names from where customers are located in.<\/p>\n<pre>mysql&gt; SELECT country FROM CustomerDetails GROUP BY country;<\/pre>\n<p>above command\u00a0 will give each value of country column only once .<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1415 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27-300x72.jpeg\" alt=\"\" width=\"654\" height=\"157\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27-300x72.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27-1024x247.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27-768x185.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27-1536x370.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27-1200x289.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-27.jpeg 1656w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>some aggregate functions like COUNT can also be used along with GROUP BY clause, see below example<\/p>\n<p><strong>Problem Statement<\/strong>: Get count of customers from each country.<\/p>\n<pre>mysql&gt; SELECT country,COUNT(customerID) FROM CustomerDetails GROUP BY country;<\/pre>\n<p>above command gives count of customers from each country .<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-1418\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-28-300x85.jpeg\" alt=\"MySQL Commands\" width=\"697\" height=\"197\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-28-300x85.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-28-1536x437.jpeg 1536w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>How to use INSERT INTO SELECT statement to add data to a table from another table.<\/strong><\/p>\n<p>sometimes you may need subset of a table or want to add data to a table from another table , INSERT INTO SELECT statement will allow us to do this, its simply SELECT columns from a table and INSERT INTO another.<\/p>\n<p>Table <strong>CustomerDetails<\/strong><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-1423\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-2-300x82.jpeg\" alt=\"MySQL Commands\" width=\"538\" height=\"147\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-2-300x82.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-2-1024x281.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-2-768x211.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-2-1200x329.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/cust2-2.jpeg 1247w\" sizes=\"(max-width: 538px) 85vw, 538px\" \/><\/p>\n<p>Table mexicoCustomers(empty table)<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1424 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-29-300x29.jpeg\" alt=\"\" width=\"600\" height=\"58\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-29-300x29.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-29-1024x100.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-29-768x75.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-29-1200x117.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-29.jpeg 1329w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>I have table named &#8220;CustomerDetails&#8221;(given above) where I have customers from all countries and have another table named &#8220;mexicoCustomers&#8221; where I want have details of\u00a0 customers only from Mexico, in above given table we have only two rows of data you can manually insert this data but when you have thousands of rows it will be tough to do that, below is the command to do this.<\/p>\n<pre>mysql&gt; INSERT INTO mexicoCustomers\r\n-&gt; SELECT customerID,customerName,country\r\n-&gt; FROM CustomerDetails\r\n-&gt; WHERE country='mexico';<\/pre>\n<p>here mexicoCustomers, CustomerDetails are table names and customerID, customerName, country are fields of CustomerDetails , above command will select rows where country value is mexico from CustomerDetails and inserts it into mexicoCustomers.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-1425\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30-300x130.jpeg\" alt=\"MySQL\" width=\"669\" height=\"290\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30-300x130.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30-1024x442.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30-768x332.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30-1536x663.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30-1200x518.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mysql-30.jpeg 1602w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: center;\">Please <\/span><a style=\"text-align: center;\" href=\"https:\/\/www.checkmateq.com\/contact-us\">contact <\/a><span style=\"text-align: center;\">with our technical consultants if you want to hire GCP cloud engineer to manage cloud infrastructure.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL is one of the most used RDMS based database management systems based on SQL, it is a relational database stores data in tables in which data in tables may be related to each other these relations between tables helps to structure the data and preform operations like Create , Read, Update and Delete\u00a0 famously &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/mysql-commands\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MySQL commands&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":1433,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[2,7,22,23,16,24],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/1349"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=1349"}],"version-history":[{"count":42,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/1349\/revisions"}],"predecessor-version":[{"id":4313,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/1349\/revisions\/4313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/1433"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}