MySQL Basic Keywords Cheat Sheet
MySQL Basic Keywords Cheat Sheet
This cheat sheet covers the most essential MySQL keywords you’ll use daily. Whether you’re a beginner or need a quick refresher, these commands form the foundation of SQL queries.
1. SELECT & FROM
Used to retrieve data from a database.
Syntax:
SELECT column1, column2, ...
FROM table_name;
Example:
-- Selects the 'FirstName' and 'LastName' columns from the 'Employees' table
SELECT FirstName, LastName
FROM Employees;
2. WHERE
Filters records based on a specified condition.
Syntax:
SELECT column1, ...
FROM table_name
WHERE condition;
Example:
-- Selects all customers from 'USA'
SELECT *
FROM Customers
WHERE Country = 'USA';
3. INSERT INTO
Inserts new records into a table.
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
-- Adds a new customer
INSERT INTO Customers (CustomerName, ContactName, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Norway');
4. UPDATE & SET
Modifies existing records in a table. Always use with WHERE to avoid updating all records!
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
-- Updates the contact name for customer with ID 1
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
5. DELETE FROM
Deletes existing records in a table. Always use with WHERE!
Syntax:
DELETE FROM table_name WHERE condition;
Example:
-- Deletes the customer with 'Alfreds Futterkiste' as the name
DELETE FROM Customers
WHERE CustomerName = 'Alfreds Futterkiste';
6. ORDER BY
Sorts the result set in ascending or descending order.
Syntax:
SELECT column1, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example:
-- Sorts products by price (High to Low)
SELECT ProductName, Price
FROM Products
ORDER BY Price DESC;
7. DISTINCT
Returns only distinct (different) values.
Syntax:
SELECT DISTINCT column1, ...
FROM table_name;
Example:
-- Selects only unique countries from the Customers table
SELECT DISTINCT Country
FROM Customers;
8. LIMIT
Specifies the number of records to return. Very useful for large tables or pagination.
Syntax:
SELECT column1, ...
FROM table_name
LIMIT number;
Example:
-- Selects the first 5 records from Customers
SELECT *
FROM Customers
LIMIT 5;
9. LIKE & Wildcards
Searches for a specified pattern in a column.
%- Represents zero, one, or multiple characters_- Represents a single character
Syntax:
SELECT column1, ...
FROM table_name
WHERE columnN LIKE pattern;
Example:
-- Selects customers starting with 'a'
SELECT *
FROM Customers
WHERE CustomerName LIKE 'a%';
10. IN
Allows you to specify multiple values in a WHERE clause.
Syntax:
SELECT column1, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example:
-- Selects customers located in Germany, France, or UK
SELECT *
FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
11. BETWEEN
Selects values within a given range. The values can be numbers, text, or dates.
Syntax:
SELECT column1, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
-- Selects products with a price between 10 and 20
SELECT *
FROM Products
WHERE Price BETWEEN 10 AND 20;
12. JOIN (INNER, LEFT, RIGHT)
Combines rows from two or more tables, based on a related column between them.
Syntax (INNER JOIN):
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example:
-- Selects orders along with customer information
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
13. ALIAS (AS)
Gives a table, or a column in a table, a temporary name.
Syntax:
SELECT column_name AS alias_name
FROM table_name;
Example:
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
14. GROUP BY
Groups rows that have the same values into summary rows, usually used with aggregate functions (COUNT, MAX, MIN, SUM, AVG).
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example:
-- Lists the number of customers in each country
SELECT Country, COUNT(CustomerID)
FROM Customers
GROUP BY Country;
15. HAVING
Used instead of WHERE with aggregate functions (because WHERE cannot be used with aggregates).
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example:
-- Lists countries with more than 5 customers
SELECT Country, COUNT(CustomerID)
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;