WizaRD TV MySQL Notes
MySQL Notes & Interview Cheatsheet
WizaRD TV
September 2025
1 What is SQL?
SQL (Structured Query Language) is a programming language designed for managing
relational databases. It allows you to read, manipulate, and update data.
Why analysts love SQL:
• Easy to learn and understand.
• Direct access to large datasets.
• Queries are easy to audit and replicate compared to spreadsheets.
2 Basic Queries
2.1 SELECT *
SELECT * FROM Sales ;
2.2 Select Columns
SELECT year , month , west FROM Sales ;
2.3 Rename Columns (Alias)
SELECT west AS " west Region " FROM Sales ;
2.4 LIMIT Clause
The LIMIT clause is used to specify the number of records to return.
SELECT * FROM Sales LIMIT 100;
1
2.5 WHERE Clause
The WHERE clause is used to filter records and extract only those that fulfill a specified
condition.
SELECT * FROM Sales WHERE Country = " Canada " ;
3 Comparison Operators on numerical data
The most basic way to filter data is using comparison operators. The following table lists
them:
• Equal to: =
• Not equal to: <> or !=
• Greater than: >
• Less than: <
• Greater than or equal to: >=
• Less than or equal to: <=
SELECT * FROM Sales WHERE city = " kolkata " ;
SELECT * FROM Sales WHERE city != " kolkata " ;
SELECT * FROM Sales WHERE Month > " January " ;
SELECT * FROM Sales WHERE sale_amount < 50000;
4 Arithmetic in SQL
You can perform arithmetic in SQL using the same operators as in Excel: +, -, *, /. In
SQL, you can only perform arithmetic across columns on values in a given row. To add
values across multiple rows, you’ll need to use aggregate functions.
SELECT year , month , west , South , west + south AS south_plus_west FROM sales ;
SELECT year , month , west , South , west + south - 4 * year AS new_column FROM
SELECT year , month , west , South , ( west + south ) / 2 AS south_west_arg FROM sa
5 CREATE TABLE
The CREATE TABLE statement is used to create a new table in a database.
CREATE TABLE person (
PersonID int ,
LastName varchar (255) ,
FirstName varchar (255) ,
Address varchar (255) ,
City varchar (255)
);
2
6 INSERT INTO
The INSERT INTO statement is used to insert new records into a table.
1. To specify both the column names and the values to be inserted, use the following
syntax:
INSERT INTO table_name ( column1 , column2 , column3 ,...)
VALUES ( value1 , value2 , value3 ,...);
2. If you are adding values for all the columns of the table, you do not need to specify
the column names in the SQL query.
INSERT INTO table_name
VALUES ( value1 , value2 , value3 ,...);
7 NULL Values
A field with a NULL value is a field with no value. If a field in a table is optional, it is
possible to insert or update a record without adding a value to this field, and the field
will be saved with a NULL value.
7.1 How to Test for NULL Values?
It is not possible to test for NULL values with comparison operators like =, <, or <>.
Instead, you must use the IS NULL and IS NOT NULL operators.
SELECT customerName , contactName , Address
FROM Sales WHERE Address IS NULL ;
SELECT customerName , ContactName , Address
FROM Sales WHERE Address IS NOT NULL ;
8 UPDATE Statement
The UPDATE statement is used to modify existing records in a table.
UPDATE Sales
SET contactName = " Alan " , city = " Goa "
WHERE customerID = 1;
-- UPDATE Multiple Records
UPDATE sales
SET PostalCode = 00000
WHERE Country = " India " ;
3
9 DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE FROM sales WHERE CustomerName = " Bob " ;
-- Delete All Records
DELETE FROM table_name ;
10 Aliases
Aliases are used to give a table or a column a temporary name. They are often used to
make column names more readable. An alias only exists for the duration of that query
and is created with the AS keyword.
-- Alias Column Example
SELECT column_name AS alias_name FROM table_name ;
-- Alias Table Example
SELECT column_name ( s ) FROM table_name AS alias_name ;
11 SQL Logical Operators
Logical operators allow you to use multiple comparison operators in a single query.
• LIKE allows you to match similar values instead of exact values.
• IN allows you to specify a list of values you would like to include.
• BETWEEN allows you to select only rows within a certain range.
• IS NULL allows you to select rows that contain no data in a given column.
• AND allows you to select only rows that satisfy two conditions.
• OR allows you to select rows that satisfy either of two conditions.
• NOT allows you to select rows that do not match a certain condition.
-- LIKE Operator
SELECT * FROM Sales WHERE " group " LIKE " New % " ;
-- IN Operator
SELECT * FROM Songs WHERE artist IN ( ’ Taylor swift ’ , ’ Usher ’ );
-- BETWEEN Operator
SELECT * FROM Songs WHERE year_rank BETWEEN 5 AND 10;
-- AND Operator
SELECT * FROM Songs WHERE year = 2012 AND year_rank <= 10;
-- OR Operator
SELECT * FROM Songs WHERE year_rank = 5 OR artist = " Sonu " ;
-- NOT Operator
SELECT * FROM Sales WHERE NOT Country = " Japan " ;
-- Combining AND , OR and NOT
SELECT * FROM Sales WHERE country = ’ Japan ’ AND ( city = ’ Goa ’ OR city = ’ Puri
4
12 ORDER BY
SELECT * FROM Sales ORDER BY country , CustomerName ;
SELECT * FROM Sales ORDER BY country ASC , CustomerName DESC ;
13 Using Comments
SELECT * -- This is a select command
FROM Sales
WHERE year = 2020;
/* Here ’ s a comment so long and descriptive that it could only fit on multipl
SELECT *
FROM Sales
WHERE year = 2015;
14 SQL Aggregate Functions
SQL is excellent at aggregating data the way you might in a pivot table in Excel. The
functions themselves are the same ones you will find in Excel or any other analytics
program.
• COUNT counts how many rows are in a particular column.
• SUM adds together all the values in a particular column.
• MIN and MAX return the lowest and highest values in a particular column.
• AVG calculates the average of a group of selected values.
SELECT COUNT (*) FROM Sales ;
SELECT COUNT ( column_name ) FROM table_name WHERE condition ;
SELECT SUM ( column_name ) FROM table_name WHERE condition ;
SELECT MIN ( column_name ) FROM table_name WHERE condition ;
SELECT MAX ( column_name ) FROM table_name WHERE condition ;
SELECT AVG ( column_name ) FROM table_name WHERE condition ;
15 GROUP BY and HAVING
15.1 The SQL GROUP BY clause
The GROUP BY clause allows you to separate data into groups, which can be aggregated
independently of one another.
SELECT year , COUNT (*) AS count FROM sales GROUP BY year ;
-- Multiple columns
SELECT year , month , COUNT (*) AS count FROM sales GROUP BY year , month ;
-- GROUP BY Column numbers
5
SELECT year , month , COUNT (*) AS count FROM sales GROUP BY 1 , 2;
-- Using GROUP BY with ORDER BY
SELECT year , month , COUNT (*) AS count FROM Sales GROUP BY year , month ORDER B
-- Using GROUP BY with LIMIT
SELECT column_name FROM table_name WHERE condition GROUP BY column_name LIMIT
15.2 HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
SELECT column_name ( s ) FROM table_name WHERE condition
GROUP BY column_name ( s ) HAVING condition ORDER BY column_name ( s );
SELECT year , month , MAX ( high ) AS month_high FROM Sales
GROUP BY year , month HAVING MAX ( high ) > 400
ORDER BY year , month ;
16 The SQL CASE statement
The CASE statement is SQL’s way of handling if/then logic. Every CASE statement must
end with the END statement. The ELSE statement is optional.
SELECT orderID , Quantity ,
CASE
WHEN Quantity > 30 THEN " The quantity is greater than 30 "
WHEN Quantity = 30 THEN " The quantity is 30 "
ELSE " The quantity is under 30 "
END AS QuantityText
FROM sales ;
17 SQL DISTINCT
You’ll occasionally want to look at only the unique values in a particular column.
SELECT DISTINCT month FROM Sales ;
SELECT DISTINCT year , month FROM Sales ;
-- Using DISTINCT in aggregations
SELECT COUNT ( DISTINCT month ) AS unique_months FROM Sales ;
18 MySQL JOINS
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
SELECT * FROM benn . c o l l e g e _ f o o t b a l l _ p l a y e r s players
JOIN benn . c o l l e g e _ f o o t b a l l _ t e a m s teams
ON teams . school_name = players . school_name ;
6
18.1 Supported Types of JOINS
• INNER JOIN: Returns records that have matching values in both tables.
• LEFT JOIN: Returns all records from the left table, and the matched records from
the right table.
• RIGHT JOIN: Returns all records from the right table, and the matched records from
the left table.
• CROSS JOIN: Returns all records from both tables.
-- INNER JOIN
SELECT column_name ( s ) FROM table1 INNER JOIN table2
ON table1 . column_name = table2 . column_name ;
-- LEFT JOIN
SELECT column_name ( s ) FROM table1 LEFT JOIN table2
ON table1 . column_name = table2 . column_name ;
-- RIGHT JOIN
SELECT column_name ( s ) FROM table1 RIGHT JOIN table2
ON table1 . column_name = table2 . column_name ;
-- CROSS JOIN
SELECT column_name ( s ) FROM table1 CROSS JOIN table2 ;
-- SELF JOIN
SELECT column_name ( s ) FROM table1 T1 , table1 T2 WHERE condition ;
19 UNION Operator
UNION allows you to stack one dataset on top of the other.
SELECT column_name ( s ) FROM table1
UNION
SELECT column_name ( s ) FROM table2 ;
SELECT column_name ( s ) FROM table1
UNION ALL
SELECT column_name ( s ) FROM table2 ;
20 IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SELECT * FROM sales WHERE country IN ( " India " , " Nepal " , " UK " );
SELECT * FROM sales WHERE country NOT IN ( " India " , " Nepal " , " UK " );
SELECT * FROM sales WHERE country IN ( SELECT country FROM Suppliers );
21 EXISTS Operator
The EXISTS operator is used to test for the existence of any record in a subquery.
SELECT column_name ( s ) FROM table_name
WHERE EXISTS ( SELECT column_name FROM table_name WHERE condition );
7
22 ANY and ALL Operators
These operators allow you to perform a comparison between a single column value and a
range of other values.
• ANY returns TRUE if any of the subquery values meet the condition.
• ALL returns TRUE if all of the subquery values meet the condition.
SELECT ProductName FROM sales
WHERE ProductID = ANY ( SELECT ProductID FROM OrderDetails WHERE Quantity > 99
SELECT ProductName FROM sales
WHERE ProductID = ALL ( SELECT ProductID FROM OrderDetails WHERE Quantity = 10
23 INSERT INTO SELECT
The INSERT INTO SELECT statement copies data from one table and inserts it into an-
other table.
INSERT INTO table2
SELECT * FROM table1 WHERE condition ;
INSERT INTO table2 ( column1 , column2 , column3 , ...)
SELECT column1 , column2 , column3 , ... FROM table1 WHERE condition ;
24 IFNULL() Function
The IFNULL() function lets you return an alternative value if an expression is NULL.
SELECT contactname , IFNULL ( bizphone , homephone ) AS phone
FROM contacts ;
SELECT name , IFNULL ( officephone , mobilephone ) AS contact
FROM employee ;