> For the complete documentation index, see [llms.txt](https://qatesting.gitbook.io/qa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qatesting.gitbook.io/qa/database-testing/sql-cheatsheet.md).

# 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&amp;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&amp;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&amp;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>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://qatesting.gitbook.io/qa/database-testing/sql-cheatsheet.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
