SQL Commands Cheat Sheet - How To Learn SQL in 10 Minutes
SQL Commands Cheat Sheet - How To Learn SQL in 10 Minutes
Forum
Donate
With more than 2.5 exabytes of data generated every day, it comes as
no surprise that this data needs to be stored somewhere where we
can access it when we need it.
This article will walk you through a hackable cheatsheet to get you up
and running with SQL quickly.
What is SQL?
SQL stands for Structured Query Language. It is a language for
relational database management systems. SQL is used today to store,
retrieve, and manipulate data within relational databases.
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 1/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Source
Using this SELECT statement, the query selects all the data from all
the columns in the customer’s table and returns data like so:
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 2/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Forum
Donate
This query returns all data from the products table with an age value
of greater than 30.
The use of ORDER BY keyword just means the results will be ordered
using the age column from the lowest value to the highest
Using the INSERT INTO statement, we can add new data to a table.
H ’ b i l ddi
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ t th t t bl 3/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Here’s a basic example adding a new user to the customers table:
Forum
Donate
How do you think that you suddenly got a Youtube ad on shoes when just a
few minutes ago, you were Googling your favourite shoes? That’s SQL (or a
form of SQL) at work!
SQL vs MySQL
Before we move on, I just want to clarify an often-confused topic — the
difference between SQL and MySQL. As it turns out, they aren’t the
same thing!
SQLite
Oracle Database
PostgreSQL
I ll M SQL
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ OS 5/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
There are lots of apps to choose from which largely do the same job,
so it’s down to your own personal preference on which one to use:
When you’re ready to start writing your own SQL queries, consider
importing dummy data rather than creating your own database.
Here are some dummy databases that are available for download free
of charge.
ALL -- Returns true if all of the subquery values meet the passed conditi
ANY -- Returns true if any of the subquery values meet the given conditio
AS -- Renames a table or column with an alias value which only exists for
CHECK -- Adds a constraint that limits the value which can be added to a
Forum
Donate
DROP DATABASE -- Deletes the entire database.
Learn to code — free 3,000-hour curriculum
DROP DEAFULT -- Removes a default value for a column.
EXISTS -- Checks for the existence of any record within the subquery, ret
IS NOT NULL -- The reverse of NULL. Tests for values that aren’t empty /
ROWNUM -- Returns results where the row number meets the passed condition
SELECT INTO -- Copies data from one table and inserts it into another.
SELECT TOP -- Allows you to return a set number of records to return from
TOP -- Used alongside SELECT to return a set number of records from a tab
TRUNCATE TABLE -- Similar to DROP, but instead of deleting the table and
UNION -- Combines the results from 2 or more SELECT statements and return
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 8/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
UNION -- Combines the results from 2 or more SELECT statements and return
Forum
Donate
UNION ALL -- The same as UNION, but includes duplicate values.
Learn to code — free 3,000-hour curriculum
UNIQUE -- This constraint ensures all values in a column are unique.
VALUES -- Used alongside the INSERT INTO keyword to add new values to a t
WHERE -- Filters results to only include data which meets the given condi
Comments in SQL
Comments allow you to explain sections of your SQL statements,
without being executed directly.
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 9/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Forum
Donate
/*
Learn to code — free 3,000-hour curriculum
This is a multiline comment.
*/
/*
You can even put code within a comment to prevent its execution
*/
id int,
FirstName varchar(255)
);
Forum
CHAR(size) -- Fixed length string which can contain letters, numbers
and
Donate
TINYBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 255 b
BLOB(size) -- Holds Binary Large Objects (BLOBs) with a max length of 655
MEDIUMBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 16,
LONGBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 4,294
ENUM(a, b, c, etc…) -- A string object that only has one value, which is
SET(a, b, c, etc…) -- A string object that can have 0 or more values, whi
SQL Operators
1. Arithmetic Operators in SQL
+ -- Add
– -- Subtract
* -- Multiply
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 12/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
* -- Multiply
/ -- Divide
Forum
Donate
% -- Modulus
Learn to code — free 3,000-hour curriculum
| -- Bitwise OR
= -- Equal to
+= -- Add equals
-= -- Subtract equals
*= -- Multiply equals
/= -- Divide equals
%= -- Modulo equals
SQL F ti
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 13/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
INSERT -- Allows you to insert one string into another at a certain point
INSTR -- Returns the position of the first time one string appears within
LEFT -- Starting from the left, extracts the given number of characters f
MID -- Extracts one string from another, starting from any position.
POSITION -- Returns the position of the first time one substring appears
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 14/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Learn
RIGHT -- Starting to code
from — free extracts
the right, 3,000-hour
thecurriculum
given number of characters
SPACE -- Returns a string full of spaces equal to the amount you pass it.
SUBSTR -- Extracts one substring from another, starting from any position
TRIM -- Removes trailing and leading spaces from the given string. Same a
CEIL -- Returns the closest whole number (integer) upwards from a given d
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 15/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
COUNT -- Returns the amount of records that are returned by a SELECT quer
FLOOR -- Returns the closest whole number (integer) downwards from a give
LOG -- Returns the natural logarithm of the given number, or the logarith
MOD -- Returns the remainder of the given number divided by the other giv
PI -- Returns PI.
POW -- Returns the value of the given number raised to the power of the o
ROUND -- Rounds the given number to the given amount of decimal places.
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 16/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Forum
Donate
DAYOFWEEK -- Returns the index for the weekday for the given date.
Learn to code — free 3,000-hour curriculum
DAYOFYEAR -- Returns the day of the year for the given date.
EXTRACT -- Extracts from the date the given part (eg MONTH for 20/01/20
FROM DAYS -- Returns the date from the given numeric date value.
LAST DAY -- Gets the last day of the month for the given date.
MAKEDATE -- Creates a date and returns it, based on the given year and
MAKETIME -- Creates a time and returns it, based on the given hour, min
Learn to code — free 3,000-hour curriculum
TIME -- Returns the time from a given time or datetime.
TO_DAYS -- Returns the total number of days that have passed from ‘00-0
YEARWEEK -- Returns the year and week number for the given date.
CONV -- Converts the given number from one numeric base system into ano
CONVERT -- Converts the given value into the given datatype or characte
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 19/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Forum
CURRENT_USER -- Returns the user and hostname which was used
Donate
to authent
Learn
DATABASE -- Gets thetoname
codeof—the
freecurrent
3,000-hour curriculum
database.
LAST_INSERT_ID -- For the last row which was added or updated in a tabl
VERSION -- Returns the current version of the MySQL powering the databa
Learn
WHERE city LIKE '_ _to_vale';
SQL Keys
In relational databases, there is a concept of primary and foreign keys.
In SQL tables, these are included as constraints, where a table can
have a primary key, a foreign key, or both.
Example (MySQL)
Create a new table and set the primary key to the ID column.
FirstName varchar(255),
address varchar(255)
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 21/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
address varchar(255),
email varchar(255),
Forum
Donate
PRIMARY KEY (id)
The table containing the foreign key is called the child key,
The table containing the referenced (or candidate) key is called the
parent table.
Example (MySQL)
Create a new table and turn any column that references IDs in other
tables into foreign keys.
user_id int,
product_id int,
);
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 22/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
CREATE UNIQUE INDEX -- The same as the above, but no duplicate values.
SQL Joins
In SQL, a JOIN clause is used to return a result which combines data
from multiple tables, based on a common column which is featured in
both of them.
Left Join: Returns all of the records from the first table, along
with any matching records from the second table.
Right Join: Returns all of the records from the second table,
along with any matching records from the first.
Full Join: Returns all records from both tables when there is a
match.
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 23/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Forum
Donate
A common wayLearn
of visualising
to code —how
free joins work is
3,000-hour like this:
curriculum
Views in SQL
A view is essentially an SQL results set that gets stored in the
database under a label, so you can return to it later without having to
rerun the query.
These are especially useful when you have a costly SQL query which
you might need a number of times. So instead of running it over and
over to generate the same results set, you can just do it once and save
it as a view.
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 24/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
Then in future, if you need to access the stored result set, you can do
so like this:
Conclusion
Th j it f b it
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ d li ti l ti ld t b i 25/27
4/3/22, 2:03 PM SQL Commands Cheat Sheet – How to Learn SQL in 10 Minutes
The majority of websites and applications use relational databases in
Forum
Donate
some way or the other. This makes SQL extremely valuable to know as
it allows you toLearn
createtomore
code — free 3,000-hour
complex, curriculum
functional systems.
Jason Dsouza
AI Researcher @Harvard, Teacher @freeCodeCamp. Part-time Compiler
Warlock.
If you read this far, tweet to the author to show them you care.
Tweet a thanks
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
Forum
Donate
Trending Guides
Learn to code — free 3,000-hour curriculum
Our Nonprofit
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/learn-sql-in-10-minutes/ 27/27