SQL Query Insert, update and delete operations, Joins

Insert

The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

Syntax

There are two basic syntaxes of the INSERT INTO statement which are shown below.

INSERT INTO TABLE_NAME (column1, column2, column3,…columnN) 

VALUES (value1, value2, value3,…valueN);

Here, column1, column2, column3,…columnN are the names of the columns in the table into which you want to insert the data.

You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the table. But make sure the order of the values is in the same order as the columns in the table.

The SQL INSERT INTO syntax will be as follows:

INSERT INTO TABLE_NAME VALUES (value1,value2,value3,…valueN);

Update

The SQL UPDATE Query is used to modify the existing records in a table. You can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.

Syntax

The basic syntax of the UPDATE query with a WHERE clause is as follows:

UPDATE table_name

SET column1 = value1, column2 = value2…., columnN = valueN

WHERE [condition];

You can combine N number of conditions using the AND or the OR operators.

Delete operations

The SQL DELETE Query is used to delete the existing records from a table.

You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all the records would be deleted.

Syntax

The basic syntax of the DELETE query with the WHERE clause is as follows:

DELETE FROM table_name

WHERE [condition];

You can combine N number of conditions using AND or OR operators.

Joins

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Table 1 − CUSTOMERS Table

+—-+———-+—–+———–+———-+

| ID | NAME     | AGE | ADDRESS   | SALARY   |

+—-+———-+—–+———–+———-+

|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |

|  2 | Khilan   |  25 | Delhi     |  1500.00 |

|  3 | kaushik  |  23 | Kota      |  2000.00 |

|  4 | Chaitali |  25 | Mumbai    |  6500.00 |

|  5 | Hardik   |  27 | Bhopal    |  8500.00 |

|  6 | Komal    |  22 | MP        |  4500.00 |

|  7 | Muffy    |  24 | Indore    | 10000.00 |

+—-+———-+—–+———–+———-+

Table 2 − ORDERS Table

+—–+———————+————-+——–+

|OID  | DATE                | CUSTOMER_ID | AMOUNT |

+—–+———————+————-+——–+

| 102 | 2009-10-08 00:00:00 |           3 |   3000 |

| 100 | 2009-10-08 00:00:00 |           3 |   1500 |

| 101 | 2009-11-20 00:00:00 |           2 |   1560 |

| 103 | 2008-05-20 00:00:00 |           4 |   2060 |

+—–+———————+————-+——–+

SQL> SELECT ID, NAME, AGE, AMOUNT

   FROM CUSTOMERS, ORDERS

   WHERE  CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

Leave a Reply

error: Content is protected !!