![](https://codinginterface.com/wp-content/uploads/2023/03/wilderchris_excited_programmer_taking_notes_in_front_of_a_compu_b14576da-1a05-4505-a8b9-b44bc52c9d84-1-edited.png)
When working on something I don’t work on a lot and I need a reference source I write a cheat sheet.
Here’s a simple SQL query language cheat sheet:
SELECT:
The SELECT statement is used to retrieve data from one or more tables.
Example:
SELECT column1, column2 FROM table_name;
FROM:
The FROM clause is used to specify the table(s) from which data is to be retrieved.
Example:
SELECT column1, column2 FROM table_name WHERE condition;
WHERE:
The WHERE clause is used to filter the results of a query based on a specified condition.
Example:
SELECT column1, column2 FROM table_name WHERE column3 = 'value';
ORDER BY:
The ORDER BY clause is used to sort the results of a query based on one or more columns.
Example:
SELECT column1, column2 FROM table_name ORDER BY column1 ASC;
GROUP BY:
The GROUP BY clause is used to group the results of a query by one or more columns.
Example:
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
HAVING:
The HAVING clause is used to filter the results of a GROUP BY query based on a specified condition.
Example:
SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING COUNT(*) > 1;
JOIN:
The JOIN clause is used to combine data from two or more tables based on a common column.
Example:
SELECT column1, column2 FROM table1 JOIN table2 ON table1.column3 = table2.column4;
INNER JOIN:
The INNER JOIN clause is used to combine data from two or more tables based on a common column, returning only the rows that have a match in both tables.
Example:
SELECT column1, column2 FROM table1 INNER JOIN table2 ON table1.column3 = table2.column4;
LEFT JOIN:
The LEFT JOIN clause is used to combine data from two or more tables based on a common column, returning all rows from the left table and matching rows from the right table.
Example:
SELECT column1, column2 FROM table1 LEFT JOIN table2 ON table1.column3 = table2.column4;
RIGHT JOIN:
The RIGHT JOIN clause is used to combine data from two or more tables based on a common column, returning all rows from the right table and matching rows from the left table.
Example:
SELECT column1, column2 FROM table1 RIGHT JOIN table2 ON table1.column3 = table2.column4;
UNION:
The UNION clause is used to combine the results of two or more SELECT statements into a single result set.
Example:
SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
LIKE:
The LIKE operator is used to search for a specified pattern in a column.
Example:
SELECT column1 FROM table_name WHERE column2 LIKE '%value%';
IN:
The IN operator is used to match a value against a list of values.
Example:
SELECT column1 FROM table_name WHERE column2 IN ('value1', 'value2', 'value3');
BETWEEN:
The BETWEEN operator is used to match a value against a range of values.
Example:
SELECT column1 FROM table_name WHERE column2 BETWEEN 'value1' AND 'value2';
DISTINCT:
The DISTINCT keyword is used to return only unique values in a column.
Example:
SELECT DISTINCT column1 FROM table_name;
These are some of the most commonly used SQL query language statements and operators. There are many more advanced features and functions available, but this cheat sheet
COUNT:
The COUNT function is used to count the number of rows in a table that meet a specified condition.
Example:
SELECT COUNT(*) FROM table_name WHERE column1 = 'value';
AVG:
The AVG function is used to calculate the average value of a column.
Example:
SELECT AVG(column1) FROM table_name;
SUM:
The SUM function is used to calculate the sum of a column.
Example:
SELECT SUM(column1) FROM table_name;
MAX:
The MAX function is used to find the maximum value in a column.
Example:
SELECT MAX(column1) FROM table_name;
MIN:
The MIN function is used to find the minimum value in a column.
Example:
SELECT MIN(column1) FROM table_name;
NULL:
The NULL value represents an unknown or missing value in a column.
Example:
SELECT column1 FROM table_name WHERE column2 IS NULL;
NOT NULL:
The NOT NULL constraint is used to specify that a column must not contain NULL values.
Example:
CREATE TABLE table_name (
column1 INT NOT NULL,
column2 VARCHAR(255) NOT NULL
);
PRIMARY KEY:
The PRIMARY KEY constraint is used to specify a column or set of columns as the primary key for a table.
Example:
CREATE TABLE table_name (
column1 INT PRIMARY KEY,
column2 VARCHAR(255)
);
FOREIGN KEY:
The FOREIGN KEY constraint is used to specify a relationship between two tables, based on a key column in each table.
Example:
CREATE TABLE table1 (
column1 INT PRIMARY KEY,
column2 VARCHAR(255)
);
CREATE TABLE table2 (
column3 INT PRIMARY KEY,
column4 VARCHAR(255),
column5 INT,
FOREIGN KEY (column5) REFERENCES table1(column1)
);
INDEX:
The INDEX statement is used to create an index on one or more columns in a table, to improve query performance.
Example:
CREATE INDEX index_name ON table_name (column1);
These are some of the most commonly used SQL statements and keywords. With this cheat sheet, you should be able to write basic SQL queries and understand the results. However, SQL is a vast and powerful language with many advanced features and functions. I encourage you to continue exploring and learning more about SQL as you work with databases.