Catalog / SQL Cheat Sheet
SQL Cheat Sheet
A quick reference guide to SQL, covering essential commands, data types, and functions for database management and querying.
Basic SQL Commands
Data Definition Language (DDL)
|
Creates a new table in the database. |
|
Adds a new column to an existing table. |
|
Deletes a column from an existing table. |
|
Modifies the data type of a column. |
|
Deletes a table from the database. |
|
Removes all rows from a table, but keeps the table structure. |
Data Manipulation Language (DML)
|
Inserts a new row into a table. |
|
Updates existing rows in a table based on a condition. |
|
Deletes rows from a table based on a condition. |
|
Retrieves data from one or more tables. |
|
Retrieves all columns from a table. |
Data Control Language (DCL)
|
Grants privileges to a user on a specific database object. |
|
Revokes privileges from a user on a specific database object. |
SQL Querying
Basic SELECT Statement
|
Aggregate Functions
|
Returns the number of rows. |
|
Returns the sum of values in a column. |
|
Returns the average value of a column. |
|
Returns the minimum value in a column. |
|
Returns the maximum value in a column. |
GROUP BY and HAVING
|
Groups rows that have the same values in a column into summary rows. |
|
Filters the results of a |
Example |
|
Joins and Subqueries
Joins
Joins are used to combine rows from two or more tables based on a related column.
|
Join Syntax
|
Inner Join Example |
|
Left Join Example |
|
Right Join Example |
|
Full Outer Join Example |
Subqueries
A subquery is a query nested inside another SQL query. Subqueries can be used in Example: |
Transactions and Indexing
Transactions
A transaction is a sequence of SQL operations that are performed as a single logical unit of work.
|
Transaction Examples
|
Transfers $100 from account 1 to account 2. |
|
If any error occurs, all changes are rolled back. |
Indexing
Indexes are special lookup tables that the database search engine can use to speed up data retrieval. Simply put, an index is a pointer to data in a table.
|