SQL Queries: Select, Insert, Update, and Delete

SQL-Queries

Structured Query Language (SQL) is a widely-used programming language for managing and manipulating relational databases.

It allows users to perform various tasks, including creating, retrieving, updating, and deleting records.

In this article, we will cover the four main SQL queries – Select, Insert, Update, and Delete – and provide examples to help you understand their functionality.

Table of Contents

SELECT Query

The SELECT query is used to retrieve data from one or more tables in a database.

It can return specific columns, rows, or even perform calculations on the data. Below are some examples of SELECT queries:

Selecting all columns and rows from a table:

SELECT * FROM employees;

Selecting specific columns:

SELECT first_name, last_name FROM employees;

Filtering rows using a condition (WHERE clause):

SELECT * FROM employees WHERE salary > 50000;

Sorting results (ORDER BY clause):

SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC;

Aggregating data (GROUP BY and HAVING clauses):

SELECT department, COUNT(*) as employee_count FROM employees GROUP BY department HAVING employee_count > 10;

INSERT Query

The INSERT query is used to add new records to a table. It can either insert a single row or multiple rows at once. Below are some examples of INSERT queries:

Inserting a single row:

INSERT INTO employees (first_name, last_name, salary, department) VALUES ('John', 'Doe', 45000, 'Marketing');

Inserting multiple rows:

INSERT INTO employees (first_name, last_name, salary, department)
VALUES
  ('Jane', 'Smith', 60000, 'IT'),
  ('Bob', 'Johnson', 55000, 'Sales');

UPDATE Query

The UPDATE query is used to modify existing records in a table. It requires a SET clause to define the new values for the columns and a WHERE clause to specify the records to be updated.

Below are some examples of UPDATE queries:

Updating a single column:

UPDATE employees SET salary = salary * 1.1 WHERE department = 'IT';

Updating multiple columns:

UPDATE employees SET first_name = 'Robert', last_name = 'Brown' WHERE employee_id = 5;

DELETE Query

The DELETE query is used to remove records from a table. It requires a WHERE clause to specify the records to be deleted. Below are some examples of DELETE queries:

Deleting a single record:

DELETE FROM employees WHERE employee_id = 5;

Deleting multiple records:

DELETE FROM employees WHERE department = 'Sales' AND salary < 50000;

Conclusion

Understanding SQL queries such as SELECT, INSERT, UPDATE, and DELETE is crucial for managing and manipulating data in relational databases.

These queries allow you to retrieve, add, modify, and remove records effectively. By mastering the use of these queries, you will be well-equipped to handle a variety of database management tasks.


Thank you for reading our blog, we hope you found the information provided helpful and informative. We invite you to follow and share this blog with your colleagues and friends if you found it useful.

Share your thoughts and ideas in the comments below. To get in touch with us, please send an email to dataspaceconsulting@gmail.com or contactus@dataspacein.com.

You can also visit our website – DataspaceAI

Leave a Reply