2. MySQL LIMIT Clause
The MySQL LIMIT clause is used to limit the number of rows
returned by a SELECT query. It allows you to specify the maximum
number of rows to retrieve from the result set. This can be useful
when you have a large dataset and only want to retrieve a subset of
the data. The LIMIT clause is typically used in conjunction with the
SELECT statement.
3. LIMIT Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
The following SQL statement selects the first three records from the "Customers" table:
TRY: SELECT * FROM Customers
LIMIT 3;
What if we want to select records 4 - 6 (inclusive)?
MySQL provides a way to handle this: by using OFFSET.
The SQL query below says "return only 3 records, start on record 4 (OFFSET 3)":
TRY: SELECT * FROM Customers
LIMIT 3 OFFSET 3;
4. ADD a WHERE CLAUSE
The following SQL statement selects the first three records from the "Customers" table,
where the country is "Germany":
Try: SELECT * FROM Customers
WHERE Country='Germany’
LIMIT 3;
5.
6. MySQL MIN() and MAX() Functions
In MySQL, the MIN() and MAX() functions are used to find the
minimum and maximum values, respectively, from a specified column
in a table. These functions are often used in conjunction with the
SELECT statement to perform calculations or retrieve specific data
from a dataset.
7. MIN() Syntax
The MIN() function returns the smallest value of the selected column.
SELECT MIN(column_name)
FROM table_name
WHERE condition;
The following SQL statement finds the price of the cheapest product:
TRY: SELECT MIN(Price) AS SmallestPrice
FROM Products;
8. MAX() Syntax
The MAX() function returns the largest value of the selected column.
SELECT MAX(column_name)
FROM table_name
WHERE condition;
The following SQL statement finds the price of the most expensive
product:
TRY: SELECT MAX(Price) AS LargestPrice
FROM Products;
9.
10. MySQL COUNT(), AVG() and SUM() Functions
In MySQL, the COUNT(), AVG(), and SUM() functions are used to
perform calculations on data in a database table. These aggregate
functions allow you to count rows, calculate averages, and sum up
values in a specified column.
11. COUNT() Syntax
The COUNT() function returns the number of rows that matches a
specified criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
The following SQL statement finds the number of products:
TRY: SELECT COUNT(ProductID)
FROM Products;
12. AVG() Syntax
The AVG() function returns the average value of a numeric column.
SELECT AVG(column_name)
FROM table_name
WHERE condition;
The following SQL statement finds the average price of all products:
TRY: SELECT AVG(Price)
FROM Products;
13. SUM() Syntax
The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name)
FROM table_name
WHERE condition;
The following SQL statement finds the sum of the "Quantity" fields in the
"OrderDetails" table:
TRY: SELECT SUM(Quantity)
FROM OrderDetails;
14.
15. Exercises:
1. Use the MIN function to select the record with the smallest value of
the Price column.
SELECT________
FROM Products;
2. Use an SQL function to select the record with the highest value of
the Price column.
SELECT________
FROM Products;
16. 3. Use the correct function to return the number of records that have the Price value set
to 18.
SELECT ________
FROM Products
WHERE Price = 18;
4. Use an SQL function to calculate the average price of all products.
SELECT ________
FROM Products;
5. Use an SQL function to calculate the sum of all the Price column values in
the Products table.
SELECT ________
FROM Products;
17. Exercises:
1. Use the MIN function to select the record with the smallest value of
the Price column.
SELECTMIN(Price)
FROM Products;
2. Use an SQL function to select the record with the highest value of
the Price column.
SELECTMIN(Price)
FROM Products;
18. 3. Use the correct function to return the number of records that have the Price value set
to 18.
SELECT COUNT(*)
FROM Products
WHERE Price = 18;
4. Use an SQL function to calculate the average price of all products.
SELECT AVE(Price)
FROM Products;
5. Use an SQL function to calculate the sum of all the Price column values in
the Products table.
SELECT SUM(Price)
FROM Products;
19. MySQL LIKE Operator
In MySQL, the LIKE operator is used in a SELECT statement to search for a
specified pattern in a column. It is commonly used to perform pattern matching
within text or string data. The LIKE operator is useful for finding rows that match a
certain pattern or contain specific substrings.
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
20.
21. LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
a. The following SQL statement selects all customers with a CustomerName starting with "a":
TRY: SELECT * FROM Customers
WHERE CustomerName LIKE 'a%’;
b. The following SQL statement selects all customers with a CustomerName ending with "a":
TRY: SELECT * FROM Customers
WHERE CustomerName LIKE ’%a’;
22. LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
c. The following SQL statement selects all customers with a CustomerName that have "or" in any
position:
TRY: SELECT * FROM Customers
WHERE CustomerName LIKE '%or%’;
d. The following SQL statement selects all customers with a CustomerName that have "r" in the
second position:
TRY: SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
23. LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
e. The following SQL statement selects all customers with a CustomerName that starts with "a"
and are at least 3 characters in length:
TRY: SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%’;
f. The following SQL statement selects all customers with a ContactName that starts with "a" and
ends with "o":
TRY: SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
24. LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
g. The following SQL statement selects all customers with a CustomerName that does NOT start
with "a":
TRY: SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
25.
26. Exercises:
1. Select all records where the value of the City column starts with the letter
"a".
SELECT * FROM Customers
WHERE City LIKE ‘a%’;
2. Select all records where the value of the City column ends with the letter
"a".
SELECT * FROM Customers
WHERE City LIKE ‘%a’;
27. Exercises:
3. Select all records where the value of the City column contains the letter "a".
SELECT * FROM Customers
WHERE City LIKE ‘%a%’;
4. Select all records where the value of the City column starts with letter "a"
and ends with the letter "b".
SELECT * FROM Customers
WHERE City LIKE ‘a%b’;
5. Select all records where the value of the City column does NOT start with the
letter "a".
SELECT * FROM Customers
WHERE City NOT LIKE ‘a%’;