You are on page 1of 1

SQL Cheatsheet

Joins
What is SQL?
Structured Query Language or SQL is a
standard Database language which is used to
create, maintain and retrieve the relational
database

Types of SQL Commands


Left Join Right Join Inner Join Cartesian Join
 DDL - Define the schema of database or
its objects (like tables and indexes) (Ex -
CREATE, ALTER, DROP)
 DML - Manipulate and Select data in the Inserting data in a table What is an index?
database (Ex – SELECT, INSERT)  Data structure that
 DCL - Rights, permissions and other INSERT INTO<tableName> improves the speed of
controls of the database system (Ex – (column1, column2, column3, ...) operations in a table.
GRANT, REVOKE)
VALUES (value1, value2,  Indexes can be created
value3, ...); using one or more
Most Common Datatypes
• int(10)
columns
• varchar(255)
Basic Query Syntax  Indexing a column
• text improves search but
• TIMESTAMP SELECT f(col1), g(col2),… from increases the time for
• ENUM (’Choice1’, ‘Choice2’, …) table1 insert and update
---filter the rows
Importing and Exporting data from WHERE col2=1 and col5=4 Creating an Index
CSV ---Aggregate the data
create index <index_name>
GROUPBY .. on <table> (<Column to
Importing data from CSV - LOAD --- Filter the results
Index>)
DATA LOCAL INFILE <full_file_path> HAVING h(col4)>=<…
INTO TABLE <tableName> --- Sort the results
COLUMNS TERMINATED BY ',’ ORDER BY col2
OPTIONALLY ENCLOSED BY '” ESCAPED
BY '"' f, g, h are aggregation functions
LINES TERMINATED BY '\n’ like
IGNORE 1 LINES; COUNT(*), COUNT(DISTINCT),
SUM(), STDDEV() etc
Exporting data to CSV - SELECT *
INTO OUTFILE <outputFilePath> Other useful Keywords which
FIELDS TERMINATED BY ',' OPTIONALLY work with SELECT
ENCLOSED BY '"' DISTINCT
LINES TERMINATED BY '\n' FROM LIKE
<tableName>; BETWEEN
IN
Creating a table Syntax
CREATE TABLE <tableName> (

<fieldname1><fieldtype1>
(NULL/NNOT NULL) ,
<fieldname2><fieldtype2>
);

You might also like