Skip to main content

Command Palette

Search for a command to run...

Types of SQL Command

Published
4 min read
Types of SQL Command
A

I am java developer from India.

Structured Query Language (SQL)

SQL (Structured Query Language) is a standardized programming language used for managing relational databases. It provides a set of commands and syntax that allow users to interact with databases, retrieve data, manipulate data, and define the structure of the database.

SQL is primarily used for:

  1. Database Creation: SQL provides commands for creating databases, tables, views, indexes, and other database objects.

  2. Data Manipulation: SQL allows users to insert, update, delete, and retrieve data from a database. It includes commands for filtering, sorting, and joining data from multiple tables.

  3. Data Definition: SQL enables users to define and modify the structure of a database, including creating tables, altering table structures, and defining constraints such as primary keys and foreign keys.

  4. Data Control: SQL includes commands for granting and revoking permissions and privileges to users, controlling access to the database objects.

SQL is a declarative language, which means users specify what data they want to retrieve or modify without specifying how to do it. The database management system (DBMS) responsible for executing SQL commands determines the most efficient way to perform the requested operations.

The syntax of SQL commands follows a set of rules and conventions, allowing users to write queries and statements that interact with the database effectively. Different database management systems may have slight variations in their implementation of SQL, but the core concepts and commands remain consistent across most systems.

Types of SQL Command

Here's an explanation of each type of SQL command and examples of their usage:

  1. Data Manipulation Language (DML) Commands:

    • SELECT: Retrieves data from one or more tables. It is used to query the database and retrieve specific data based on conditions.

      Example: SELECT * FROM customers WHERE age > 30;

    • INSERT: Inserts new rows of data into a table. It is used to add new records to a table.

      Example: INSERT INTO employees (name, age) VALUES ('John Doe', 35);

    • UPDATE: Modifies existing data in a table. It is used to update values in one or more columns of a table.

      Example: UPDATE products SET price = 9.99 WHERE id = 1;

    • DELETE: Deletes rows of data from a table. It is used to remove records from a table based on specified conditions.

      Example: DELETE FROM orders WHERE order_date < '2023-01-01';

  2. Data Definition Language (DDL) Commands:

    • CREATE: Creates a new database, table, or other database objects. It is used to define the structure of the database.

      Example: CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50), email VARCHAR(100));

    • ALTER: Modifies the structure of a database object. It is used to alter existing database objects like tables, columns, or constraints.

      Example: ALTER TABLE employees ADD COLUMN salary DECIMAL(10,2);

    • DROP: Deletes a database object. It is used to remove tables, databases, or other objects from the database.

      Example: DROP TABLE customers;

    • TRUNCATE: Removes all data from a table. It is used to delete all rows from a table while keeping its structure intact.

      Example: TRUNCATE TABLE orders;

  3. Data Control Language (DCL) Commands:

    • GRANT: Provides user permissions and privileges to database objects. It is used to grant specific privileges to database users.

      Example: GRANT SELECT, INSERT ON employees TO user1;

    • REVOKE: Removes user permissions and privileges from database objects. It is used to revoke previously granted privileges from database users.

      Example: REVOKE UPDATE ON products FROM user2;

  4. Transaction Control Language (TCL) Commands:

    • COMMIT: Saves changes permanently to the database. It is used to make the changes performed within a transaction permanent.

      Example: COMMIT;

    • ROLLBACK: Reverts the database to its previous state. It is used to undo changes made within a transaction.

      Example: ROLLBACK;

    • SAVEPOINT: Sets a point within a transaction to which it can be rolled back. It is used to create a named marker within a transaction.

      Example: SAVEPOINT sp1;

  5. Data Query Language (DQL) Commands:

    • SELECT: Retrieves data from one or more tables. Same as explained in the DML commands above.

      Example: SELECT * FROM customers WHERE age > 30;

    • JOIN: Combines rows from two or more tables based on related columns. It is used to retrieve data from multiple tables based on common columns.

      Example: SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id;

    • ORDER BY: Sorts the result set based on specified columns. It is used to arrange the output in ascending or descending order.

      Example: SELECT * FROM products ORDER BY price DESC;. Here DESC stands for descending order and if you want to do it in Ascending order use ASC.

    • GROUP BY: Groups rows based on specified columns. It is used to group rows with similar values in specified columns and apply aggregate functions.

      Example: SELECT category, COUNT(*) FROM products GROUP BY category;

These are the main types of SQL commands with explanations and examples of their usage. Keep in mind that the syntax and specific features may vary depending on the database management system (DBMS) being used.

We appreciate your interest in types of SQL Commands and hope this post has been helpful to you. If you have any further questions or feedback, please feel free to reach out to us. Thank you for reading!