SQL Cheat Sheet

-- Basic SQL Syntax
  
  -- SQL (Structured Query Language) is used to manage and manipulate relational databases. 
  -- It includes a range of statements for creating, querying, updating, and controlling access to databases.
  -- Here are some fundamental aspects of SQL syntax.
  
  -- Comments in SQL
  
  
  --SQL supports single-line and multi-line comments. 
  -- Single-line comments start with two dashes (--) and extend to the end of the line.
  -- Multi-line comments are enclosed between /* and */

  
  -- This is a single-line comment
  
  /*
     This is a
     multi-line comment
  */
  
  -- SQL Statements
  
  /* 
     SQL statements are classified into various categories based on their purpose:
     - DDL (Data Definition Language): Used to define and modify database structures (e.g., CREATE, ALTER, DROP).
     - DML (Data Manipulation Language): Used for querying and modifying data (e.g., SELECT, INSERT, UPDATE, DELETE).
     - DCL (Data Control Language): Used to control access to data (e.g., GRANT, REVOKE).
     - TCL (Transaction Control Language): Used to manage transactions (e.g., COMMIT, ROLLBACK, SAVEPOINT).
  */
  
  -- DDL Example: Creating a Table
  CREATE TABLE Employees (
      EmployeeID INT PRIMARY KEY,
      FirstName VARCHAR(50),
      LastName VARCHAR(50),
      HireDate DATE
  );
  
  -- DML Example: Inserting Data
  INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
  VALUES (1, 'John', 'Doe', '2020-01-01');
  
  -- DML Example: Querying Data
  SELECT * FROM Employees;
  
  -- DCL Example: Granting Permissions
  GRANT SELECT ON Employees TO Public;
  
  -- TCL Example: Committing a Transaction
  COMMIT;
  
  -- SQL Keywords and Case Sensitivity
  
  /* 
     SQL keywords are typically written in uppercase, though SQL is case-insensitive for keywords and identifiers.
     - `SELECT`, `FROM`, and `WHERE` are keywords in the SQL language.
     - Case sensitivity for data and table/column names depends on the database system.
  */
  
  SELECT FirstName, LastName FROM Employees WHERE EmployeeID = 1;
  
  -- Identifiers and Naming Conventions
  
  /* 
     Identifiers in SQL (such as table names, column names, and database names) follow these conventions:
     - They must start with a letter or underscore.
     - They can contain letters, digits, and underscores.
     - They should not exceed the maximum length defined by the database system.
     - Quoted identifiers (enclosed in double quotes) allow the use of spaces and special characters.
  
     Common naming conventions include using snake_case or camelCase for identifiers.
  */
  
  CREATE TABLE "Order Details" (
      OrderID INT,
      ProductID INT,
      Quantity INT
  );
  
  -- Basic SQL Structure
  
  /* 
     A basic SQL query typically includes the following structure:
     - SELECT: Specifies the columns to retrieve.
     - FROM: Specifies the table to query.
     - WHERE: Filters the rows based on conditions.
     - GROUP BY: Groups rows that share a value in one or more columns.
     - HAVING: Filters groups based on conditions.
     - ORDER BY: Sorts the rows based on specified columns.
  
     Here is a simple example combining these clauses:
  */
  
  SELECT FirstName, LastName, COUNT(*)
  FROM Employees
  WHERE HireDate > '2020-01-01'
  GROUP BY FirstName, LastName
  HAVING COUNT(*) > 1
  ORDER BY LastName;
  
  /* This query selects the first and last names of employees hired after January 1, 2020, groups them by name, filters groups with more than one member, and orders the results by last name. */
  
© 2024 CheatsheetCoder, All Rights Reserved.