Using Basic SQL Commands: SELECT, INSERT, UPDATE, and DELETE
You should be here after finishing previous content. Or click here to see table of content.
SQL (Structured Query Language) is a powerful tool for managing data in a database. SQL commands are used to insert, update, delete, and retrieve data from a database. In this article, we will cover the basic SQL commands: SELECT, INSERT, UPDATE, and DELETE.SELECT
The SELECT statement is used to retrieve data from a database. The syntax for the SELECT statement is as follows:
SELECT column1, column2, column3, ...
FROM table_name;
Here, column1, column2, column3, etc. are the columns that you want to retrieve data from, and table_name is the name of the table where the data is stored.
For example, let's say you have a table named employees with columns id, first_name, last_name, and email. To retrieve all the data from the employees table, you would write the following SQL query:
SELECT *
FROM employees;
This would retrieve all the data from the employees table.
You can also retrieve specific data from a table by using the WHERE clause in the SELECT statement. The WHERE clause is used to filter the results based on a condition. The syntax for the WHERE clause is as follows:
SELECT column1, column2, column3, ...
FROM table_name
WHERE condition;
Here, condition is the condition that must be met for the data to be retrieved.
For example, let's say you want to retrieve all the employees from the employees table who have an email address ending in "@example.com". To do this, you would write the following SQL query:
SELECT *
FROM employees
WHERE email LIKE '%@example.com';
This would retrieve all the employees from the employees table who have an email address ending in "@example.com".
INSERT
The INSERT statement is used to insert new data into a table. The syntax for the INSERT statement is as follows:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Here, table_name is the name of the table where you want to insert data, and column1, column2, column3, etc. are the columns where you want to insert data. value1, value2, value3, etc. are the values that you want to insert into the columns.
For example, let's say you want to insert a new employee into the employees table. The new employee has an id of 4, a first_name of "Jane", a last_name of "Doe", and an email of "[email protected]". To do this, you would write the following SQL query:
INSERT INTO employees (id, first_name, last_name, email)
VALUES (4, 'Jane', 'Doe', '[email protected]');
This would insert the new employee into the employees table.
UPDATE
The UPDATE statement is used to update existing data in a table. The syntax for the UPDATE statement is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Here, table_name is the name of the table where you want to update data. column1, column2, etc. are the columns that you want to update, and value1, value2,etc. are the new values for the columns. condition is the condition that must be met for the data to be updated.
For example, let's say you want to update the email address for an employee in the employees table. The employee has an id of 2 and their current email address is "[email protected]". You want to update their email address to "[email protected]". To do this, you would write the following SQL query:
UPDATE employees
SET email = '[email protected]'
WHERE id = 2;
This would update the email address for the employee with an id of 2 in the employees table.
DELETE
The DELETE statement is used to delete data from a table. The syntax for the DELETE statement is as follows:
DELETE FROM table_name
WHERE condition;
Here, table_name is the name of the table where you want to delete data, and condition is the condition that must be met for the data to be deleted.
For example, let's say you want to delete an employee from the employees table. The employee has an id of 3. To do this, you would write the following SQL query:
DELETE FROM employees
WHERE id = 3;
This would delete the employee with an id of 3 from the employees table.
Post a Comment