0% found this document useful (0 votes)
5 views3 pages

SQL Commands

This SQL Cheat Sheet provides a comprehensive overview of essential SQL commands, including creation, deletion, and manipulation of databases and tables. It covers various operations such as inserting, updating, and selecting data, as well as advanced features like triggers, assertions, and joins. Additionally, it outlines data types, arithmetic operators, and aggregate functions for effective database management.

Uploaded by

pratik kujur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

SQL Commands

This SQL Cheat Sheet provides a comprehensive overview of essential SQL commands, including creation, deletion, and manipulation of databases and tables. It covers various operations such as inserting, updating, and selecting data, as well as advanced features like triggers, assertions, and joins. Additionally, it outlines data types, arithmetic operators, and aggregate functions for effective database management.

Uploaded by

pratik kujur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Cheat Sheet

by Rahil (rahilkasimi) via [Link]/102433/cs/32112/

COMMANDS

COMMAND CODE DESCRI​PTION


Create CREATE DATABASE <DA​TABASE NAME> used to create a new database or table
CREATE TABLE <TABLE NAME>

Drop DROP DATABASE <DA​TABASE NAME> used to delete an existing database or table
DROP TABLE <TABLE NAME>
Truncate TRUNCATE TABLE <TABLE NAME> used to delete inform​ation in the table but doesn
t delete the table itself
Alter ALTER TABLE <TABLE NAME> used to delete, add or modify constr​aints or
ADD <COLUMN NAME> <DATA TYPE> columns in a table
ALTER TABLE <TABLE NAME>
DROP COLUMN <COLUMN NAME>
ALTER TABLE <TABLE NAME>
ALTER COLUMN <COLUMN NAME> <DATA TYPE>
Backup BACKUP DATABASE <DA​TABASE NAME> used to create a backup on an existing database
TO DISK = ‘<P​ATH​>’
Insert INSERT INTO <TABLE NAME> (<C​OLU​MN1​>, ....) used to insert new tuples (rows) in a table
VALUES (<V​ALU​E1>, ....) *you do not need to specify all columns if you
will add values for all the columns
Delete DELETE FROM <TABLE NAME> used to delete tuples (rows) from a table
WHERE <CO​NDI​TIO​N> *if you don t add the WHERE clause, all rows
will be deleted
Update UPDATE <TABLE NAME> used to modify existing records in a table
SET <COLUMN NAME> = <NEW VALUE>
WHERE <CO​NDI​TIO​N>
Select SELECT <AT​TRIBUTE LIST> used to select data from a table
FROM <TABLE NAME> *if you want all attributes of a table use (*)
WHERE <CO​NDI​TIO​N>
Union, Intersect, <FIRST SELECT STATEM​ENT> equivalent to the set operat​ions: union, inters​‐
Except UNION / INTERSECT / EXCEPT ection and differ​ence.
<SECOND SELECT STATEM​ENT>

In SELECT <AT​TRIBUTE LIST> compares a value with a set of values, returns


FROM <TABLE NAME> true if the value is one of the elements of the set.
WHERE <VA​LUE> IN <AN​OTHER SELECT QUERY>
Null <AT​TRIBUTE NAME> IS (NOT) NULL used to check whether a value is NULL
Join SELECT <AT​TRI​BUTES LIST> used to join two tables based on a related
FROM <TABLE 1> JOIN <TABLE 2> column between them
ON <JOIN CONDIT​ION>
WHERE <SE​LECTION CONDIT​ION>

By Rahil (rahilkasimi) Published 17th May, 2022. Sponsored by [Link]


[Link]/rahilkasimi/ Last updated 17th May, 2022. Measure your website readability!
Page 1 of 3. [Link]
SQL Cheat Sheet
by Rahil (rahilkasimi) via [Link]/102433/cs/32112/

COMMANDS (cont)

Assertion CREATE ASSERTION <AS​SERTION NAME> used to ensure a certain condition is always met
CHECK (<C​OND​ITI​ON>) in the database
Trigger CREATE TRIGGER <TR​IGGER NAME> Triggers are activated when a defined action is
BEFORE / AFTER executed for the table
INSERT / UPDATE / DELETE
ON <TABLE NAME>
FOR EACH ROW
<TRIGGER BODY>
Data Types Numeric - INT, SMALLINT, DECIMAL(i, j)
String - CHAR, CHAR(n), VARCHAR(n)
Bit Sring - BIT, BIT(n)
Date and Time - DATE, TIME, TIME(i)
Timestamp -TIMESTAMP
Refere​ntial ON DELETE <OP​TIO​N> used to set what happens on updating or
Triggered Action ON UPDATE <OP​TIO​N> deleting a tuple (row) in the database that
references another row
OPTIONS:
SET NULL
SET DEFAULT
CASCADE
Renaming <TABLE NAME> AS <NEW TABLE NAME> Relation and attribute names can be renamed
(Aliasing) (<NEW ATTRIBUTE 1 NAME>, .....) for conenience or to remove ambiguity using the
keyword AS
Cross Product (,) SELECT <AT​TRIBUTE LIST> used to produce a result table that has the
FROM <TABLE 1>, <TABLE 2> number of rows of the first table multiplied by the
number of rows of the second table
Duplicates SELECT ALL <AT​TRIBUTE LIST> DISTINCT is used to eliminate duplicates
FROM <TABLE NAME> <AT​TRI​BUT​E> LIKE <PA​TTE​RN> <AT​‐ ALL is used to allow duplicates *SELECT
TRIBUTE NAME> IS (NOT) NULL without ALL or DISTINCT is equivalent to ALL
SELECT <AT​TRI​BUTES LIST>
FROM <TABLE 1> JOIN <TABLE 2> ON <JOIN CONDIT​ION>
WHERE <SE​LECTION CONDIT​ION> SELECT DISTINCT <AT​‐
TRIBUTE LIST> FROM <TABLE NAME>
String Compar​‐ <AT​TRI​BUT​E> LIKE <PA​TTE​RN> LIKE is used for string comparisoJ
isons (%) replaces an arbitary number of characters
(_) replaces one character

By Rahil (rahilkasimi) Published 17th May, 2022. Sponsored by [Link]


[Link]/rahilkasimi/ Last updated 17th May, 2022. Measure your website readability!
Page 2 of 3. [Link]
SQL Cheat Sheet
by Rahil (rahilkasimi) via [Link]/102433/cs/32112/

COMMANDS (cont)

Arithmetic # (+) add ​ ​ ​ ​ ​ ​ ​ ​ ​ # (*) multiply


Operators # ( ) subtract ​ ​ ​ ​ # (/) divide
Ordering <SELECT STATEM​ENT> ORDER BY is used to order the resulting tuples
ORDER BY <AT​TRI​BUT​E> <ASC / DESC> The keyword ASC (ascen​ding) and DESC can
be used. *The default is ASC (ascen​ding)
Set Compar​isons SELECT <AT​TRIBUTE LIST> ANY and ALL can be used with (=, >, >=, <, <=,
FROM <TABLE NAME> <>) to compare a value with a set
WHERE <VA​LUE> > ALL / ANY <AN​OTHER SELECT QUERY> #CONTAINS Compares two sets and returns
true if one set contains the other
#EXISTS It checks whether the result of a
nested query is empty or not
#UNIQUE checks if the table has duplicates
Aggregate #COUNT - Counts how many rows in a
Functions particular column
#SUM - adds together all the values in a
particular column
#MIN returns the minumum value in a column
#MAX returns the maximum value in a column
#AVG - returns the average of a group of
selected values
Types of Join Inner join
Left Join
Right Join
Full Outer Join

Credit: @yosra​codes

By Rahil (rahilkasimi) Published 17th May, 2022. Sponsored by [Link]


[Link]/rahilkasimi/ Last updated 17th May, 2022. Measure your website readability!
Page 3 of 3. [Link]

You might also like