SQL Commands Cheatsheet

SQL Command Overview

CommandCategoryDescription
SELECTDMLRetrieves data from a table.
INSERTDMLAdds new records to a table.
UPDATEDMLModifies existing data in a table.
DELETEDMLRemoves data from a table.
CREATE TABLEDDLCreates a new table.
ALTER TABLEDDLModifies the structure of an existing table.
DROP TABLEDDLDeletes an existing table and its data.
GRANTDCLGrants specific privileges to a user or role.
REVOKEDCLRevokes specific privileges from a user or role.
COMMITDML/DCLSaves changes made through DML statements and ends a transaction (also related to DCL for transaction control).

DML (Data Manipulation Language):

  1. SELECT – Retrieve data from a table.
  • Syntax:
    sql SELECT column1, column2 FROM table_name WHERE condition;
  • Example:
    sql SELECT product_name, price FROM products WHERE category = 'Electronics';
  1. INSERT – Insert data into a table.
  • Syntax:
    sql INSERT INTO table_name (column1, column2) VALUES (value1, value2);
  • Example:
    sql INSERT INTO customers (customer_name, email) VALUES ('John Doe', 'john.doe@example.com');
  1. UPDATE – Modify existing data in a table.
  • Syntax:
    sql UPDATE table_name SET column1 = new_value WHERE condition;
  • Example:
    sql UPDATE products SET price = 199.99 WHERE product_id = 101;
  1. DELETE – Remove data from a table.
  • Syntax:
    sql DELETE FROM table_name WHERE condition;
  • Example:
    sql DELETE FROM customers WHERE customer_id = 3;

DDL (Data Definition Language):

  1. CREATE TABLE – Create a new table.
  • Syntax:
    sql CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
  • Example:
    sql CREATE TABLE products ( product_id INT, product_name VARCHAR(255), price DECIMAL(10, 2) );
  1. ALTER TABLE – Modify the structure of an existing table.
  • Syntax:
    sql ALTER TABLE table_name ADD column_name datatype;
  • Example:
    sql ALTER TABLE products ADD quantity INT;
  1. DROP TABLE – Delete an existing table and its data.
  • Syntax:
    sql DROP TABLE table_name;
  • Example:
    sql DROP TABLE invoices;

DCL (Data Control Language):

  1. GRANT – Provide specific privileges to a user or role.
  • Syntax:
    sql GRANT privilege ON object TO user_or_role;
  • Example:
    sql GRANT SELECT, INSERT ON products TO sales_team;
  1. REVOKE – Revoke specific privileges from a user or role.
  • Syntax:
    sql REVOKE privilege ON object FROM user_or_role;
  • Example:
    sql REVOKE INSERT ON products FROM sales_team;
  1. COMMIT – Save changes to the database.
  • Syntax:
    sql COMMIT;
  • Example:
    sql -- After executing a series of INSERT, UPDATE, or DELETE statements COMMIT;

This cheat sheet covers the basic SQL commands for data manipulation, definition, and control. Use these examples as a starting point for your SQL operations in a database environment.