MySQL 2
MySQL 2
show databases;
drop database -Database Name- //Delete a database
1. Create A Database
create database -Name-;
2. Table Creation
use -database -Name-; // To state the database which will be used
create table -Table Name-(attributes of the table) //e.g. create table
user(id int(11) primary key, name varchar(30) not null, email varchar(30) not
null);
show tables; //show existing tables in the current database
desc -Table Name-; //show how the table is created
3. Table Manipulation
insert into -Table Name- values(attribute values); //e.g. insert into
user values("1","Mark","[email protected]");
select * from -Table Name-; //show the table and the attributes e.g.
select * from user;
update set -Table Name- set -What to change- where -Number-; //e.g.
update user set name="Jake" where id="1";
alter table -Table Name- add column -Attribute-; //e.g. alter table
user add column password varchar(30) not null;
delete -Table Name- where -What to delete-; //e.g. delete from user
where id="1";
6. INSERT INTO - The INSERT INTO statement is used to insert new records in a
table.
Basic Structure(Specified): INSERT INTO table_name (column1, column2,
column3, ...) VALUES (value1, value2, value3, ...);
Basic Structure(All Column): INSERT INTO table_name VALUES (value1,
value2, value3, ...);
9. MIN, MAX
Basic Structure: SELECT MIN(column_name) FROM table_name WHERE
condition;
SELECT MIN(Price) AS SmallestPrice FROM Products;
SELECT MAX(Price) AS SmallestPrice FROM Products;
11. LIKE - The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.
Basic Structure: SELECT column1, column2, ... FROM table_name WHERE
columnN LIKE pattern;
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
SELECT * FROM Customers WHERE CustomerName LIKE 'a%'; //Starting with
"a"
SELECT * FROM Customers WHERE CustomerName LIKE '%a'; //Ending with "a"
SELECT * FROM Customers WHERE CustomerName LIKE '%or%'; //Have "or" in
any position
SELECT * FROM Customers WHERE CustomerName LIKE '_r%'; //Have "r" as
the second letter
SELECT * FROM Customers WHERE CustomerName LIKE 'a__%'; //Starts with
"a" and are at least 3 characters in length
SELECT * FROM Customers WHERE ContactName LIKE 'a%o'; //Start with "a"
ends with "o"
SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%'; //No Letter
"a"
13. BETWEEN - The BETWEEN operator selects values within a given range. The
values can be numbers, text, or dates.
Basic Structure: SELECT column_name(s) FROM table_name WHERE
column_name BETWEEN value1 AND value2;
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
15 JOIN - A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.
Basic Structure: SELECT Orders.OrderID, Customers.CustomerName,
Orders.OrderDate FROM Orders INNER JOIN Customers ON
Orders.CustomerID=Customers.CustomerID;
INNER JOIN: Returns records that have matching values in both tables
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER
JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
3 Tables: SELECT Orders.OrderID, Customers.CustomerName,
Shippers.ShipperName FROM ((Orders INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID) INNER JOIN Shippers ON Orders.ShipperID =
Shippers.ShipperID);
LEFT JOIN: Returns all records from the left table, and the matched
records from the right table
SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT
JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY
Customers.CustomerName;
RIGHT JOIN: Returns all records from the right table, and the matched
records from the left table
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID ORDER
BY Orders.OrderID;
CROSS JOIN: Returns all records from both tables
SELECT Customers.CustomerName, Orders.OrderID FROM Customers
CROSS JOIN Orders;
SELF JOIN
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS
CustomerName2, A.City FROM Customers A, Customers B WHERE A.CustomerID <>
B.CustomerID AND A.City = B.City ORDER BY A.City;
16. UNION - The UNION operator is used to combine the result-set of two or
more SELECT statements.
Basic Structure: SELECT column_name(s) FROM table1 UNION SELECT
column_name(s) FROM table2;
Basic Structure(Allow Duplicates): SELECT column_name(s) FROM table1
UNION ALL SELECT column_name(s) FROM table2;
SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY
City;
SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER
BY City;
17. GROUP BY - The GROUP BY statement groups rows that have the same values
into summary rows, like "find the number of customers in each country".
Basic Structure - SELECT column_name(s) FROM table_name WHERE condition
GROUP BY column_name(s) ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
18. HAVING - The HAVING clause was added to SQL because the WHERE keyword
cannot be used with aggregate functions.
Basic Structure - SELECT column_name(s) FROM table_name WHERE condition
GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country
HAVING COUNT(CustomerID) > 5;
19. EXISTS - The EXISTS operator is used to test for the existence of any
record in a subquery.
Basic Structure - SELECT column_name(s) FROM table_name WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName
FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);
20. ANY, ALL - The ANY and ALL operators allow you to perform a comparison
between a single column value and a range of other values.
Basic Structure(ANY) - SELECT column_name(s) FROM table_name WHERE
column_name operator ANY SELECT column_name FROM table_name WHERE condition);
Basic Structure(ALL) - SELECT ALL column_name(s) FROM table_name WHERE
condition;
SELECT ProductName FROM Products WHERE ProductID = ANY (SELECT
ProductID FROM OrderDetails WHERE Quantity = 10);
SELECT ALL ProductName FROM Products WHERE TRUE;
SELECT ProductName FROM Products WHERE ProductID = ALL (SELECT
ProductID FROM OrderDetails WHERE Quantity = 10);
21. INSERT INTO SELECT - The INSERT INTO SELECT statement copies data from
one table and inserts it into another table. With Matching data types and target
tables
Basic Structure - INSERT INTO table2 SELECT * FROM table1 WHERE
condition;
Basic Structure - INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ... FROM table1 WHERE condition; //Selected
INSERT INTO Customers (CustomerName, City, Country) SELECT
SupplierName, City, Country FROM Suppliers;
INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country) SELECT SupplierName, ContactName, Address, City, PostalCode,
Country FROM Suppliers;
22. CASE - The CASE statement goes through conditions and returns a value
when the first condition is met (like an if-then-else statement). So, once a
condition is true, it will stop reading and return the result. If no conditions are
true, it returns the value in the ELSE clause.
Basic Structure - CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
LAST_DAY - Extracts the last day of the month for a given date
Syntax: LAST_DAY(date)
Example: LAST_DAY('2025-01-11') ('2025-01-31')
QUARTER - Returns the quarter of the year for a given date value
Syntax: QUARTER(date)
Example: QUARTER('2025-01-11') (1)
TO_DAYS - Returns the number of days between a date and date "0000-00-
00"
Syntax: TO_DAYS(date)
Example: TO_DAYS('2025-01-11') (738204)
YEARWEEK - Returns the year and week number for a given date
Syntax: YEARWEEK(date)
Example: YEARWEEK('2025-01-11') (202502)
CASE Goes through conditions and returns a value when the first
condition is met
Syntax
CASE WHEN condition THEN result [WHEN condition THEN result] ... [ELSE
result] END
Example
SELECT CASE WHEN 1 > 0 THEN 'True' ELSE 'False' END; -- Output: True
CURRENT_USER Returns the user name and host name for the current
MySQL account
Syntax
CURRENT_USER()
Example
SELECT CURRENT_USER(); -- Output: user@host
SESSION_USER Returns the current MySQL user name and host name
Syntax
SESSION_USER()
Example
SELECT SESSION_USER(); -- Output: user@host
SYSTEM_USER Returns the current MySQL user name and host name
Syntax
SYSTEM_USER()
Example
SELECT SYSTEM_USER(); -- Output: user@host
USER Returns the current MySQL user name and host name
Syntax
USER()
Example
SELECT USER(); -- Output: user@host
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
NOTE 1:
-- Displaying existing databases
SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.03 sec)
NOTE 2
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| contacts |
| firstdatabase |
| information_schema |
| mysql |
| new_database |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
9 rows in set (0.00 sec)