SQL Commands Cheat Sheet - How To Learn SQL in 10 Minutes
SQL Commands Cheat Sheet - How To Learn SQL in 10 Minutes
Using this SELECT statement, the query selects all the data from all the
columns in the customer’s table and returns data like so:
The asterisk wildcard character (*) refers to “all” and selects all the rows
and columns. We can replace it with specific column names instead — here
only those columns will be returned by the query
SELECT FirstName, LastName FROM customers;
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. Here’s a
basic example adding a new user to the customers table:
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!
SQL outlines syntax that allows you to write queries that manage relational
databases.
In addition to MySQL, there are other systems that implement SQL. Some
of the more popular ones include:
SQLite
Oracle Database
PostgreSQL
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 condit
ANY -- Returns true if any of the subquery values meet the given conditi
CHECK -- Adds a constraint that limits the value which can be added to a
EXISTS -- Checks for the existence of any record within the subquery, re
IS NOT NULL -- The reverse of NULL. Tests for values that aren’t empty /
ROWNUM -- Returns results where the row number meets the passed conditio
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 fro
TRUNCATE TABLE -- Similar to DROP, but instead of deleting the table and
UNION -- Combines the results from 2 or more SELECT statements and retur
VALUES -- Used alongside the INSERT INTO keyword to add new values to a
WHERE -- Filters results to only include data which meets the given cond
Comments in SQL
Comments allow you to explain sections of your SQL statements, without
being executed directly.
/*
*/
/*
TINYBLOB -- Holds Binary Large Objects (BLOBs) with a max length of 255
ENUM(a, b, c, etc…) -- A string object that only has one value, which is
SQL Operators
1. Arithmetic Operators in SQL
+ -- Add
– -- Subtract
* -- Multiply
/ -- Divide
% -- Modulus
SQL Functions
1. String Functions in SQL
ASCII -- Returns the equivalent ASCII value for a specific character.
INSERT -- Allows you to insert one string into another at a certain poin
INSTR -- Returns the position of the first time one string appears withi
LEFT -- Starting from the left, extracts the given number of characters
MID -- Extracts one string from another, starting from any position.
POSITION -- Returns the position of the first time one substring appears
RIGHT -- Starting from the right, extracts the given number of character
SPACE -- Returns a string full of spaces equal to the amount you pass it
SUBSTR -- Extracts one substring from another, starting from any positio
TRIM -- Removes trailing and leading spaces from the given string. Same
CEIL -- Returns the closest whole number (integer) upwards from a given
COUNT -- Returns the amount of records that are returned by a SELECT que
DEGREES -- Converts a radians value to degrees.
FLOOR -- Returns the closest whole number (integer) downwards from a giv
LOG -- Returns the natural logarithm of the given number, or the logarit
MOD -- Returns the remainder of the given number divided by the other gi
PI -- Returns PI.
POW -- Returns the value of the given number raised to the power of the
ROUND -- Rounds the given number to the given amount of decimal places.
DAYOFWEEK -- Returns the index for the weekday for the given date.
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 n
MAKETIME -- Creates a time and returns it, based on the given hour, minu
TO_DAYS -- Returns the total number of days that have passed from ‘00-00
YEARWEEK -- Returns the year and week number for the given date.
CONV -- Converts the given number from one numeric base system into anot
CONVERT -- Converts the given value into the given datatype or character
CURRENT_USER -- Returns the user and hostname which was used to authenti
LAST_INSERT_ID -- For the last row which was added or updated in a table
VERSION -- Returns the current version of the MySQL powering the databas
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.
The table containing the foreign key is called the child key,
The table containing the referenced (or candidate) key is called the parent
table.
This essentially means that the column data is shared between 2 tables,
because a foreign key also prevents invalid data from being inserted which
isn’t also present in the parent table.
Example (MySQL)
Create a new table and turn any column that references IDs in other tables
into foreign keys.
Indexes in SQL
Indexes are attributes that can be assigned to columns that are frequently
searched against to make data retrieval a quicker and more efficient
process.
CREATE INDEX -- Creates an index named ‘idx_test’ on the first_name and
CREATE INDEX idx_test
ON users (first_name, surname);
CREATE UNIQUE INDEX -- The same as the above, but no duplicate values.
CREATE UNIQUE INDEX idx_test
ON users (first_name, surname);
DROP INDEX -- Removes an index.
ALTER TABLE users
DROP INDEX idx_test;
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.
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.
Then in future, if you need to access the stored result set, you can do so like
this: