You are on page 1of 16

Basic SQL

by Tardigrade Team
Outline

Data Types
Basic commands
Aggregate function
Aliasing and grouping
Joins and Union
Operator and Function
Data Types

String Boolean NULL


Prefixed with (‘) or (“). Returns a truth value, i.e. Shows no data.
Contains a series of TRUE and FALSE (or 1 and Represented with
letters/characters 0 in numeric) NULL

Numeric Date and Time


A series of numbers, including
A string starting with
whole numbers, fractions, positive
DATE, TIME, and
and negative as well as scientific
TIMESTAMP
Basic commands

Select column Row filter

Sort Data
Basic commands
Select column
It starts with the SELECT command.

SELECT col_1, col_2 FROM table_name

LIMIT will limit the query results to the number of integers we specify

SELECT * FROM table_name LIMIT 10,

Which means taking all the columns in table_name and limiting the query row to 10 pieces
DISTINCT will display the unique value of that column

SELECT DISCTINCT col_1 FROM table_name


Basic commands
Row filter (WHERE)
The word WHERE will start to filter the queryed data. Here logical operators such as =,
!=, <, >, <=, >= are used in the filtering process. WHERE can be combined with other
logical operators such as :

SELECT*
FROM_____
WHERE __Statement1 OR__Statement2 Used to combine 2 or

OR
more conditions to filter
with at least 1 condition
WHERE ____ IN ('Statement1 ','Statement1 ') must be true
Basic commands
Row filter (WHERE)
Used to combine 2 or
SELECT*
more conditions to filter
FROM_____
WHERE __Statement1 AND __Statement2
AND
with all conditions must be
true

WHERE(____ BETWEEN Statement1 AND Statement2 );

Can be filtered for NULL values ​using the command

WHERE __Statement1 IS NULL WHERE __Statement1 IS NOT NULL

Pro Tip! : For selecting range of date/ time, you can use WHERE BETWEEN to replace AND
Basic commands
Row filter (LIKE)
LIKE can be used to perform string searches. The character '%' is used to mark
match characters, such as
a% : find the contents of the field with the prefix "a"
%a : find the contents of the field with the suffix "a"
%a% : find the contents of the field containing the letter "a" anywhere
a%a : find the contents of a field that begins and ends with the letter "a"
LIKE is case sensitive, which means LIKE “a” != LIKE “A”
NOT LIKE is the opposite of LIKE, avoiding fields that contain match characters
Basic commands
Sort Data
ORDER BY is used to sort the query results. ORDER BY by default will sort from smallest to
largest, ORDER BY col_name DESC will do the opposite.

SELECT *
FROM ____
ORDER BY ____ DESC
LIMIT ____
Aggregate function

The Aggregate function can be used to manipulate data in selected columns in


a particular table. The aggregate functions in mySQL are as follows:
COUNT : Counts the number of rows in the corresponding column
AVG : Calculates the average of the corresponding column
MIN : calculates the smallest value in the corresponding column
MAX : calculates the largest value in the corresponding column
SUM: calculates the sum of all values ​in the corresponding column

Arithmetic operations between columns can be performed on SQL. Operations that


can be used are addition (+), subtraction (-), multiplication (*) and division (/)
Aliasing, Grouping, and Having
Sometimes writing a When aggregation is done,
GROUP BY can be
function over and over WHERE cannot be used for
applied to group
again can make the filtering data. Instead HAVING
data so that
function too long and is used in the filtering clause
aggregation can
difficult to read, so you
be done based on
can use AS for aliasing a
the group SELECT____,
variable
COUNT(*) ___
ROUND(AVG(___)) ___
SELECT SELECT ____,
FROM ___
AVG(____) AS ____, COUNT(*) ____
GROUP BY ____
SUM(____) AS ____ FROM _____
HAVING AVG(___)>___
FROM _____ GROUP BY _____
JOINs and UNIONS

JOIN is used to join one


table with another table.
JOIN has several types,
namely LEFT JOIN,
INNER JOIN, RIGHT
JOIN, and FULL JOIN
JOINs and UNIONS

SELECT * SELECT *
FROM ____ FROM ____
LEFT JOIN ____ INNER JOIN ____
ON ____ = ____ ON ____ = ____

SELECT ___ AS ___, SELECT *FROM ____


'___' AS __ LEFT JOIN ____ ON ____ =____
UNION ALL UNION
SELECT ____ AS ___, '___' AS __ SELECT * FROM ____
RIGHT JOIN ____ ON ____ = ____

Pro Tip! : FULL JOIN is not directly available in MySQL, instead use UNION on both LEFT and RIGHT JOIN
CAST
Can be used to change the data type, for
example CAST(value as datatype) here is
the datatype in sql

SELECT CASE WHEN ____ > __THEN


‘TRUE STATEMEN 1”
WHEN ____ > __ THEN ‘TRUE
STATEMENT 2”
ELSE ‘ELSE STATEMENT” END

Pro Tip! : Change > to other logical values to adjust (i.e. : <, =, <>, etc)
COALESCE
Used to replace the contents of the NULL value to the value we want.
COALESCE(col_name, desired_value)

TIMESTAMP FUNCTIONS
EXTRACT (datepart FROM col_name) can be used to extract datepart from related
columns, such as year, quarter, month, week, day, hour, minute, second, up to microseconds
DATE_ADD or DATE_SUB(date_col, datepart interval) can be used to add or subtract time
from the date column with datepart interval as above respectively
DATE_FORMAT(col_name, 'format') can be used to convert the date column to the format
we specify i.e. %Y%M%d
Stay Tu ne d, f or
AD VA NC E D S QL
Que r ie s !

You might also like