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

SQL Cheat Sheet

This document provides a summary of common MySQL mathematical functions, string functions, and SQL commands for modifying data. It lists functions for counting, averaging, finding minimum/maximum values, summing values in a group, and performing mathematical operations. It also lists commands for creating tables, inserting, updating, and deleting data, as well as functions for comparing, converting, and extracting parts of strings.

Uploaded by

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

SQL Cheat Sheet

This document provides a summary of common MySQL mathematical functions, string functions, and SQL commands for modifying data. It lists functions for counting, averaging, finding minimum/maximum values, summing values in a group, and performing mathematical operations. It also lists commands for creating tables, inserting, updating, and deleting data, as well as functions for comparing, converting, and extracting parts of strings.

Uploaded by

GyanSingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL CHEAT SHEET MySQL Mathematical Functions

What How
Common MySQL Column Types
Count rows per group COUNT(column | *)
Purpose Data Type Example Average value of group AVG(column)
Integers int(M) int(5)
Minumum value of group MIN(column)
Floating-point (real) numbers float(M,D) float(12,3)
Maximum value of group MAX(column)
Double-precision floating-point double(M,D) double(20,3) Sum values in a group SUM(column)
Dates and times timestamp(M) timestamp(8) (for YYYYMMDD) Absolute value abs(number)
timestamp(12) (for
YYYYMMDDHHMMSS) Rounding numbers round(number)
Fixed-length strings char(M) char(10) Largest integer not greater floor(number)
Smallest integer not smaller ceiling(number)
Variable-length strings varchar(M) varchar(20)
Square root sqrt(number)
A large amount of text blob blob
nth power pow(base,exponent)
Values chosen from a list enum('value1',value2) enum('apples','oranges','bananas') random number n, 0<n < 1 rand()
M is maximum to display, and D is precision to the right of the decimal. sin (similar cos, etc.) sin(number)

Basic MySQL Commands MySQL String Functions


What How
What How Example(s)
Compare strings strcmp(string1,string2)
List all databases SHOW DATABASES; SHOW DATABASES;
Convert to lower case lower(string)
Create database CREATE DATABASE database; CREATE DATABASE
Convert to upper case upper(string)
PhoneDB;
Left-trim whitespace (similar right) ltrim(string)
Use a database USE database; USE PhonDB;
Substring of string substring(string,index1,index2)
List tables in the database SHOW TABLES; SHOW TABLES;
Encrypt password password(string)
Show the structure of a table DESCRIBE table; DESCRIBE Animals; Encode string encode(string,key)
SHOW COLUMNS FROM table; SHOW COLUMNS
FROM Animals; Decode string decode(string,key)
Get date curdate()
Delete a database DROP DATABASE database; DROP DATABASE
PhoneDB; Get time curtime()
Extract day name from date string dayname(string)
Extract day number from date string dayofweek(string)
Extract month from date string monthname(string)

G WORKS
SQL Commands: Modifying
What How Example(s)
CREATE TABLE table ( CREATE TABLE Students (
column1 type [[NOT] NULL] LastName varchar(30) NOT NULL,
[AUTO_INCREMENT], FirstName varchar(30) NOT NULL,
Create table column2 type [[NOT] NULL] StudentID int NOT NULL,
[AUTO_INCREMENT], Major varchar(20),
... Dorm varchar(20),
other options, PRIMARY KEY (StudentID) );
PRIMARY KEY (column(s)) );

INSERT INTO table VALUES INSERT INTO Students VALUES


(list of values); ('Smith','John',123456789,'Math','Selleck');
INSERT INTO table SET INSERT INTO Students SET
column1=value1, FirstName='John',
Insert data column2=value2, LastName='Smith',
... StudentID=123456789,
columnk=valuek; Major='Math';
INSERT INTO table (column1,column2,...) INSERT INTO Students
VALUES (value1,value2...); (StudentID,FirstName,LastName)
VALUES (123456789,'John','Smith);

INSERT INTO table (column1,column2,...) INSERT INTO Students


Insert/Select SELECT statement; (StudentID,FirstName,LastName)
(See below) SELECT StudentID,FirstName,LastName
FROM OtherStudentTable;
WHERE LastName like '%son;

DELETE FROM table DELETE FROM Students


Delete data [WHERE condition(s)]; WHERE LastName='Smith';
(Omit WHERE to delete all data) DELETE FROM Students
WHERE LastName like '%Smith%';
AND FirstName='John';
DELETE FROM Students;

UPDATE table SET UPDATE Students SET


column1=value1, LastName='Jones' WHERE
Updating Data column2=value2, StudentID=987654321;
... UPDATE Students SET
columnk=valuek LastName='Jones', Major='Theatre'
[WHERE condition(s)]; WHERE StudentID=987654321 OR
(MAJOR='Art' AND FirstName='Pete');
Insert column ALTER TABLE table ADD COLUMN ALTER TABLE Students ADD COLUMN
column type options; Hometown varchar(20);
Delete column ALTER TABLE table ALTER TABLE Students
DROP COLUMN column; DROP COLUMN Dorm;
DROP TABLE [IF EXISTS] table; DROP TABLE Animals;
SQL Commands: Querying
What How Example(s)
All columns SELECT * FROM table; SELECT * FROM Students;
Some columns SELECT column1,column2,... FROM table; SELECT LastName, FirstName FROM Students;
Some rows/ SELECT column1,column2,... SELECT LastName,FirstName
columns FROM table FROM Students
[WHERE condition(s)]; WHERE StudentID LIKE '%123%';
No Repeats SELECT [DISTINCT] column(s) SELECT DISTINCT LastName
FROM table; FROM Students;

Ordering SELECT column1,column2,... SELECT LastName,FirstName


FROM table FROM Students
[ORDER BY column(s) [DESC]]; ORDER BY LastName, FirstName DESC;
Column SELECT column1 [AS alias1], SELECT LastName,FirstName AS First
Aliases column2 [AS alias2], ... FROM Students;
FROM table1;
Grouping SELECT column1,column2,... SELECT LastName,COUNT(*)
FROM table FROM Students
[GROUP BY column(s)]; GROUP BY LastName;
Group Filtering SELECT column1,column2,... SELECT LastName,COUNT(*)
FROM table FROM Students
[GROUP BY column(s)] GROUP BY LastName
[HAVING condition(s)]; HAVING LastName like '%son';
Joins SELECT column1,column2,... SELECT LastName,Points
FROM table1,table2,... FROM Students,Assignments
[WHERE condition(s)]; WHERE AssignmentID=12 AND
Students.StudentID=Assignments.StudentID;
Table SELECT column1,column2,... SELECT LastName,Points
Aliases FROM table1 [alias1], FROM Students S,Assignments A
table2 [alias2],... WHERE S.StudentID=A.StudentID AND
[WHERE condition(s)]; A.AssignmentID=12;
Everything
SELECT [DISTINCT] SELECT Points, COUNT(*) AS Cnt
column1 [AS alias1], FROM Students S,Assignments A
column2 [AS alias2], ... WHERE S.StudentID=A.StudentID AND
FROM table1 [alias1], A.AssignmentID=12
table2 [alias2],... GROUP BY Points
[WHERE condition(s)] HAVING Points > 10
[GROUP BY column(s)] ORDER BY Cnt, Points DESC;
[HAVING condition(s)]
[ORDER BY column(s) [DESC]];

You might also like