SQL Cheatsheet

January 24, 2024

Table of Contents

  1. Fundamental SQL Queries
  2. Data Modification Queries
  3. Aggregate Function Queries
  4. Join Queries
  5. View Queries
  6. Table Queries

1. Fundamental SQL Queries

SELECT: Retrieves data from a database

DISTINCT: Eliminates duplicate values in the specified column(s)

WHERE: Filters records based on specific conditions

ORDER BY: Sorts the result-set in ascending or descending order

LIMIT, OFFSET: Limits the number of rows returned by the query and specifies the number of rows to skip

LIKE: Operator used in a WHERE clause to search for a specific pattern in a column

IN: Operator that allows you to specify multiple values in a WHERE clause

BETWEEN: Operator selects values within a given range (inclusive)

NULL: Values in a field with no value

AS: Aliases are used to assign a temporary name to a table or column

UNION: Set operator used to combine the result-set of two or more SELECT statements

INTERSECT: Set operator which is used to return the records that two SELECT statements have in common

EXCEPT: Set operator used to return all the records in the first SELECT statement that are not found in the second SELECT statement

ANY|ALL: Operators used to check subquery conditions within WHERE or HAVING clauses

GROUP BY: Often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns

HAVING: This clause was added to SQL because the WHERE keyword could not be used with aggregate functions

WITH: Often used for retrieving hierarchical data or re-using a temporary result set multiple times in a query. Also referred to as “Common Table Expression”


2. Data Modification Queries

INSERT INTO: Used to insert new records/rows in a table

UPDATE: Used to modify the existing records in a table

DELETE: Used to delete existing records/rows in a table


3. Aggregate Function Queries

COUNT: Returns the number of occurrences

MIN() and MAX(): Returns the smallest/largest value of the selected column

AVG(): Returns the average value of a numeric column

SUM(): Returns the total sum of a numeric column


4. Join Queries

INNER JOIN: Returns records with matching values in both tables

LEFT (OUTER) JOIN: Returns all records from the left table (table1), and the matched records from the right table (table2)

RIGHT (OUTER) JOIN: Returns all records from the right table (table2), and the matched records from the left table (table1)

FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

Self JOIN: A regular join where the table is joined with itself


5. View Queries

CREATE: Creates a view

SELECT: Retrieves a view

DROP: Drops a view


6. Table Queries

Constraints

CREATE TABLE Employees (
    ID int NOT NULL,
    Name varchar(255) NOT NULL,
    Age int CHECK (Age >= 18),
    Salary decimal(8, 2) DEFAULT 50000.00,
    Email varchar(255) UNIQUE,
    DepartmentID int,
    PRIMARY KEY (ID),
    FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

CREATE INDEX idx_Employees_Name
ON Employees (Name);

ADD: Adds a column

MODIFY: Changes data type of a column

DROP: Deletes a column