0% found this document useful (0 votes)
4 views

1742292145_MySQL_Codes

The document provides a comprehensive overview of various MySQL commands and operations, including SELECT statements, operators (AND, OR, NOT), NULL handling, UPDATE and DELETE operations, LIMIT clauses, and aggregate functions like COUNT, AVG, and SUM. It also covers the use of the LIKE operator, IN and BETWEEN operators, and different types of JOINs (INNER, CROSS, LEFT, RIGHT) for combining data from multiple tables. Additionally, it discusses the GROUP BY statement for summarizing data and the UNION operator for combining result sets.

Uploaded by

TANISHA SINHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1742292145_MySQL_Codes

The document provides a comprehensive overview of various MySQL commands and operations, including SELECT statements, operators (AND, OR, NOT), NULL handling, UPDATE and DELETE operations, LIMIT clauses, and aggregate functions like COUNT, AVG, and SUM. It also covers the use of the LIKE operator, IN and BETWEEN operators, and different types of JOINs (INNER, CROSS, LEFT, RIGHT) for combining data from multiple tables. Additionally, it discusses the GROUP BY statement for summarizing data and the UNION operator for combining result sets.

Uploaded by

TANISHA SINHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

MySQL Codes

SQL
Commands
MySQL SELECT Statement
SELECT CustomerName, City, Country FROM Customers;

SELECT * FROM Customers;


MySQL AND, OR and NOT Operators
SELECT * FROM Customers
WHERE Country = 'Germany' AND City = 'Berlin’;

SELECT * FROM Customers


WHERE City = 'Berlin' OR City = 'Stuttgart’;

SELECT * FROM Customers


WHERE NOT Country = 'Germany';
MySQL NULL
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
MySQL NOT NULL
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
MySQL UPDATE SINGLE RECORD
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;

What will happen if the WHERE


clause is omitted?
MySQL Update Multiple Records
UPDATE Customers
SET PostalCode = 00000
WHERE Country = 'Mexico';
MySQL DELETE Statement
DELETE FROM Customers WHERE CustomerName='Alfreds
Futterkiste';
MySQL Delete All Records
• DELETE FROM Customers;

Will it DELETE the TABLE OR


ONLY ROWS?
MySQL LIMIT Clause
SELECT * FROM Customers
LIMIT 3;
MySQL LIMIT and OFFSET
SELECT * FROM Customers
LIMIT 3 OFFSET 10;
MySQL WHERE and LIMIT
• SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
MYSQL ORDER BY and LIMIT Clause
SELECT * FROM Customers
ORDER BY Country
LIMIT 3;
MySQL MIN() and MAX()
SELECT MIN(Price)
FROM Products;

SELECT MAX(Price)
FROM Products;
MySQL MIN() and MAX() with AS
SELECT MIN(Price) AS SmallestPrice
FROM Products;

SELECT MAX(Price) AS LargestPrice


FROM Products;
MySQL Aliases
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

Guess the Output !!


MySQL COUNT(), AVG() and SUM()
FUNCTIONS
SELECT COUNT(ProductID)
FROM Products;

SELECT AVG(Price)
FROM Products;

SELECT SUM(Quantity)
FROM OrderDetails;
MySQL LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.

• There are two wildcards often used in conjunction with the LIKE
operator:

• The percent sign (%) represents zero, one, or multiple characters


• The underscore sign (_) represents one, single character

The percent sign and the underscore can also be used in combinations!
LIKE operators with '%' and '_' wildcards
MySQL LIKE Operator
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%’;

SELECT * FROM Customers SELECT * FROM Customers


WHERE CustomerName LIKE '%a’;
WHERE City LIKE 'ber%';

SELECT * FROM Customers


WHERE CustomerName LIKE '_r%’;

SELECT * FROM Customers


WHERE CustomerName LIKE 'a__%';
MySQL IN Operator
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK’);

SELECT * FROM Customers


WHERE Country NOT IN ('Germany', 'France', 'UK');
MySQL BETWEEN Operator
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;

SELECT * FROM Products


WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);
MySQL Joins
SELECT Orders.OrderID, Customers.CustomerName,
Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.
CustomerID;

GUESS WHAT WILL INNER JOIN select ??


MySQL Joins
MySQL Joins
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
CROSS JOIN Orders;
MySQL Joins
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
CROSS JOIN Orders
WHERE Customers.CustomerID=Orders.CustomerID;
MySQL GROUP BY Statement
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

Guess the output !!


Output
What will be the MySQL Query for : What are the customer IDs and names of all
customers, along with the order IDs for all the orders they have placed in a
sequential manner?
Query: What are the customer IDs and names of all
customers, along with the order IDs for all the orders
they have placed?
SELECT Customer_T.CustomerID, Order_T.CustomerID, CustomerName, OrderID
FROM Customer_T, Order_T
WHERE Customer_T.CustomerID = Order_T. CustomerID
ORDER BY OrderID
OR

SELECT Customer_T.CustomerID, CustomerName, OrderID


FROM Customer_T
INNER JOIN Order_T ON Customer_T.CustomerID = Order_T. CustomerID
ORDER BY OrderID
OUTPUT
Example-2: Customers Data
Orders Data
Find OUT
SELECT Customers.CustomerID, Orders.CustomerID
FROM Customers, Orders

• Number of Records in Customers Table: 91


• Number of Records in Orders Table: 830

• Number of Records in OUTPUT: ??????????????


Find OUT
SELECT Customers.CustomerID, Orders.OrderID
FROM Customers
CROSS JOIN Orders;

• Number of Records in Customers Table: 91


• Number of Records in Orders Table: 830

• Number of Records in OUTPUT: ??????????????

The CROSS JOIN keyword returns all records from both tables (table1 and table2).
Find OUT
SELECT Orders.OrderID, Customers.CustomerID
FROM Orders
INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;

• Number of Records in Customers Table: 91


• Number of Records in Orders Table: 830
• Number of Records in OUTPUT: ??????????????
Find OUT
SELECT Orders.OrderID, Customers.CustomerID
DISTINCT CustomerID in
FROM Orders Orders Table: 89

LEFT JOIN Customers DISTINCT OrderID in


Orders Table: 830
ON Orders.CustomerID = Customers.CustomerID

• Number of Records in Customers Table: 91


• Number of Records in Orders Table: 830
• Number of Records in OUTPUT: ??????????????

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records (if any) from the right
table (table2).
Find OUT
SELECT Orders.OrderID, Customers.CustomerID
DISTINCT CustomerID in
FROM Orders Orders Table: 89
RIGHT JOIN Customers DISTINCT OrderID in
ON Orders.CustomerID = Customers.CustomerID Orders Table: 830

• Number of Records in Customers Table: 91


• Number of Records in Orders Table: 830
• Number of Records in OUTPUT: ??????????????

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records (if any) from the left table
(table1).
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL
Which will be the Result for Union and Union ALL ??
• The GROUP BY statement groups rows that have the
same values into summary rows, like "find the number of
customers in each country".
• The GROUP BY statement is often used with aggregate
functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group
the result-set by one or more columns.
SQL SQL
Commands
Commands

You might also like