SQL commands are instructions used to interact with databases. They allow you to perform various operations, such as retrieving, manipulating, and managing data within a database. These commands can be grouped into different categories based on their functions:
These commands are used to query and retrieve data from a database. The most common DQL command is SELECT
, which allows you to retrieve specific data from one or more tables based on certain conditions.
These commands are used to define and modify database structures, such as tables and schemas. CREATE
is used to create new tables or other database objects, ALTER
is used to modify existing objects (like adding or deleting columns), and DROP
is used to delete objects like tables or views from the database.
These commands are used to manipulate the data within the database. INSERT
is used to add new records to a table, UPDATE
is used to modify existing records, and DELETE
is used to remove records from a table.
These commands are used to control access to the data in the database. GRANT
gives specific privileges (like read or write access) to a user, and REVOKE
removes those privileges.
These commands manage transactions in a database, ensuring that operations are completed successfully or rolled back in case of errors. COMMIT
saves all changes made during a transaction, while ROLLBACK
undoes changes. SAVEPOINT
allows you to set a point within a transaction to which you can roll back, and SET TRANSACTION
is used to configure transaction properties like isolation levels.
These commands are used to retrieve data from the database.
employees
table.”
SELECT * FROM employees;
SELECT name, salary FROM employees WHERE salary > 50000;
These commands are used to define and modify database structures like tables, views, and indexes.
CREATE: Creates a new table, database, or other objects.
employees
.”
CREATE TABLE employees (id INT, name VARCHAR(50), salary DECIMAL(10, 2));
ALTER: Modifies an existing database object (like a table or column).
hire_date
to the employees
table.”
ALTER TABLE employees ADD hire_date DATE;
DROP: Deletes a table, view, or other database objects.
employees
table.”
DROP TABLE employees;
TRUNCATE: Deletes all rows from a table, but keeps the table structure.
employees
table.”
TRUNCATE TABLE employees;
These commands are used to insert, update, and delete data in the tables.
INSERT: Adds new records (rows) to a table.
employees
table.”
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 60000);
UPDATE: Modifies existing records in a table.
UPDATE employees SET salary = 65000 WHERE id = 1;
DELETE: Removes records from a table.
employees
table.”
DELETE FROM employees WHERE id = 1;
These commands are used to control permissions and access to the database.
GRANT: Provides specific privileges to a user or role.
SELECT
privilege to user john
on the employees
table.”
GRANT SELECT ON employees TO john;
REVOKE: Removes privileges from a user or role.
SELECT
privilege from user john
on the employees
table.”
REVOKE SELECT ON employees FROM john;
These commands are used to manage transactions, ensuring consistency and reliability.
COMMIT: Saves the changes made during a transaction.
COMMIT;
ROLLBACK: Reverts all changes made during the current transaction.
ROLLBACK;
SAVEPOINT: Sets a point within a transaction to which you can later roll back.
SAVEPOINT sp1;
SET TRANSACTION: Configures properties like transaction isolation levels.
SERIALIZABLE
.”
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;