# SQL Cheatsheet

{% embed url="<https://quizlet.com/325416045/sql-flash-cards/?i=fnawl&x=1jqt>" fullWidth="true" %}

## DDL Commands

<div data-full-width="true"><figure><img src="https://824924021-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjEG2NepZQd4awmKtG6qY%2Fuploads%2Fg11Evw2IAHiUf6KDqMQO%2FScreen%20Shot%202023-07-26%20at%204.02.04%20AM.png?alt=media&#x26;token=b84f117a-afec-4eab-b020-ade59fa52687" alt=""><figcaption></figcaption></figure></div>

## DML Commands

<div data-full-width="true"><figure><img src="https://824924021-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjEG2NepZQd4awmKtG6qY%2Fuploads%2F3rWGOGuO0STY7ZY1cAO4%2FScreen%20Shot%202023-07-26%20at%204.02.25%20AM.png?alt=media&#x26;token=6a929c81-00a7-4395-8a01-add2bc2a63fa" alt=""><figcaption></figcaption></figure></div>

## Data Type

<div data-full-width="true"><figure><img src="https://824924021-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjEG2NepZQd4awmKtG6qY%2Fuploads%2FaF7vD09ExzfxbNxgJh8K%2FScreen%20Shot%202023-07-26%20at%204.03.26%20AM.png?alt=media&#x26;token=fd419593-16c0-40df-9188-0c8dbc2f6e92" alt=""><figcaption></figcaption></figure></div>

## Constraints

<table data-full-width="true"><thead><tr><th width="177.24999999999997">Constraint</th><th>Explanation</th><th>Rules</th><th>SQL Example</th></tr></thead><tbody><tr><td>Primary Key</td><td>Uniquely identifies each record in a table. Only one primary key allowed.</td><td>- Column(s) must be unique<br>- Cannot contain null values<br>- Limited to one per table</td><td><code>CREATE TABLE table_name ( id INT PRIMARY KEY );</code></td></tr><tr><td>Foreign Key</td><td>Links to primary key in another table. Enforces referential integrity.</td><td>- Column(s) must match type of referenced column(s)<br>- Values must exist in referenced table</td><td><code>CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(id) );</code></td></tr><tr><td>Unique</td><td>Ensures values in column are unique. Only distinct values allowed.</td><td>- Column(s) cannot contain duplicate values</td><td><code>CREATE TABLE users ( id INT PRIMARY KEY, username VARCHAR(50) UNIQUE );</code></td></tr><tr><td>Not Null</td><td>Mandates column cannot contain NULL values.</td><td>- Column cannot contain null values</td><td><code>CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL );</code></td></tr><tr><td>Check</td><td>Validates values against logical expression.</td><td>- Inserted/updated values must evaluate to true</td><td><code>CREATE TABLE users ( age INT CHECK (age > 0) );</code></td></tr><tr><td>Default</td><td>Sets default value if none provided.</td><td>- Default value is inserted if no other value specified</td><td><code>CREATE TABLE users ( country VARCHAR(50) DEFAULT 'United States' );</code></td></tr><tr><td>Index</td><td>Improves lookup performance on columns.</td><td>- Can be unique or non-unique<br>- Single column or multiple columns</td><td><code>CREATE INDEX idx_lastname ON users(lastname);</code></td></tr></tbody></table>

## Joins

<table data-full-width="true"><thead><tr><th width="158.24999999999997">Join Type</th><th>Explanation</th><th>Rules</th><th>Example</th></tr></thead><tbody><tr><td>INNER JOIN</td><td>Returns records that have matching values in both tables</td><td>Matches rows from the first table with rows from the second table where the join condition is met</td><td><code>SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id</code></td></tr><tr><td>LEFT JOIN</td><td>Returns all records from the left table, and the matched records from the right table</td><td>Returns all rows from the left table, along with any matching rows from the right table</td><td><code>SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id</code></td></tr><tr><td>RIGHT JOIN</td><td>Returns all records from the right table, and the matched records from the left table</td><td>Returns all rows from the right table, along with any matching rows from the left table</td><td><code>SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id</code></td></tr><tr><td>FULL OUTER JOIN</td><td>Returns all records when there is a match in either left or right table</td><td>Returns rows when there is a match in one of the tables</td><td><code>SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id</code></td></tr><tr><td>CROSS JOIN</td><td>Returns rows combined by cartesian product between two tables</td><td>No join condition needed, combines every row from the first table with every row from the second table</td><td><code>SELECT * FROM table1 CROSS JOIN table2</code></td></tr></tbody></table>

## GROUP

<table data-full-width="true"><thead><tr><th>Term</th><th>Explanation</th><th>Rules</th><th>Example</th></tr></thead><tbody><tr><td>GROUP BY</td><td>Used to group rows that have the same values into summary rows</td><td>Used after the FROM and WHERE clauses. Must be used with aggregate functions like COUNT, MAX etc</td><td><code>SELECT department, COUNT(*) FROM employees GROUP BY department</code></td></tr><tr><td>GROUP BY Column</td><td>Groups rows based on values in the specified column</td><td>Column used in GROUP BY must be present in the SELECT statement</td><td><code>SELECT department, COUNT(*) FROM employees GROUP BY department</code></td></tr><tr><td>GROUP BY Multiple Columns</td><td>Groups rows based on multiple column values</td><td>List multiple columns in GROUP BY to group on combinations of values</td><td><code>SELECT department, city, COUNT(*) FROM employees GROUP BY department, city</code></td></tr><tr><td>HAVING</td><td>Filters grouped rows that meet a specified condition</td><td>Goes after GROUP BY. Can reference aggregated columns</td><td><code>SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 10</code></td></tr><tr><td>ROLLUP</td><td>Provides subtotals at different grouping levels</td><td>Used after GROUP BY, provides extra super-aggregate rows</td><td><code>SELECT year, quarter, SUM(profit) FROM sales GROUP BY year, quarter WITH ROLLUP</code></td></tr></tbody></table>
