SQL (Structured Query Language) is a standard language used for managing relational databases. Here are some of the most common SQL commands:
- SELECT: used to retrieve data from a database table
Example:
SELECT * FROM customers;
- INSERT: used to add data to a database table
Example:
INSERT INTO customers (first_name, last_name, email) VALUES ('John', 'Doe', '[email protected]');
- UPDATE: used to modify existing data in a database table
Example:
UPDATE customers SET first_name = 'Jane' WHERE id = 1;
- DELETE: used to delete data from a database table
Example:
DELETE FROM customers WHERE id = 1;
- CREATE TABLE: used to create a new database table
Example:
CREATE TABLE customers ( id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(50) );
- ALTER TABLE: used to modify an existing database table
Example:
ALTER TABLE customers ADD phone VARCHAR(20);
- DROP TABLE: used to delete an entire database table
Example:
DROP TABLE customers;
These are just a few of the many SQL commands that exist.
Types of SQL Commands:
SQL commands can be classified into several categories based on their functionality. Here are the main types of SQL commands:
- Data Manipulation Language (DML) Commands:
- SELECT: Retrieve data from one or more tables.
- INSERT: Add new rows of data into a table.
- UPDATE: Modify existing data in a table.
- DELETE: Remove rows of data from a table.
- Data Definition Language (DDL) Commands:
- CREATE: Create a new database, table, view, index, or other database objects.
- ALTER: Modify the structure of a database object, such as adding or dropping columns from a table.
- DROP: Delete a database, table, view, index, or other database objects.
- TRUNCATE: Remove all data from a table, but keep the table structure intact.
- Data Control Language (DCL) Commands:
- GRANT: Provide user permissions and privileges to access database objects.
- REVOKE: Remove user permissions and privileges from accessing database objects.
- Transaction Control Commands:
- COMMIT: Save the changes made in the current transaction.
- ROLLBACK: Undo the changes made in the current transaction.
- SAVEPOINT: Set a specific point in a transaction to which you can roll back later.
- Data Query Language (DQL) Commands:
- SELECT: Retrieve data from one or more tables (also part of DML).
- JOIN: Combine rows from two or more tables based on related columns.
- GROUP BY: Group rows based on one or more columns.
- HAVING: Filter groups based on conditions.
- ORDER BY: Sort the result set based on one or more columns.
- Data Manipulation Functions:
- COUNT: Count the number of rows or non-null values in a column.
- SUM: Calculate the sum of values in a column.
- AVG: Calculate the average of values in a column.
- MAX: Retrieve the maximum value in a column.
- MIN: Retrieve the minimum value in a column.
These are some of the common types of SQL commands. The specific commands available may vary slightly depending on the database management system (DBMS) you are using, as different DBMS may have their own additional commands or variations.